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

2014/5/12

How does one make an actor move when another actor comes in contact with it?

Arichman Arichman

2014/5/12

#
I have an actor called "Red_Robot"(subclass of Robot which is a subclass of Game_Piece). I want an actor called "Red_Ball"(subclass of Ball which is a subclass of Game_Piece)to move when Red_Robot touches it. If anyone knows how that would work please answer. Here's the "Red_Ball" code:
public class Red_Ball extends Ball
{
    public void act() 
    {
        PushedByRed();
        PushedByBlue();
        
    } 
    
    public void PushedByRed()
    {
       if (Greenfoot.isKeyDown("w") && isTouching(Red_Robot.class))
           move(3);
       if (Greenfoot.isKeyDown("s") && isTouching(Red_Robot.class))
           move(-3);
    }
    
    public void PushedByBlue()
    {
       if (Greenfoot.isKeyDown("up") && isTouching(Blue_Robot.class))
           move(3);
       if (Greenfoot.isKeyDown("down") && isTouching(Blue_Robot.class))
           move(-3);
    }
}
Here's the code for "Red_Robot"
public class Red_Robot extends Robot
{
    public void act() 
    {
       wasdDrive();
    }    
    
    public void wasdDrive()
    {
        if (Greenfoot.isKeyDown("a"))
           turn(-3);
        if (Greenfoot.isKeyDown("d"))
            turn(3);
        if (Greenfoot.isKeyDown("w"))
            move(3);
        if (Greenfoot.isKeyDown("s"))
            move(-3);
    }
}
The Blue_Ball is identical to Red_Ball. Blue_Robot is only different in that
public void wasdDrive()
becomes
public void arrowDrive()
. And that Red_Robot drives with WASD and Blue_Robot drives with Arrow keys.
danpost danpost

2014/5/12

#
I think what you have will move the balls when the robots touch them. They are just not moving in the way you would like. I believe you want the ball to move in the same direction as the robot touching it is moving (or facing). For one example method:
public void pushedByRed()
{
    if (isTouching(Red.class))
    {
        Actor red = getOneIntersectingObject(Red.class);
        setRotation(red.getRotation());
        move(3);
    }
}
You need to login to post a reply.