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
NickyL212 NickyL212

2014/1/10

#
Hi. I just started working with greenfoot today. Did some of the tutorials. But i got a question about the actors. Is there a way to limit my actors movement? It can go up down backward and forward, but i dont want it to go past a certain point. Lets say i got a super(800, 800, 1) but i want to limit its movement to 400,800. Could someone tell me how to do this? Thanks in advance :)
danpost danpost

2014/1/10

#
You should always show any code that is in question (the code for you actor, in this case). However, if you put
if (getX()>400) setLocation(400, getY());
in the act method of the class of that actor, that should do it. It basically says, if this actor tries to go past an x of 400, then hold at 400.
NickyL212 NickyL212

2014/1/11

#
Thanks got that working now the way i want it to now. But now iam struggeling with another actor of mine. Its my box actor. I cant seem to find a way or scenario where if i click on this actor, a new window appears inside the game. In this window i want to put more actors that can manipulate my 'plain' actor. Like putting a temporary shield around it etc. Just trying to give you an image of what iam trying to achieve. But it isnt going very wel at the moment. The only thing i got for my box actor is that i resized it, and gave it an location in the world. I tried things like
public void act() 
    {
      if(Greenfoot.mouseClicked(this))
      { 
          //
      }
      imgResize();
    }    
    
But i have no clue what to fill the if statement with. Could you tell me how i can let it pop up a new window inside the game by clicking on it?
danpost danpost

2014/1/11

#
You should be able to create a new Actor class for the window and just 'fill the if statement with' adding a new instance of it into the world. I am unsure as to what your 'imgResize' method does.
NickyL212 NickyL212

2014/1/11

#
I just noticed the imgResize didnt have to be put in the act method. I made it cause the original img was to big. But i now made an actor that has the image i want to use when i click on the 'box' actor. Called it 'menu'. But i dont understand with what you mean with
and just 'fill the if statement with' adding a new instance of it into the world
And should the 'menu' actor be a sub actor of my 'box' actor? or keep it on the same level box 'menu' ?
danpost danpost

2014/1/12

#
It definitely should NOT be a sub actor of your 'box' actor. If your 'box' actor extends Actor, then yes -- at the same level.
if (Greenfoot.mouseClicked(this) && getWorld().getObjects(/*name of menu class+'.class' */).isEmpty()) getWorld().addObject(/* 'new '+name of menu class+'(), '+x location+', '+y location */);
This should add a menu actor into the world if no menu actor is already in the world and the box actor is clicked on.
NickyL212 NickyL212

2014/1/12

#
Thanks for explaining that. But now i got a problem with pausing the game. The moment i want to click on my 'box' actor, the game continues. Pausing the game when i hit the 'box' actor isnt the problem, but continueing the game was a bit harder for me cause for some reason my 'menu' and 'back actor are staying on the screen. It wont compile the removeObject part. Heres what i got in my act class so far. The 'menu' actor is the screen that appears when i hit the 'box' actor and the 'back' actor is a button i use to go back to the game.
public void act() 
    {
      if(Greenfoot.mouseClicked(this)&& getWorld().getObjects(Back.class).isEmpty())
      { 
          Menu menu = new Menu();
          Back back = new Back();
          
          getWorld().addObject(new Menu(),360,360); 
          getWorld().addObject(new Back(),250,470); 
          
              if (Greenfoot.mouseClicked(this))
              {  
                Greenfoot.stop();  
              }  
              
              if (Greenfoot.mouseClicked(Back.class))
              {  
                getWorld().removeObject(Menu.class);
                getWorld().removeObject(Back.class);
                Greenfoot.start();  
              }  
      }
    } 
danpost danpost

2014/1/12

#
Lines 11 through 21 will only run if (1) a click is detected (again) on the box object and (2) there is no Back class object in the world (per your 'if' statement on line 3). Those lines need to be move elsewhere (outside the 'if' block). Also, you cannot 'stop' and 'start' the game this way. Once the game is stopped, no act methods are run and the code to detect mouse clicks and act on them is moot. If you want the game to 'pause' while the menu is present, then either use a different world (instead of an actor) for the menu or enclose all Actor act methods (except for the code you wish to run for the menu inside an 'if' block with the condition 'if (getWorld().getObjects(Menu.class).isEmpty())' and similarly for the current world class act method.
NickyL212 NickyL212

2014/1/12

#
ok let me get this straight first. Find the different uses of worlds still pretty complicated. First is that the code above is pretty much hopeless at the moment right? and second I need to place my so called 'menu' actor with my 'back' actor in another world? But can i go back to my main world after i hit the 'box' actor and continue where i left of? or does greenfoot start all over again?
danpost danpost

2014/1/12

#
I think what you mean by 'does greenfoot start all over again' is 'does the world get re-initialized'. The world is only re-initialized if you 'setWorld' to a 'new' world of that class. The current world is an object and a reference to it can be saved in a field. This reference can later be used to return to that world in exactly the same state you left it. So, this is what we need: (1) a field in the Menu world to hold the reference to the main world (2) a constructor that can except the main world as a parameter and save it in the field ref'd in (1) In the Menu world:
// instance field
private MyWorld main; // (1)
// start of constructor
public Menu(MyWorld mw) // (2a)
{
    // super...
    main = mw; // (2b)
    // rest of constructor
Then, when the box is clicked, create the Menu world with:
Greenfoot.setWorld(new Menu((MyWorld)getWorld());
I had the Menu class hold the reference as a MyWorld object to make things easier within the class. You can still work with that world by referencing it. Like if a shield was to be added to your player, you could do something like this:
((Player)main.getObjects(Player.class).get(0)).addShield();
// or if you had a get method for the player in the world
main.getPlayer().addShield();
which (hypothetically) would get the player in your MyWorld world and have the method 'addShield' run on that player. Return to the main world with 'Greenfoot.setWorld(main);'.
NickyL212 NickyL212

2014/1/12

#
Thanks for the explanation. I got an idea of how i should do it now. But i got a question abut this piece you gave me.
Then, when the box is clicked, create the Menu world with:
Greenfoot.setWorld(new Menu((MyWorld)getWorld());
The getWorld part. I keep getting an error if i try to imply it in my 'menu' world The act method in the 'menu' world looks like this so far.
public void act()   
{  
      if(Greenfoot.mouseClicked(Box.class));
      {
          Greenfoot.setWorld(new Menu((mainWorld)getWorld())); 
      }
}
danpost danpost

2014/1/12

#
The act method you posted should be in the class of the box that is clicked on and line 3 should have 'this', not 'Box.class'.
NickyL212 NickyL212

2014/1/12

#
Yea i thought you ment the method should be in the Menu world. I placed instance and constructor you gave me in my 'menu' world. Now iam trying to place the setWorld in my act of my 'box' actor. But it cant find an argument? Shouldnt the line
          if(Greenfoot.mouseClicked(this));  
          
          Greenfoot.setWorld(new Menu((MenuWorld)getWorld()));   
          
just open the MenuWorld if i pressed on the 'box' actor?
danpost danpost

2014/1/12

#
Remove the semi-colon at the end of line 1.
NickyL212 NickyL212

2014/1/12

#
Did that in the meantime. But it still gives an error in
          if(Greenfoot.mouseClicked(this))  
          {
          [b]Greenfoot.setWorld(new Menu((MenuWorld)getWorld()));[/b]
          }
          
There are more replies on the next page.
1
2
3
4