Enemy Speed

Hi, I’m building a java game with tileMap

I’ve a Sprite class and some Enemy class that extends Sprite (ex. Enemy1, Enemy2, Enemy3…)

The Sprites are added in a LinkedList in the Tilemap Class with

addSprite(map,enemy1Sprite,x,y);

and are got with

 Iterator i = map.getSprites();
        while(i.hasNext())

Now I want to get velocity (speed for Enemy1 is differetn by Enemy2, Enemy3)

How I can Do this??

(some code example??)

You need to have an object that holds both speed (and position) and the sprite:

class Entity
{
// position, speed, sprite
}

Iterator it = map.getEntities();
while(it.hasNext())
get pos/spd/sprite

My sprite class is like this:

 
public class Sprite
{
    public Animation anim;
    float x;
    float y;
    float dx;
    float dy;    
    
    public Sprite(Animation anim)
    {
        this.anim = anim;        
        anim.start();
    }
    
    public void update(long elapsedTime)
    {        
        x += dx * elapsedTime;  
        anim.update(elapsedTime);
    }
    
    public void setX(float x)
    {
        this.x = x;
    }
    
    public void setY(float y)
    {
        this.y = y;
    }
    
    public float getX()
    {
        return x;
    }
    
    public float getY()
    {
        return y;
    }
    
    public void setDx(float dx)
    {
        this.dx = dx;
    }
    
    public void setDy(float dy)
    {
        this.dy = dy;
    }
    
    public float getDx()
    {
        return dx;
    }
    
    public float getDy()
    {
        return dy;
    } 
    
    public Image getImage()
    {
        return anim.getImage();
    }
    
    public int getWidth()
    {
        return anim.getImage().getWidth(null);
    }
    
    public int getHeight()
    {
        return anim.getImage().getHeight(null);
    }
    
    public Object clone()
    {
        return new Sprite(anim);
    }     
}


and my Enemy1 class is:


public class Enemy1 extends Creature
{
    private Animation left;   
    
    public Enemy1(Animation left)
    {
        super(left);        
    }
}

So… I’ve to modify Sprite class and to insert Entity class??
}

Use the

code

tag. (the # button) :slight_smile:

help!!

I see a dx and a dy.

Assuming thats dx per ms and dy per ms then distance per ms is Math.sqrt ((dxdx)+(dydy))

If thats not your question then you are ging to have to explain it with a lot mroe detail, and I dontmean more code, I mean explain clearly what you are trying to accomplish.