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

2017/1/5

image

Hondrok Hondrok

2017/1/5

#
I want the actor to change its image when different keys are pressed and I don't know how
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    private Label myLabel;
    private int count = 10000;
     
    public Mario (Label label)
    {
        myLabel = label;
    }
     
    public void act() 
    {
       checkKeys();
       checkFall();
       hitGoomba();
       hitCastle();
    }
     
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
        {
           moveLeft();
           
        } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
           scoreCount();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
           scoreCount();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
        }
        else
        {
            fall();
        }
    }
 
    public void scoreCount()
    {
        count-=1;
        myLabel.setText("SCOR: " + count);
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
     
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
            myWorld.removeObject(goomba);
        }
    }
     
    public void hitCastle()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            World myWorld = getWorld();
            GameOver_Win gameover_win = new GameOver_Win();
            myWorld.addObject(gameover_win, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
        }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
    }
     
     
}
Super_Hippo Super_Hippo

2017/1/5

#
You can change the image with the 'setImage' method.
//for example
setImage("movingRight.png");
Hondrok Hondrok

2017/1/5

#
Super_Hippo wrote...
You can change the image with the 'setImage' method.
//for example
setImage("movingRight.png");
thank youu
You need to login to post a reply.