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

2013/1/31

Getting location of an actor from an actor

1
2
3
danpost danpost

2013/2/3

#
In your AnswerCorrectWorld world class code, the 'if' statement (lines 23 through 26) should not be in the constructor of the world, but in an 'act' method for the world. In your BattleArrow actor class, you need to get the value of 'gameWorld' from the currently running BattleWorld world. To get that value, use;
BattleWorld bw=(BattleWorld)getWorld();
World gw=bw.gameWorld;
This is the world that you need to pass along. BTW, I am assuming that BattleArrow objects are only instantiated from within the BattleWorld world and never used in any other.
student101 student101

2013/2/3

#
The BattleArrow object is in the BattleFightAttack world so I adjusted your code accordingly. But I've been racking my brain trying to understand what should be going on, what needs to be here and there. I'm getting close to just creating duplicate but slightly different worlds and using the normal Greenfoot.setWorld command to shift to them because this is stressing me out. I'm not sure I'm doing what you're telling me to do right.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class AnswerCorrectWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AnswerCorrectWorld extends World
{
    World gameWorld;
    /**
     * Constructor for objects of class AnswerCorrectWorld.
     * 
     */
    public AnswerCorrectWorld(World world)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        gameWorld = world;
        addObject (new PressEnterToContinueCorrectAnswer(), 306, 230);
        prepare();
         
        }
    
    public void act()
    {
       if ("enter".equals(Greenfoot.getKey())) 
        {
         Greenfoot.setWorld(gameWorld);   
    }
}
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        CorrectAnswerWorldText correctanswerworldtext = new CorrectAnswerWorldText();
        addObject(correctanswerworldtext, 301, 199);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class BattleArrow here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BattleArrow extends Arrow
{
    /**
     * Act - do whatever the BattleArrow wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        BattleFight bf=(BattleFight)getWorld();  
        World gw=bf.gameWorld; 
        BattleKeys();
        CheckEnterBattle();
        
    }
    
     public void CheckEnterBattle()    
    {    
         if (getX()==41 && getY()==332 && "enter".equals(Greenfoot.getKey())) 
         { 
          World nextWorld = new AnswerCorrectWorld(/** still have no idea what should go in here */); 
           Greenfoot.setWorld(nextWorld);
        }
    }        
}
danpost danpost

2013/2/3

#
In the BattleArrow class, cut and paste lines 17 through 18 to before line 28. And put 'gw' as the missing argument.
student101 student101

2013/2/3

#
Thank you danpost. The code compiles! I noticed I've put the codes in the wrong places though and I've adjusted them accordingly. You see, there is an AnswerCorrectWorld the player gets taken to once they've chosen a correct answer. They then press enter and that's when they get taken to back to where they were initially. The thing is that the state of the world when they left it still isn't saved. The actor that leads to the original world (GameWorld):
import greenfoot.*; 
import java.awt.Color;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PressEnterToContinueCorrectAnswer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PressEnterToContinueCorrectAnswer extends PressEnterToContinue
{
   
    /**
     * Act - do whatever the PressEnterToContinueCorrectAnswer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Press Enter to Continue", 35, Color.WHITE, Color.BLACK));
        
       if ("enter".equals(Greenfoot.getKey())) 
       {
           AnswerCorrectWorld aca=(AnswerCorrectWorld)getWorld();  
           World gw=aca.gameWorld;
           World nextWorld = new GameWorld(gw); 
           Greenfoot.setWorld(nextWorld);
       }
        
        } 
        
    }    

GameWorld:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class GameWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GameWorld extends World
{
    
    World gameWorld;
    /**
     * Constructor for objects of class GameWorld.
     * 
     */
    public GameWorld(World world)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        
        super(600, 400, 1); 
        setBackground("sand.jpg");
        
        prepare();
        
        
        
        
    }

    
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Shrubs shrubs = new Shrubs();
        addObject(shrubs, 498, 93);
        Shrubs shrubs2 = new Shrubs();
        addObject(shrubs2, 479, 276);
        Shrubs shrubs3 = new Shrubs();
        addObject(shrubs3, 296, 356);
        Shrubs shrubs4 = new Shrubs();
        addObject(shrubs4, 316, 42);
        Mouse mouse = new Mouse();
        addObject(mouse, 128, 188);
        Snake snake = new Snake();
        addObject(snake, 427, 192);
        snake.setLocation(388, 209);
        snake.setLocation(391, 209);
       
       
        
    }
}
danpost danpost

2013/2/3

#
In your PressEnterToContinueCorrectAnswer class, you are creating a new GameWorld world and sending it the original GameWorld world to it in the argument. This is not what you want to do. You just want to reset the world back to the original. Remove line 25 and change line 26 to 'Greenfoot.setWorld(gw);'. Also, in the GameWorld class code remove the 'World gameWorld' field and the argument in its constructor.
student101 student101

2013/2/3

#
Ah yes, sorry about that. It's all good until I press enter to go back to GameWorld. An error pops up saying that the given world cannot be null?
danpost danpost

2013/2/4

#
Follow the path of the gameWorld world through the active worlds . Make sure it is not being replaced or nullified along the way.
student101 student101

2013/2/4

#
Danpost I've been studying your crayon babies game again and I've realised how in each of those worlds, have an inWorld variable that saves the world you want to return to? Perhaps my game needs that? Because I can't tell if GameWorld is being replaced or nullified. I can tell you that my other worlds don't constantly reference GameWorld, only AnswerCorrectWorld does, so perhaps that's why the null error pops up?
danpost danpost

2013/2/4

#
danpost wrote...
public class BattleWorld extends World
{
    World gameWorld;

    public BattleWorld(World world)
    {
        super(....)// whatever
        gameWorld=world;
        // etc.
//  pass it the same way to next world

// when returning to main world
Greenfoot.setWorld(gameWorld);
I had supplied this yesterday. Line 10 was trying to tell you that the world saved in 'gameWorld' needs to be passed along from world to world until you finally return to it by using the code on line 13.
You need to login to post a reply.
1
2
3