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

2018/5/23

Setspeed not working

mikeliyixuan mikeliyixuan

2018/5/23

#
This is used when I touch the ice and the speed will decrease by 3, after 100 counts it will set to orinigal 43 again, but when I run the program it never set back to 43 after I touch the ice. public class ice extends Actor { public int timer=0; public boolean freeze=false; /** * Act - do whatever the ice wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { timer=0; if(isTouching(pacman.class)){ timer++; freeze=true; getWorld().removeObject(this); Greenfoot.setSpeed(40); } if(freeze=false){ Greenfoot.setSpeed(43); } if(timer>=100){ Greenfoot.setSpeed(43); freeze=false; timer=0; } // Add your action code here. }
mikeliyixuan mikeliyixuan

2018/5/23

#
public class ice extends Actor
{
    public int timer=0;
    public boolean freeze=false;
    /**
     * Act - do whatever the ice wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        timer=0;
        if(isTouching(pacman.class)){
           timer++;
            freeze=true;
            getWorld().removeObject(this);
            Greenfoot.setSpeed(40);
        }
        if(freeze=false){
           Greenfoot.setSpeed(43); 
        }
        
        
        if(timer>=100){
            Greenfoot.setSpeed(43);
            freeze=false; 
            timer=0;
        }
        
        // Add your action code here.
    }    
danpost danpost

2018/5/23

#
There are several reasons why your speed will not reset. One, if actor is not in world, timer will not run (line 15). Two, actor must touch pacman for timer to run (line 12 and 13). Three (sort of), you are not comparing, but setting, freeze to false within if condition (line 18); so, the resetting of speed there (line 19) will never execute. You will need to find another place to put your timer (a different class) just because of the first reason alone.
You need to login to post a reply.