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/19

#
added the image in the MenuWorld
danpost danpost

2017/12/19

#
Your MenuSelector object still has no clue as to what or where your text objects are at. Use the code I provided above. Set your "rectangle.png" image as the default image for instances of the class (right click on class and click on 'Set image...', then select the image). Finally, show the code around which you create (using 'new MenuSelector()') an instance of the class. Actually, it would be best to show the whole class as other information needs to be acquired from it.
danpost danpost

2017/12/19

#
Here is the class again, with some added implementation documentation:
import greenfoot.*;
 
public class MenuSelector extends Actor
{
    Actor[] boxes; // to hold the objects to cycle among
    int onBox; // index pointer to 'boxes' array (currently highlighted actor)
    boolean rightDown; // last state of 'right' key
 
    public MenuSelector(Actor[] actors)
    {
        boxes = actors; // save actors to cycle among
    }
     
     // highlight actor pointed to in array when added into world
    protected void addedToWorld(World world)
    {
        updateLocation();
    }
     
     // highlight actor pointed to in array
    private void updateLocation()
    {
        setLocation(boxes[onBox].getX(), boxes[onBox].getY());
    }
    
    // allow cycling of highlight among actors
    public void act()
    {
        if (rightDown != Greenfoot.isKeyDown("right")) // is state of key changed
        {
            rightDown = ! rightDown; // save new state
            if (rightDown) // if new state is "pressed"
            {
                onBox = (onBox+1)%boxes.length; // pointt to next index (with wrapping)
                updateLocation(); // highlight actor now pointed at in array
            }
        }
    }
}
A new MenuSelector object can be placed anywhere in the world. I would add it at location (0 0) so the values given do not appear to be so important -- which they are not, in this case, as the object will control its own location among the text actors.
jacquelinereilly jacquelinereilly

2017/12/19

#
Thank you. So I did exactly what you did in MenuSelector. Here is the MainMenu.
public class MainMenu extends World
{

    /**
     * Constructor for objects of class MainMenu.
     * 
     */
    public MainMenu()
    {    
        // Create a new world 
        super(25*48, 14*48, 1); 
        GreenfootImage Bg = new GreenfootImage("Background.jpg");
  
        //scaling the image
        Bg.scale(getWidth(), getHeight());
        setBackground (Bg);
        
        //Adding title
        Title title = new Title ();
        addObject (title, 610, 122);
        
        //Adding text object for the menu 
        addObject (new Text ("Pokemon"), 458, 336);
        addObject (new Text ("Items"), 809, 330);
        addObject (new Text ("Map"), 654, 487);
        
        //Adding the rectangle image
        Selector select = new Selector ();
        addObject (select, 458, 336);
        
    }
   
    public void act(){
    
    if (Greenfoot.isKeyDown("enter") && Trigger.detected()) {
        WalkingWorld.WorldNumber = 1;
        WalkingWorld.CurrentWorld = 1;
        Greenfoot.setWorld(new InsideArea(50));
    
    }
    Trigger.update();
    
    }
}
jacquelinereilly jacquelinereilly

2017/12/19

#
I think the problem is in lines 27-29, because nothing is happening now
Super_Hippo Super_Hippo

2017/12/19

#
What is the Selector class? Lines 23+ should be looking like this:
Text[] textBoxes = {new Text("Pokemon"), new Text("Items"), new Text("Map")};

addObject(textBoxes[0], 458, 336);
addObject(textBoxes[1], 809, 330);
addObject(textBoxes[2], 654, 487);

addObject(new MenuSelector(textBoxes), 0, 0);
jacquelinereilly jacquelinereilly

2017/12/20

#
The Selector actor is where I have the rectangle. It is simply just to hold the picture. this is the code
public class Selector extends Actor
{
    public Selector ()
    {
        GreenfootImage image1= getImage ();
        image1.scale (image1.getWidth () -200, image1.getHeight ()-300);
        setImage (image1);
    }
jacquelinereilly jacquelinereilly

2017/12/20

#
so the image that is set to the selector actor is the rectangle that needs to be moved
Super_Hippo Super_Hippo

2017/12/20

#
Use these three lines in the MenuSelector class's constructor and remove the Selector class.
jacquelinereilly jacquelinereilly

2017/12/21

#
Got it working -thank you!
You need to login to post a reply.
1
2