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

2017/2/14

How to make every Object in my World stop if the player hits something?

ElAdriano ElAdriano

2017/2/14

#
So I´m trying to make a scroller game where the player only moves vertically and the objects(obstacles) move horizontally. But now I´m having trouble making the obstacles stop if the player is running into them. In my Player class I have:
public int vMovement()
    {
        if(Greenfoot.isKeyDown("d"))
        {
            x += 4;
        }
         if(Greenfoot.isKeyDown("a"))
        {
            x -= 4;
        }
        if(x < 0)
        {
            x = 0;
        }
        
        return x;
    }
Now I want to access that method in my GroundGrassLeft class:
public class GroundGrassLeft extends Actor
{
    int i = 0;
    int x = 0;
    int a = 4;
    int j = 0;
    public GroundGrassLeft(int j)
    {
        i = j;
    }
    public void act() 
    {
        setLocation(16 * i - 9 - x, getY());
        x = 9;
    }
    public static void getPlayerX()
    {
        List player = getObjectsInRange(1000, Player.class);
        Actor a = (Actor) player.get(0);
        x = a.
    }
}
I´m struggling in the getPlayerX() method in which I want to access the vMovement() method from the Player class. Could anyone help me?
Nosson1459 Nosson1459

2017/2/14

#
You can't set the int x be equal to a which is an actor. You have to have a field for the actor then you can use a method for getting x. (It should probably be a separate method from the actual movement)
danpost danpost

2017/2/14

#
First, if you are going to use non-static methods within the 'getPlayerX' method, then it, the 'getPlayerX' method, cannot be declared as a 'static' method (remove 'static' from line 16). Second, you cannot access the 'vMovement' method of the Player class by way of the Actor class (change both 'Actor' in line 19 to 'Player'. Finally, line 20 seems to be incomplete and should finish up as 'a.vMovement();'.
Nosson1459 Nosson1459

2017/2/14

#
danpost wrote...
First, if you are going to use non-static methods within the 'getPlayerX' method, then it, the 'getPlayerX' method,...
: )
ElAdriano ElAdriano

2017/2/14

#
Yeah it was unfinished because i couldnt autocomplete it with vMovement() But now it works perfectly. Thanks a lot :D
You need to login to post a reply.