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

2017/5/19

How to do setImage when in collision

CaiDampier CaiDampier

2017/5/19

#
I need the player image to change when in collision with car and water, please help?!
import greenfoot.*;

public class PlayerSit extends Actor
{
    public void act() 
    {
        movePlayer();
    }   

    public void movePlayer()
    {
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(),getY()-2);
            setImage("FroggerUp.png");
        }

        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(),getY()+2);
            setImage("FroggerDown.png");
        }

        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-2,getY());
            setImage("FroggerLeft.png");
        }

        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+2,getY()); 
            setImage("FroggerRight.png");
        }
    }
}
danpost danpost

2017/5/19

#
Need a little more information. Do you have two separate images (one for intersecting car and one for intersecting water). Or, do you have one? And what if intersecting both at same time? which image should take precedence (or is there a third image)?
Yehuda Yehuda

2017/5/19

#
To answer the question as is you can do this:
if (isTouching(Water.class) || isTouching(Car.class)) {
    setImage(/*The image*/); // but the image will change back when you press a key (so you have to reset the level)
}
But still:
danpost wrote...
Need a little more information. Do you have two separate images (one for intersecting car and one for intersecting water). Or, do you have one? And what if intersecting both at same time? which image should take precedence (or is there a third image)?
danpost danpost

2017/5/19

#
Yehuda wrote...
but the image will change back when you press a key
Not if the code for collision checking is after the key checking code. It will only change back when no longer intersecting one of the objects.
Yehuda Yehuda

2017/5/19

#
danpost wrote...
Yehuda wrote...
but the image will change back when you press a key
Not if the code for collision checking is after the key checking code.
But no one said where that code is going to go, but it does make sense to be after the movement code (which is when/where the collision happens).
You need to login to post a reply.