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

2014/4/10

how to change a image when turning

nc-munk nc-munk

2014/4/10

#
I need help with a school project I want to change the image to at turning ship when I press right here is my code import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * A simple SpaceShip that can move up and down. * * @author Poul Henriksen */ public class SpaceShip extends Actor { private GreenfootImage spaceship = new GreenfootImage("Spaceship.png"); private GreenfootImage spaceshipRight = new GreenfootImage("spaceship right.png"); public void checkKeys() { if (Greenfoot.isKeyDown("right") ) { setLocation (getX() + 2, getY()); } if (Greenfoot.isKeyDown("left") ) { setLocation (getX() - 2, getY()); } } public void turn( boolean turn ) { if (Greenfoot.isKeyDown("right")) { setImage (spaceshipRight); } if (!Greenfoot.isKeyDown("right")) { setImage (spaceship); } } }
Zamoht Zamoht

2014/4/10

#
public void checkKeys() { if (Greenfoot.isKeyDown("right") ) { setLocation (getX() + 2, getY()); setImage(spaceshipRight); } if (Greenfoot.isKeyDown("left") ) { setLocation (getX() - 2, getY()); setImage(spaceship); } } You don't need the turn method. Else if you really want the turn method you have to call it.
MatheMagician MatheMagician

2014/4/10

#
You have to call those methods in the act cycle(): add this method.
public void act()
{
       turn(true);
       checkKeys();
}
I am not sure why you call that boolean: you do not use it at all.
nc-munk nc-munk

2014/4/11

#
Thanks. by I only want have the image changed as long the right is pressed and the same when left is pressed code import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class SpaceShip extends Actor { private GreenfootImage spaceship = new GreenfootImage("Spaceship.png"); private GreenfootImage spaceshipRight = new GreenfootImage("spaceship right.png"); private GreenfootImage spaceshipLeft = new GreenfootImage("spaceship left.png"); public void checkKeys() { if (Greenfoot.isKeyDown("right") ) { setLocation (getX() + 2, getY()); setImage(spaceshipRight); } if (Greenfoot.isKeyDown("left") ) { setLocation (getX() - 2, getY()); setImage(spaceshipLeft } } }
nc-munk nc-munk

2014/4/11

#
and with this code my ship wont move anymore
Zamoht Zamoht

2014/4/11

#
Remember to put checkKeys() in the act method. Btw try to change the method a little:
    public void checkKeys() 
    {
        setImage(spaceship);
        if (Greenfoot.isKeyDown("right") && !Greenfoot.isKeyDown("left")) 
        {
            setLocation (getX() + 2, getY());
            setImage(spaceshipRight);
        }
        if (Greenfoot.isKeyDown("left") && !Greenfoot.isKeyDown("right")) 
        {
            setLocation (getX() - 2, getY());
            setImage(spaceshipLeft);
        }
    }
nc-munk nc-munk

2014/4/11

#
Thanks that helped alot. but I cant get the image to change back to the original image them the key are not pressed anymore
kasperk22 kasperk22

2014/4/11

#
Hey
danpost danpost

2014/4/11

#
The following should work. However, setting the image every act cycles is not recommended (working with images takes CPU time and by setting the image every act, it becomes a hog). The way to avoid (or minimize) this is to add an instance field for the last dx value (call it 'dxHold') and set it to 'dx' at the end of the method. Only if the new 'dx' value is not the same as the 'dxHold' value should the image be set (or, rather, changed).
public void checkKeys()
{
    GreenfootImage[] images = { spaceshipLeft, spaceship, spaceshipRight };
    int dx = 0;
    if (Greenfoot.isKeyDown("right")) dx++: 
    if (Greenfoot.isKeyDown("left")) dx--;
    setLocation (getX() - 2*dx, getY());
    setImage(images[dx+1]);
}
Super_Hippo Super_Hippo

2014/4/11

#
I think in line 7, it should be + instead of -.
danpost danpost

2014/4/11

#
Super_Hippo wrote...
I think in line 7, it should be + instead of -.
You are so correct. I missed that when copy/pasting original code post and editing. Thanks.
You need to login to post a reply.