简体   繁体   中英

setting enemy movement speed to less than 1

In the game that I'm creating I want the zombies to be twice as slow as the player put I don't want to set the players movement speed to 2 because it moves way to fast then. This is the code that controls the zombies speed:

Zombie z = (Zombie) zombie.get(i);
if(z.getY() > player.getY()){
        z.setY(z.getY() - 1);
}
if(z.getY() < player.getY()){
        z.setY(z.getY() + 1);
}
if(z.getX() > player.getX()){
        z.setX(z.getX() - 1);
}
if(z.getX() < player.getX()){
        z.setX(z.getX() + 1);
}

I have tried to use (int) .5f , (int) .5 and 1 / 2 but all of them makes the zombie stand completly still.

.5 and 1/2 aren't integers. Math 101. You can't cast a float as an int just by putting an f after it. Try removing the (int) cast.

I think that what you want to do is to declare your Zombie's x and y fields as doubles, not ints, and thus the setX(...) and setY(...) methods would accept double and the getX() and getY() return doubles. If you need int values for your imaging then you can either cast or round the values at the time that this is needed, or give Zombie methods to do this, getIntValueX() and getIntValueY() .

Also you could just use a single Point2D.Double field in place of your x and y fields.

I was also stuck with this problem. But it's just a simple issue.

for example:

private int x = 100;
private float speed = 0.5f;

x += speed;

this will increase the x position by a float but the x value is an integer so it automaticly rounds off the float so it'll be 0

solution:

private float x = 100;
private float speed = 0.5f;

x += speed;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM