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

2014/1/10

limit my actors movement

1
2
3
4
5
danpost danpost

2014/1/12

#
Please answer these: (1) what is the class name of the world that the 'box' object is in? (2) the 'box' object is in your game world, correct? (3) what is the class name of the world you will be using for the menu? (4) did you remove your Actor subclass called 'Menu'?
NickyL212 NickyL212

2014/1/12

#
1,2)The 'box' is an actor. Its part of my main world and if i click on it, i want to open my 'menu' world. 3) menuWorld 4) No, because my Menu actor is different from my menuWorld. My menuWorld holds the same background as my mainWorld, but i want to add the image of my Menu actor in my menuWorld.
danpost danpost

2014/1/12

#
Then:
if (Greenfoot.mouseClicked(this))
{
    Greenfoot.setWorld(new menuWorld((mainWorld)getWorld()));
}
NickyL212 NickyL212

2014/1/12

#
I see. Ok got that going for me now. I placed several actors in this menuWorld of mine. Like the 'Menu' and the 'return' actor. Can i let the 'Back' actor get me back 2 my game? cause Greenfoot.start isnt the right way to do this. Its still pretty plain in this world though.
public class MenuWorld extends World
{
        
  
    
    /**
     * Constructor for objects of class MenuWorld.
     * 
     */
    public MenuWorld(mainWorld)
    {    
       
        
        super(800, 800, 5); 
     
        addMenu();
        addBack();
    }
    
    private void addMenu()
    {
        Menu Menu = new Menu();
        addObject(Menu, 380, 380);
    }
    
    private void addBack()
    {
        Back Back = new Back();
        addObject(Back, 230, 520);

    }
    
}
danpost danpost

2014/1/13

#
First, you do not have the field to hold your mainWorld object in or the line in your constructor to set that field with:
import greenfoot.*;

public class MenuWorld extends World
{
    public MainWorld mainWorld; // instance field to hold mainWorld object
  
    public MenuWorld(world)
    {    
        super(800, 800, 5); 
        mainWorld = world; // sets value of 'mainWorld' field to mainWorld object
        addMenu();
        addBack();
    }
    
    private void addMenu()
    {
        Menu Menu = new Menu();
        addObject(Menu, 380, 380);
    }
    
    private void addBack()
    {
        Back Back = new Back();
        addObject(Back, 230, 520);

    }
}
Now: in your Back class act method put the code where if 'this' is clicked on set the world back to the mainWorld object saved in the 'mainWorld' field of the MenuWorld object. The Back class act method:
public void act()
{
    if (Greenfoot.mouseClicked(this)) Greenfoot.setWorld((MenuWorld)getWorld()).mainWorld);
}
NickyL212 NickyL212

2014/1/13

#
While i was trying to compile some code, i suddenly got an error from where i didnt even touched the code as far as i know. I tried to trace back where it went wrong and went back to about the start but it still gives an error? I think i backspaced or used undo 1 or 2 times to much. Could you tell me where i went wrong here (again)? Its the box actor again.
public void act() 
    {
      if(Greenfoot.mouseClicked(this))
      {
          
           Greenfoot.setWorld(new menuWorld((mainWorld)getWorld()));  
         
      }
     
    }  
danpost danpost

2014/1/13

#
Any time you ask about something where you are getting an error message, please copy/paste the entire message into your post. The only thing that could possibly be creating an error in the act method you supplied above is the name of one of your world classes. The fact that you are not capitalizing your class names makes it a bit confusing as to what is a field, what is a class name and what is just you saying what it is. It is standard procedure to use uppercase letters to start class names and lowercase letters to start field names. As far as I know 'menuWorld extends World' and 'mainWorld extends World' and 'Box extends Actor' and 'Menu extends Actor'; but I could be mistaken.
NickyL212 NickyL212

2014/1/13

#
Ok. I changed all my World and Actors to names starting with a capital from now on. i got 2 world classes so far. MenuWorld and MainWorld. My actors are Box Back and Menu. I got confused and couldnt see it myself aswell. Organized it a bit and changed the fieldnames that started with a capital to lowercase.
    import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Box here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Box extends Actor
{
    /**
     * Act - do whatever the Upgrade wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      if(Greenfoot.mouseClicked(this))
      {
          
           Greenfoot.setWorld(new MenuWorld((MainWorld)getWorld()));  
         
      }
      imgResize();
    }    
    
    public void imgResize()  
    {      
        GreenfootImage image = getImage();    
        image.scale(120, 100);  
        setImage(image);  
    }
}
The reason it wont compile is : Actual and formal argument lists differ in length.
danpost danpost

2014/1/13

#
Then you need to show your MenuWorld class code...at least the constructor(s).
NickyL212 NickyL212

2014/1/13

#
Update the code to show the whole actor class. This is my MenuWorld code so far.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MenuWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MenuWorld extends World
{
        
  
    public MainWorld mainWorld;
    /**
     * Constructor for objects of class MenuWorld.
     * 
     */
    public MenuWorld(world)
    {    
        
        
        super(800, 800, 5);
        MainWorld = world; 
        addMenu();
        addBack();
    }
    
    private void addMenu()
    {
        Menu Menu = new Menu();
        addObject(Menu, 380, 380);
    }
    
    private void addBack()
    {
        Back Back = new Back();
        addObject(Back, 230, 520);

    }
    
}
danpost danpost

2014/1/13

#
Line 18 should be
public MenuWorld(MainWorld world)
In a method or constructor declaration statement, all arguments must be given a type. Line 23 should be
mainWorld = world;
You cannot set a value to the class name, only to the field.
danpost danpost

2014/1/13

#
Although it works with the method call on lines 24 and 25 and the methods 'addMenu' and 'addBack', I would not stoop to creating methods that are only executed one time (unless they are extensive method that required a lot of debugging. I would replace everything from line 24 on with this
// remove this line (used to set indent on next line of code)
        addObject(new Menu(), 380, 380);
        addObject(new Back(), 230, 520);
    }
}
NickyL212 NickyL212

2014/1/13

#
Didnt knew it could be placed like that aswell. Makes it allot more clear though. Then i got 1 more question about the this statement to return to the 'MainWorld' once i entered the 'MenuWorld'.
public class Back extends Actor
{
    /**
     * Act - do whatever the Back wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public void act()  
    {  
        if (Greenfoot.mouseClicked(this)) 
        {
                  Greenfoot.setWorld((MenuWorld)getWorld().MainWorld);  
        }
    }  

}
Just why wont this work?
danpost danpost

2014/1/13

#
You are trying to return to a class, not to a world. The field is named 'mainWorld' (with a small 'm'); and its value (or what it is set to refer to) is the MainWorld object.
NickyL212 NickyL212

2014/1/13

#
Even if i tried to refer back to the field 'mainWorld', it keeps telling me it isnt a statement. But i used the if correctly right? Here the updated MenuWorld
public class MenuWorld extends World
{
        
  
    public Mainworld mainworld;
    /**
     * Constructor for objects of class MenuWorld.
     * 
     */
    public MenuWorld(MainWorld world) 
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        
        super(760, 760, 1);
        mainWorld = World; 
        addObject(new Menu(), 380, 380);  
        addObject(new Back(), 230, 520);  
    }  
} 
There are more replies on the next page.
1
2
3
4
5