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

2017/1/5

Changing images

Hondrok Hondrok

2017/1/5

#
I want the actor to change its image when specific keys are pressed and when I press the left key it doesn't work
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; 
           setImage("Mario_idle.png");
        }
        else
        {
            setImage("Mario_jump.png");
            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());
        setImage("Mario_walk.png");
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        setImage("Mario_walk_mirror.png");
    }
     
     
}
danpost danpost

2017/1/5

#
What do you mean by "it doesn't work"? Try a different image in line 117 to make sure that the image is being set. If so, then your images are probably the same image (copies of each other). Adding "-mirror" to the name of an image does nothing to the image within the file.
Nosson1459 Nosson1459

2017/1/6

#
The CODE is fine for switching an image.
Hondrok Hondrok

2017/1/6

#
danpost wrote...
What do you mean by "it doesn't work"? Try a different image in line 117 to make sure that the image is being set. If so, then your images are probably the same image (copies of each other). Adding "-mirror" to the name of an image does nothing to the image within the file.
mirror means that the image is orientated to the left
davemib123 davemib123

2017/1/6

#
@Hondork, Danpost was referring to if the image was flipped, then it would show it as orientated to the left. If you have just added the text "_mirror" to the end of the file name then it wont work as expected. My advice would be to use something like Paint to flip the image, save it and then use it :)
Hondrok Hondrok

2017/1/6

#
davemib123 wrote...
@Hondork, Danpost was referring to if the image was flipped, then it would show it as orientated to the left. If you have just added the text "_mirror" to the end of the file name then it wont work as expected. My advice would be to use something like Paint to flip the image, save it and then use it :)
you didn't understand :)) images that include mirror have already been flipped, I added mirror just to see them easily
danpost danpost

2017/1/6

#
danpost wrote...
What do you mean by "it doesn't work"? Try a different image in line 117 to make sure that the image is being set. If so, then your images are probably the same image (copies of each other). Adding "-mirror" to the name of an image does nothing to the image within the file.
You still did not answer my first question. And, did you try a different image? if so, what happened -- did the image change?
Hondrok Hondrok

2017/1/6

#
danpost wrote...
danpost wrote...
What do you mean by "it doesn't work"? Try a different image in line 117 to make sure that the image is being set. If so, then your images are probably the same image (copies of each other). Adding "-mirror" to the name of an image does nothing to the image within the file.
You still did not answer my first question. And, did you try a different image? if so, what happened -- did the image change?
nope it didn't change
danpost danpost

2017/1/6

#
I see the problem. It is in the 'checkFall' method, which is called after the 'checkKeys' method in 'act'. Any image set in the 'checkKeys' method is overridden by an image being set in the 'checkFall' method. Replace line 48 with:
if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) setImage("Mario_idle.png");
Hondrok Hondrok

2017/1/6

#
danpost wrote...
I see the problem. It is in the 'checkFall' method, which is called after the 'checkKeys' method in 'act'. Any image set in the 'checkKeys' method is overridden by an image being set in the 'checkFall' method. Replace line 48 with:
if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) setImage("Mario_idle.png");
it's working thank youu
You need to login to post a reply.