This site requires JavaScript, please enable it in your browser!
Greenfoot back
Philipplol
Philipplol wrote ...

2019/4/19

Delay

1
2
Philipplol Philipplol

2019/4/20

#
what shoud i use instead?
Super_Hippo Super_Hippo

2019/4/20

#
Create an int variable you use as a timer. Then you can "run" this timer (increase/decrease by 1 each time) and when it reaches a certain number, execute the action.
Philipplol Philipplol

2019/4/20

#
can you pleas send the code.
Philipplol Philipplol

2019/4/20

#
i made this but it doesn´t wort and i dont know why it is in akt.
 int moveDelay = 0;
public void laufen2()
        
        {
            moveDelay ++;
            if (Greenfoot.getRandomNumber(5) == 1 && !wandRechts() && moveDelay == 3)
            {
                
                
                this.setImage(down);
                this.setLocation(getX(), getY()+1);
                moveDelay = 0;
                moveDelay ++;
            }
            
            
            if (Greenfoot.getRandomNumber(5) == 2 && !wandLinks() && moveDelay == 3)
            {
                
                this.setImage(up);
                this.setLocation(getX(), getY()-1);
                moveDelay = 0;
                moveDelay ++;
            }
            
            
            if ((Greenfoot.getRandomNumber(5) == 3 && wandVorne()) && moveDelay == 3)
            {
                Greenfoot.delay(70);
                this.setImage(right);
                this.setLocation(getX()+1, getY());
                moveDelay = 0;
                moveDelay ++;
            }
           
            
            if(Greenfoot.getRandomNumber(5) == 4 && !wandHinten() && moveDelay == 3)
            {
                
                this.setImage(left);
                this.setLocation(getX()-1, getY());
                moveDelay = 0;
                moveDelay ++;
            }
            
           
        }
Super_Hippo Super_Hippo

2019/4/20

#
So you want your enemy to move in one random direction every three act cycles? I just changed that to 30, so about two steps per second.
private int moveDelay=30, moveTimer=moveDelay;

public void laufen2()
{
    if (--moveTimer==0)
    {
        moveTimer=moveDelay;
        switch (Greenfoot.getRandomNumber(4))
        {
            case 0:
            if (!wandRechts()) setLocation(getX()+1, getY());
            setImage(right);
            break;
            
            case 1:
            if (!wandLinks()) setLocation(getX()-1, getY());
            setImage(left);
            break;
            
            case 2:
            if (!wandOben()) setLocation(getX(), getY()-1);
            setImage(up);
            break;
            
            case 3:
            if (!wandUnten()) setLocation(getX(), getY()+1);
            setImage(down);
            break;
        }
    }
}
I changed the directions of the obstacle check to fit with the direction in which is moves. What I would suggest is to move in one possible direction rather than pick a random direction and move if it is possible.
You need to login to post a reply.
1
2