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

2016/4/12

Trouble with getX and getY

StoveAteMe StoveAteMe

2016/4/12

#
Okay, so, here is my code:
if(Greenfoot.isKeyDown("w")) {
           addObject(new Missile(), Savior.getX(), Savior.getY());
        
    }
What I want is when every time I press W, I want to create an object (a missile) from the location of my current movable actor (Savior). I have this much so far but I can't seem to understand what I am doing incorrect. This code is in my actor class "Savior", if that helps at all.
danpost danpost

2016/4/12

#
StoveAteMe wrote...
What I want is when every time I press W, I want to create an object (a missile) from the location of my current movable actor (Savior). I have this much so far but I can't seem to understand what I am doing incorrect. This code is in my actor class "Savior", if that helps at all.
Yes -- knowing the code is in your Savior class help a bunch. You are trying to use the 'getX' and 'getY' methods on the class itself. The class is the script that you write -- not the actor(s) that are created from the class. The class has no location in your world -- the objects created from the class and placed into the world do. When a non-static method is executed, it has an object of the class as the thing that the method is executed on or for. You do not even have to reference it when calling methods on it; although, you could use the 'this' keyword to explicitly reference it:
addObject(new Missile(), getX(), getY());
// or
addObject(new Missile(), this.getX(), this.getY());
You need to login to post a reply.