Hi , Can help me for make object actor move left and right if i click my Gui arrow button ?
public class Bee extends Mover
{
/**
* Act - do whatever the Bee wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private Button1 button1;
private Button2 button2;
public Bee()
{
this.speed = 7;
}
public void act()
{
if(Greenfoot.mouseClicked(button1))
{
walkRight();
}
else
if(Greenfoot.mouseClicked(button2))
{
walkLeft();
}
}
public void check(){
}
public void walkRight()
{
moveRight();
}
public void walkLeft()
{
moveLeft();
}
}
import greenfoot.*;
/**
* Write a description of class Mover here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Mover extends Actor
{
/**
* Act - do whatever the Mover wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
protected int speed;
/**
* Act - do whatever the Mover wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void moveRight()
{
setLocation (getX() + speed, getY());
}
public void moveLeft()
{
setLocation (getX() - speed, getY());
}
public void act()
{
// Add your action code here.
}
}
Button1 button1 = getWorld().getObjects(Button1.class).get(0);
private Button1 button1;
private Button2 button2;
protected void addedToWorld(World w)
{
button1 = w.getObjects(Button1.class).get(0);
button2 = w.getObjects(Button2.class).get(0);
}Button1 button1 = getWorld().getObjects(Button1.class).get(0);
private Button1 button1;
private Button2 button2;
protected void addedToWorld(World w)
{
button1 = w.getObjects(Button1.class).get(0);
button2 = w.getObjects(Button2.class).get(0);
}import greenfoot.*;
/**
* Write a description of class backdoor here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class backdoor extends World
{
/**
* Constructor for objects of class backdoor.
*
*/
public Button1 button1;
public Button2 button2;
private Bee bee;
private GameState state;
private enum GameState{GAME};
private int speed=7;
public backdoor()
{
// 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.
*/
public void prepare()
{
button1 = new Button1();
addObject(button1, 169, 296);
button2 = new Button2();
addObject(button2, 448, 306);
bee = new Bee();
addObject(bee, 163, 96);
// state = GameState.GAME;
}
/*public void act()
{
switch(state)
{
case GAME:
{
actGame();
break;
}
}
}*/
/*public void actGame()
{
if(Greenfoot.mouseClicked(button1))
{
setLocation(getX(bee) + speed, getY(bee));
}
else
if(Greenfoot.mouseClicked(button2))
{
setLocation(getX(bee) - speed, getY(bee));
}
}*/
}
import greenfoot.*;
import java.util.List;
/**
* Write a description of class Bee here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bee extends Mover
{
/**
* Act - do whatever the Bee wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private Button1 button1;
private Button2 button2;
protected void addedToWorld(World w)
{
button1 = w.getObjects(Button1.class).get(0);
button2 = w.getObjects(Button2.class).get(0);
}
public Bee()
{
this.speed = 7;
}
public void act()
{
button1 = new Button1();
button2 = new Button2();
if(Greenfoot.mouseClicked(button1))
{
moveRight();
}
else
if(Greenfoot.mouseClicked(button2))
{
moveLeft();
}
}
}
import greenfoot.*;
/**
* Write a description of class Mover here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Mover extends Actor
{
/**
* Act - do whatever the Mover wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
protected int speed;
/**
* Act - do whatever the Mover wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void moveRight()
{
setLocation (getX() + speed, getY());
}
public void moveLeft()
{
setLocation (getX() - speed, getY());
}
public void act()
{
// Add your action code here.
}
}
protected void addedToWorld(World w)
{
button1 = (Button1) w.getObjects(Button1.class).get(0);
button2 = (Button2) w.getObjects(Button2.class).get(0);
}