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

#
I want an actor to get the location of itself but I'm finding trouble understanding the getX/Y() commands. What I'm trying to do create an if statement where if the actor is in a specific location and the user presses enter, a new world gets loaded. So this is what I have so far:
1
2
3
4
5
public void CheckEnter()
{
if (actor location x = value && actor location y = value && (Greenfoot.isKeyDown("enter")))
Greenfoot.setWorld(new BattleFightAttack());
}
danpost danpost

2013/1/16

#
First off, you need to understand to syntax of OOP (object-oriented programming). To run a method on an object, the sytax is
1
object.method();
Because you are trying to detect a keystroke, I would guess that the code is in the 'act' method of the actor object you are trying to get the location of (or in a method that this 'act' method calls). Also, looking in the Actor class API of the Greenfoot documentation, the methods that return the x and y locations of the actor are 'getX()' and 'getY()'. From this, the above code becomes:
1
2
3
this.getX();
// or simply (since 'this' is understood because the 'act' method is being executed on 'this' object
getX();
Another thing of note: the single equal sign '=' is used to assign values to variables; the double equals sign '==' is the used to compare the equality of two values returning a 'true'/'false' value.
MatheMagician MatheMagician

2013/1/16

#
All you have to do is replace the "actor location x" and "actor location y" with getX() and getY(). This methods act like numbers and are used in the same way. For example you can initiate a variable using them like
1
2
int x = getX()+5;
int y = getY()-5;
Your code should look like:
1
2
3
4
5
public void CheckEnter() 
if (getX() == value && getY() == value && (Greenfoot.isKeyDown("enter"))) 
Greenfoot.setWorld(new BattleFightAttack()); 
}
MatheMagician MatheMagician

2013/1/16

#
LOL, I keep posting at the same time as Danpost.
student101 student101

2013/1/16

#
Thanks for all your help guys! It worked!
vonmeth vonmeth

2013/1/16

#
MatheMagician wrote...
LOL, I keep posting at the same time as Danpost.
Hah, I have had the same thing happen to me a few times as well.
danpost danpost

2013/1/16

#
I have had that happen to me enough times that I usually refresh the page to see if anybody has posted yet, and if not, click the 'back' button and post.
MatheMagician MatheMagician

2013/1/17

#
Ahh! Thanks for the tip danpost.
danpost danpost

2013/1/17

#
@MatheMagician, I forgot to mention to cut and paste the posting text in the process (that is, create your text, cut or copy the text, refresh the screen, check for new posts; if none, paste the text and post). Paging back is not neccessary.
student101 student101

2013/1/21

#
Is there a way I can add a timer to the code so that the player has to wait for a second or two before pressing enter? Because right now, if I press enter a split second too long the game loads the other scene instead of the one I want. So basically:
1
2
3
4
5
6
public void CheckEnter()   
    {   
        if (getX() == 41 && getY() == 332 && (Greenfoot.isKeyDown("enter")))   
        User waits for X seconds;
        Greenfoot.setWorld(new BattleFightAttack());   
    }
danpost danpost

2013/1/21

#
Instead of using 'isKeyDown' to trigger the scene change, use the 'geKey' method, which is triggered on the release of the key.
student101 student101

2013/1/21

#
The problem of it skipping a scene still happens danpost, even with the new code you gave me.
danpost danpost

2013/1/21

#
Then the problem is in the BattleFightAttack world (not the 'enter' key input).
student101 student101

2013/1/21

#
Well I don't see the problem in that specific world. All it currently does is add objects to the world.
danpost danpost

2013/1/21

#
Well, the scenario is still running, which means all your 'act' methods for all the actors you placed in that world, plus the world 'act' method will all be running. What trigger are you using to proceed from BattleFightAttack to the next world? Are the conditions correctly stated to proceed to the next world? Are the values of all the variables used in those conditions being maintained properly?
There are more replies on the next page.
1
2