简体   繁体   中英

Slow down enemy movement

I'm making a game using slick2d - and ive created a pathfinding algorithm, that will allow a 'zombie' to find the shortest path to the main hero.

  public void findPrey(int characterXPosition,int characterYPosition){

    Pathfinder p = new Pathfinder();
    n =  p.aStar(xPosition,yPosition,characterXPosition,characterYPosition);
    //n is a linked list which holds node objects in the path
    if(!n.isEmpty()){
        xPosition = (n.get(0).x);
        yPosition = (n.get(0).y);   
    }   
}

At the moment this works, but it just moves the zombie a tile (32x32) really quickly - like way too quickly!

I tried this instead:

        if(xPosition > n.get(0).x){
            xPosition -= .1f * delta;

        }
        else if(xPosition < n.get(0).x){
            xPosition += .1f * delta;

        }
        if(yPosition < n.get(0).y){
            yPosition += .1f * delta;

        }
        else if(yPosition > n.get(0).y){
            yPosition -= .1f * delta;

        }

Which only works if the zombie goes up or left - even then its really jumpy and if its meant to go down or right it goes mental and just keeps going up out of the screen.

So anyone any idea how I can either slow down the movement of the first technique - or know why the second one isnt working? Thanks for any help!

EDIT - ANSWER

Okay figured a solution out - was pretty basic , I think just staring at the same code for a few hours did me.

In the render method of my 'world' class where the zombies were getting rendered I just put this change in:

    Iterator<Zombie> i = zombies.iterator();
    while (i.hasNext()) {
        Zombie z = i.next();

        if (!z.isDead()) {      
            z.updateDelta(newDelta);
            z.incrementCounter(); //put a counter variable in
            if(z.getCounter() % 40 == 0){
                z.findPrey((int)shiftX,(int)shiftY);
            }

            z.render(gc, sbg, g);
        }else{
            i.remove();
        }
    }

So i basically just added a counter variable in the Zombie class and then used modular so that it wouldnt get called as frequently. Thanks for everyone for trying to help.

I suggest you use timers. Then on every computer game will work with the same speed and the movement of your units will be with fixed time.

Units move too fast, because every loop unit is moved. Amount fo loops should be less or decrease the distance moved by units.

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