Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  AI Sprite Change  (Read 3117 times)
0 Members and 1 Guest are viewing this topic.
Offline Zaroth

Senior Newbie





« Posted 2010-04-04 03:14:44 »

Screenshot
http://i44.tinypic.com/n5ih6u.jpg

It's just a simulation for a school project where there are 2 teams. Basicly what happens is that an "Actor" finds the closest "Actor" to him and starts going to that location or runs away, depending on what race they are. Thing is, I'm not really sure how to change the sprite image based on there direction. Any ideas?

P.S, There will be an up, down, left, and right image



   public void move (Actor aTargetActor)
   {
      double xDirection, yDirection, radian;
      double dX, dY = 0;
      
      if(!actorCollision(this, aTargetActor)){
       xDirection = aTargetActor.p2dLocation.getX() - this.p2dLocation.getX();
       yDirection = aTargetActor.p2dLocation.getY() - this.p2dLocation.getY();       
       
       radian = Math.atan2(yDirection, xDirection);
              
      dY = (this.p2dLocation.getY()-(Math.sin(radian)/100)*this.getSpeed());
       dX = (this.p2dLocation.getX()-(Math.cos(radian)/100)*this.getSpeed());

      this.p2dLocation.setLocation(dX, dY);
       }
   }
   
   
   public boolean actorCollision(Actor a, Actor b)
   {
   double xD, yD;

   xD = Math.abs(a.getLocation().getX() - b.getLocation().getX());
   yD = Math.abs(a.getLocation().getY() - b.getLocation().getY());

   if (xD > 20.0)
   {
      if (yD > 20.0)
      return true;
   }
   return false;
   }
}








Offline zoto

Senior Member


Medals: 4



« Reply #1 - Posted 2010-04-06 12:30:14 »

Create a static final int for each direction with values from 0-3.
Create an array of images that match up with the direction variables.
Add a direction variable to the actor class.
Set the direction variable based on the dx and dy in the actors move method.
Draw the image from the image array using the direction variable as the index.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (39 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (36 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!