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

2015/5/9

How to move other actor when an actor is mousePressed ?

yusuf yusuf

2015/5/9

#
I want to port my game to android so i create a button so when it get pressed it will move bee object.
danpost danpost

2015/5/9

#
Have your subclass of World keep a reference to the button and the bee, and detect mouse clicks on the button in the act method.
yusuf yusuf

2015/5/10

#
How to keep a reference to the button and bee ?
danpost danpost

2015/5/10

#
yusuf wrote...
How to keep a reference to the button and bee ?
Show code of your World subclass where they are created from. (read the posting code link below the reply box first).
yusuf yusuf

2015/5/10

#
This is my world
import greenfoot.*;

public class Level1 extends World
{

    /**
     * Constructor for objects of class Level1.
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Bee bee = new Bee();
        addObject(bee, 148, 191);
        Button button = new Button();
        addObject(button, 336, 326);

    }
}
danpost danpost

2015/5/10

#
First thing is that you need to keep references to the Bee and Button objects. Move lines 24 and 26 to line 5. Then, you need an act method to check for clicks on the button:
public void act()
{
    if (Greenfoot.mouseClicked(button))
    {
        bee.move();
    }
}
Finally, change the 'act'' method in the Bee class to a 'move' method.
You need to login to post a reply.