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

2013/5/5

AI Error

lgbrf lgbrf

2013/5/5

#
So im creating an 'AI' zombie so to speak, and one of the methods is for random movement (until he spots the character). It should toggle after running through the loop each time, but instead the 'toggle switch' stays on false, and never reverts back to true. I don't see how it logically doesn't make sense, but i might have missed something... Thanks for any help you can give me :)
public void move(){
        if (toggle = true){
            if(i < randomI){
                setLocation(getX() - 4, getY());
                i++;
            }
            else{
                i=0;
                j=0;
                randomI = Greenfoot.getRandomNumber(10) + 3;
                toggle = false;
            }
        }
        if(toggle = false){
            if(j < randomJ){
                setLocation(getX() + 4, getY());
                j++;
            }
            else{
                i=0;
                j=0;
                randomJ = Greenfoot.getRandomNumber(10) + 3;
                toggle = true;
            }
        }
danpost danpost

2013/5/5

#
First, the comparison operator is '==', not '='; so, lines 2 and 14 should be 'if (toggle == true) {' and 'if (toggle == false) {'. However, better would be 'if (toggle) {' and 'else {'.
lgbrf lgbrf

2013/5/6

#
Thanks, I knew it would be something simple..
You need to login to post a reply.