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

2016/1/21

How to let an actor stop moving

SuperPandaMario SuperPandaMario

2016/1/21

#
How does one make an actor stop moving? Below is my code, the int "richting" is the movement of the actor. It is a fps game and when the actor is shot, it should stop moving, which doesn't work.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Human1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Human1 extends Animal
{
    /**
     * Act - do whatever the Human1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int deaths=0;
    public int i=15;
    public int richting=4;
    
    public void act() 
    {
        die();
        move(richting);
        beweeg();
        i=i+1;
    }    
    public void beweeg()
    {
        
        
        if (canSee(Wall.class))
        { 
            richting=richting*-1;
            
           
        }
        
        if (canSee(Bullet.class) && deaths>=1)
        {
           int richting =0;
            
     
        }
       
    }
    public void die() 
    {
        if (canSee(Bullet.class) && deaths==0 && i>15)
       {
           GreenfootImage image=new GreenfootImage("Body.png");
           setImage(image);
           setLocation (getX(), getY()+30);
           
          deaths=deaths+1;
          i=0;
         
          
        }
        
        if (canSee(Bullet.class) && deaths==1 && i>15)
       {
            GreenfootImage image=new GreenfootImage("Body2.png");
           setImage(image);
          deaths=deaths+1;
          i=0;
        }
        
        if (canSee(Bullet.class) && deaths==2 && i>15)
       {
            GreenfootImage image=new GreenfootImage("Body3.png");
           setImage(image);
          deaths=deaths+1;
          i=0;
        }
        
        if (canSee(Bullet.class) && deaths==3 && i>15)
       {
            GreenfootImage image=new GreenfootImage("Body4.png");
           setImage(image);
          deaths=deaths+1;
          i=0;
        }
        
        if (canSee(Bullet.class) && deaths==4 && i>15)
       {
            GreenfootImage image=new GreenfootImage("Body5.png");
           setImage(image);
          deaths=deaths+1;
          i=0;
        }
        
    }    
}
danpost danpost

2016/1/22

#
Lines 37 through 42 do absolutely nothing at all -- even if the conditions defined on line 37 were both true. This is because line 39 is declaring a local variable that in no way relates to the field declared on line 17. Remove the 'int' from line 39 so that you are referring to the already defined field declared on line 17.
You need to login to post a reply.