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

2015/4/3

Gui Arrow Button Help!

alpensan alpensan

2015/4/3

#
Hi , Can help me for make object actor move left and right if i click my Gui arrow button ?
Super_Hippo Super_Hippo

2015/4/3

#
With what exactly do you have problems? What have you tried so far?
danpost danpost

2015/4/3

#
Please supply the code for Gui arrow button, the code for the actor object that moves (or is supposed to) left and right and any code related to the creation, saving references to, adding to the world, etc. of the Gui arrow button giving what class and method each part of these codes reside in.
alpensan alpensan

2015/4/3

#
i have bee class that is extends by class mover in class bee i connect to class button1 (for move right) and class button2 (for move left)
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();  
    }
}
but it is error can fix it?
alpensan alpensan

2015/4/3

#
danpost wrote...
Please supply the code for Gui arrow button, the code for the actor object that moves (or is supposed to) left and right and any code related to the creation, saving references to, adding to the world, etc. of the Gui arrow button giving what class and method each part of these codes reside in.
this for class mover
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.
    }    
}
danpost danpost

2015/4/3

#
alpensan wrote...
but it is error can fix it?
Looks like a lesson or two. First, lines 17 and 22 are checking for clicks on the non-existent objects of the 'button1' and 'button2 fields. I am not saying that the buttons you intend to check clicks on are not there -- I am saying that those fields do not point to those buttons. You need to set those fields to point to those buttons (assign the actors to them). Second, the 'check' method has no code in it and I doubt that you have a subclass that overrides it; so, you can remove that method. Finally, the 'walkRight' and 'walkLeft' methods are both one-line methods that just call other methods. You do not need a method to call a method! Just call the secondary method immediately (change 19 and 24 to 'moveRight();' and 'moveLeft()' and then remove the 'walkRight' and 'walkLeft' methods).
alpensan alpensan

2015/4/4

#
danpost wrote...
alpensan wrote...
but it is error can fix it?
Looks like a lesson or two. First, lines 17 and 22 are checking for clicks on the non-existent objects of the 'button1' and 'button2 fields. I am not saying that the buttons you intend to check clicks on are not there -- I am saying that those fields do not point to those buttons. You need to set those fields to point to those buttons (assign the actors to them). Second, the 'check' method has no code in it and I doubt that you have a subclass that overrides it; so, you can remove that method. Finally, the 'walkRight' and 'walkLeft' methods are both one-line methods that just call other methods. You do not need a method to call a method! Just call the secondary method immediately (change 19 and 24 to 'moveRight();' and 'moveLeft()' and then remove the 'walkRight' and 'walkLeft' methods).
i dont get it, how to set fields to point to those buttons, can you show code i have class Button1 and Button2 .......
Super_Hippo Super_Hippo

2015/4/4

#
You can't check for a click on a class. A class is never "in the world". You can check for a click on an object. You can use this to get a reference to the button:
Button1 button1 = getWorld().getObjects(Button1.class).get(0);
Note that you can't just use this in line 7 and 8, you need the object to be in the world when calling this method. The button has to be in the world as well. If you add the buttons first and then the bee, you can use this in the Bee class:
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);
}
You can do it like this. I usually check for clicks on an object in the class code of this object though.
danpost danpost

2015/4/4

#
The way you have the Bee behaving (by what code is in the act method), the bee will not move except when a button is clicked. In other words, the buttons not only tell the bee which way to go, but when to go also. The bee will not move between clicks. I do not think that is what you want (though, I could be mistaken).
alpensan alpensan

2015/4/4

#
Super_Hippo wrote...
You can't check for a click on a class. A class is never "in the world". You can check for a click on an object. You can use this to get a reference to the button:
Button1 button1 = getWorld().getObjects(Button1.class).get(0);
Note that you can't just use this in line 7 and 8, you need the object to be in the world when calling this method. The button has to be in the world as well. If you add the buttons first and then the bee, you can use this in the Bee class:
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);
}
You can do it like this. I usually check for clicks on an object in the class code of this object though.
i doint this but i got an error 'incompatible types " this for my code Class backdoor (background)
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));
        }
    }*/
}
class 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();
        }
    }    

}
Class Mover
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.
    }    
}
Class Button1 and Button2
danpost danpost

2015/4/4

#
The button objects retrieved from the world are not cast as Button1 and Button2 types -- they are returned as Object types. Use this instead:
protected void addedToWorld(World w)
{
    button1 = (Button1) w.getObjects(Button1.class).get(0);
    button2 = (Button2) w.getObjects(Button2.class).get(0);
}
You need to login to post a reply.