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

2013/1/16

Get location of actor

1
2
student101 student101

2013/1/21

#
Well right now this is how the worlds are set up: There's the initial beginning world. Then there's the world the player gets transported to when they collide with an enemy. Then there's the BattleReady world (named BattleWorld. You can tell I've been programming on the fly and not thinking about it in enough depth). It outputs a message to the player saying enter to continue. It then leads to the BattleFightAttack world that's in the code. All the worlds are subclasses of the beginning world btw, I just thought I should mention that in case it's important.
danpost danpost

2013/1/21

#
I get a strange feeling that you are trying to do two steps at once here. Please supply the code for your BattleReady world class.
student101 student101

2013/1/21

#
public BattleWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        
        addObject (new BattleText(), 300,187);
        addObject (new PressEnterToContinue(), 297, 228);
        
        
    }
That's pretty much it. The coding to go to the next world is in the PressEnterToContinue class.
danpost danpost

2013/1/21

#
OK, then the PressEnterToContinue class.
student101 student101

2013/1/21

#
public void PressEnter()
    {
         setImage(new GreenfootImage("Press Enter to Continue", 35, Color.WHITE, Color.BLACK));
            }
    public void act() 
    {
        PressEnter();
        CheckEnterKey();
    }    
    public void CheckEnterKey()
    {
        if (Greenfoot.isKeyDown("enter"))
        Greenfoot.setWorld(new BattleFight());
        
    }
danpost danpost

2013/1/21

#
Please show the entire PressEnterToContinue class code.
student101 student101

2013/1/21

#
There wasn't anything else to the code but okay:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color; 
/**
 * Write a description of class PressEnterToContinue here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PressEnterToContinue extends Actor
{
    /**
     * Act - do whatever the PressEnterToContinue wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void PressEnter()
    {
         setImage(new GreenfootImage("Press Enter to Continue", 35, Color.WHITE, Color.BLACK));
            }
    public void act() 
    {
        PressEnter();
        CheckEnterKey();
    }    
    public void CheckEnterKey()
    {
        if (Greenfoot.isKeyDown("enter"))
        Greenfoot.setWorld(new BattleFight());
        
    }
        
}
danpost danpost

2013/1/21

#
Had to be sure. I cleaned it up and modified it slightly. The method 'PressEnter' just sets the image of the object, but the image, once set, never changes; so that code can be placed in the constructor of the class. The 'CheckEnterKey' method is not the only thing the 'act' method would do, so I got rid of it and moved to code directly into the 'act' method (this was the code that was modified). Try it out. If your problem persists, then maybe we should be looking at the code that calls 'new BattleReady()'. Here is the code:
import greenfoot.*;
import java.awt.Color; 

public class PressEnterToContinue extends Actor
{
    public PressEnter()
    {
         setImage(new GreenfootImage("Press Enter to Continue", 35, Color.WHITE, Color.BLACK));
    }

    public void act() 
    {
        if ("enter".equals(Greenfoot.getKey()) Greenfoot.setWorld(new BattleFight());
    }
}
student101 student101

2013/1/21

#
Thank you, i think it worked. The only problem is that now it won't show the text 'Press Enter...'.
danpost danpost

2013/1/21

#
Sorry, I did not correct the name of the constructor and also had a missing paranthesis in the code. Here is the corrected (and tested; as far as I could) code.
import greenfoot.*;
import java.awt.Color; 

public class PressEnterToContinue extends Actor
{
    public PressEnterToContinue()
    {
         setImage(new GreenfootImage("Press Enter to Continue", 35, Color.WHITE, Color.BLACK));
    }

    public void act() 
    {
        if ("enter".equals(Greenfoot.getKey())) Greenfoot.setWorld(new BattleFight());
    }
}
student101 student101

2013/1/21

#
Okay it works perfectly! thank u danpost!
You need to login to post a reply.
1
2