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

2017/12/18

How to make this rectangle move

1
2
jacquelinereilly jacquelinereilly

2017/12/18

#
I am trying to get the rectangle I drew to move to the right when the right arrow key is pressed. This is the code so far:
public class MenuSelector extends Actor
{
    /**
     * Act - do whatever the MenuSelector wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    //Creating instance of a new image to change it 
    GreenfootImage rightBox = new GreenfootImage ("ThisScreenBox.jpg");
    
    public void act() 
    {
       if (Greenfoot.isKeyDown ("right")) 
       {
           setImage(rightBox);
    }    
}
}
danpost danpost

2017/12/18

#
The actor is not moving and once the image of the actor is set to 'rightBox', nothing more will happen (continually setting the image of the actor to the same image will not make the actor (or the image) move. It would help if you gave an idea of what the menu selector is, what exactly it is for, and how it is supposed to behave. It may be exactly what its name implies, but details are needed for extra help (instead of getting just a general explanation of what is currently happening).
jacquelinereilly jacquelinereilly

2017/12/19

#
So the menu screen has three text boxes on it. They are text boxes so we can change what they say. I have drawn a rectangle on the first box, and I am hoping for the rectangle to move when the right arrow key is pressed, so that it will highlight the users choice on the menu. Then they press enter to go into that option
jacquelinereilly jacquelinereilly

2017/12/19

#
So yes I need to make the rectangle move, haha.
danpost danpost

2017/12/19

#
jacquelinereilly wrote...
So yes I need to make the rectangle move, haha.
Are you good, then? Are you still in need of help? Can you get the rectangle to cycle between the three text boxes?
jacquelinereilly jacquelinereilly

2017/12/19

#
The cycling between the boxes is where I am stuck. How do I get the rectangle to move? Thank you
danpost danpost

2017/12/19

#
The MenuSelector object will need to know what actors to cycle between (in the order it is to cycle among them). Maybe the text boxes should be passed to it when it is created. A boolean field will also be needed to track the state of the "right" key so that the cycling can be controlled (you do not want it to continuously jump among the boxes while the key is in the down state which 'isKeyDown' will give a true value for). So, maybe:
import greenfoot.*;

public class MenuSelector extends Actor
{
    Actor[] boxes;
    int onBox;
    boolean rightDown;

    public MenuSelector(Actor[] actors)
    {
        boxes = actors;
    }
    
    protected void addedToWorld(World world)
    {
        updateLocation();
    }
    
    private void updateLocation()
    {
        setLocation(boxes[onBox].getX(), boxes[onBox].getY());
    }
    
    public void act()
    {
        if (rightDown != Greenfoot.isKeyDown("right"))
        {
            rightDown = ! rightDown;
            if (rightDown)
            {
                onBox = (onBox+1)%boxes.length;
                updateLocation();
            }
        }
    }
}
jacquelinereilly jacquelinereilly

2017/12/19

#
Thank you. I am just a bit confused - the rectangle still doesn't move
danpost danpost

2017/12/19

#
jacquelinereilly wrote...
Thank you. I am just a bit confused - the rectangle still doesn't move
Show how you create a MenuSelector object and give relevant code around it.
jacquelinereilly jacquelinereilly

2017/12/19

#
I have just drawn the rectangle on the main menu and then created the MenuSelector object with the code above. Am I missing something
danpost danpost

2017/12/19

#
danpost wrote...
The rectangle should be on an image set to the MenuSelector object.
The rectangle should be on an image set to the MenuSelector object.
jacquelinereilly jacquelinereilly

2017/12/19

#
Oh, yes. So I made a few changes and instead of drawing a rectangle; I just added an image of a rectangle. I just don't understand why it is != in the code you provided. So I changed it to what I had before and made it an image. So now an image is added of a rectangle, but it is not moving still
danpost danpost

2017/12/19

#
jacquelinereilly wrote...
I changed it to what I had before and made it an image. So now an image is added of a rectangle, but it is not moving still
Show your revised code.
jacquelinereilly jacquelinereilly

2017/12/19

#
this is the MenuSelector
public class MenuSelector extends Actor
{
    /**
     * Act - do whatever the MenuSelector wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    //Creating instance of a new image to change it 
    GreenfootImage rectangle = new GreenfootImage ("rectangle.png");

    public void act() 
    {
        if (Greenfoot.isKeyDown ("right")) 
       {
           setImage (rectangle);
           setLocation (getX(), getY() );

        }   
    }
}
jacquelinereilly jacquelinereilly

2017/12/19

#
this is where I added the image
//Adding the rectangle image
        Selector select = new Selector ();
        addObject (select, 458, 336);
There are more replies on the next page.
1
2