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

2014/6/6

Need Help with this.

1
2
GreenFootieNewbie GreenFootieNewbie

2014/6/6

#
Line 16 is my code that's messed up. I'm pretty sure I have everything else in the world, key, and door class correct but I don't know why line 16 isn't working (I don't have a "nextLevel" thing in any of my other code, and need a response quickly, thanks to whoever helps :D)
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

public class Player extends Mover
{
    private static final int jumpStrength = 26;
    
    public void act() 
    {
        checkKeys();        
        checkFall();

        Key key = (Key)getOneIntersectingObject(Key.class);
        if (key != null) key.encountered();

        Door door = (Door)getOneIntersectingObject(Door.class);
        if (door != null && door.isOpened()) nextLevel();
    }

    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            //setImage("Riley.png");
            moveLeft();
        }
        if (Greenfoot.isKeyDown("right") )
        {
            // setImage("Riley001.png");
            moveRight();
        }
        if (Greenfoot.isKeyDown("space") )
        {
            if (onGround())
                jump();
        }
    }    

    private void jump()
    {
        setVSpeed(-jumpStrength);
        fall();
    }

    private void checkFall()
    {
        if (onGround()) {
            setVSpeed(0);
        }
        else {
            fall();
        }
    }
}
GreenFootieNewbie GreenFootieNewbie

2014/6/6

#
(I also have the images for it right now as a comment - don't have the images ready yet so I'm not using them at the moment)
dan11 dan11

2014/6/6

#
It is currently checking if a door is intersecting a door, is that what you want?
GreenFootieNewbie GreenFootieNewbie

2014/6/6

#
Well, right now it's a platform game, and I'm trying to make it so that once you get a key, the door will change images, and when you touch the door it will take you to the next world, and it's just that one thing in line 16 that's not working.
dan11 dan11

2014/6/6

#
Okay, get rid if (Door) in front of getOneIntersectingObject, and change Door door to Actor door
dan11 dan11

2014/6/6

#
If door.isOpened() is false it also will not work, so check that
dan11 dan11

2014/6/6

#
Sorry, the problem here is if nextLevel() does not call anything it won't work
GreenFootieNewbie GreenFootieNewbie

2014/6/6

#
I think this might help, it is all the different methods I used from the classes in one, (to show you where they are) and hopefully this will make it easier for you to help me.
// placed in the world class
Door door = new Door();
Key key = new Key(door);
// in Key the class' instance field
private Door door;
// in key class
public Key(Door linkedDoor)
{
    door = linkedDoor;
}
// with the above, the linking is complete; the rest puts it to use
// in Player class' act method
Key key = (Key)getOneIntersectingObject(Key.class);
if (key != null) key.encountered();
// in Key class
public void encountered()
{
    door.open();
    getWorld().removeObject(this);
}
// in the Door class' instance field
private boolean opened;
// the 'open' method in the door class
public void open()
{
    // change image of door
    opened = true;
}
// the 'isOpened' method in the door class
public boolean isOpened()
{
    return opened;
}
// in Player class 'act' method 
Door door = (Door)getOneIntersectingObject(Door.class);
if (door != null && door.isOpened()) nextLevel();
dan11 dan11

2014/6/6

#
Where is nextLevel() method
dan11 dan11

2014/6/6

#
And what exactly does not work? Is there an error?
GreenFootieNewbie GreenFootieNewbie

2014/6/6

#
I don't have a nextLevel() method, and it says "Cannot find symbol- method nextLevel(), and like my name suggests, I'm new at Greenfoot and could use some help with this (if you know how).
dan11 dan11

2014/6/6

#
Oh, okay. In Greenfoot, you cannot call a method that does not exist. Create a nextLevel() method and all should work.
dan11 dan11

2014/6/6

#
Basically, when you call a method, Greenfoot looks for the method to run it, and will give you an error if it can't find it. Saying nextLevel() looks for a method (void) called nextLevel() in Player.class.
GreenFootieNewbie GreenFootieNewbie

2014/6/6

#
Thanks, but would you know how to make that method, that will change my world to the next and have my player spawn at a certain spot when that world starts. Would it just be putting the player in the world, and saving the world so that right when it switches over to the next world, it will immediately start? Would that work? (what class should I put the method in - if you help me with the actual method making.) Also, if anyone knows any youtube videos or links in the Greenfoot website, that has the code and or can teach you how to have your person and any enemies have life points? Thanks.
dan11 dan11

2014/6/6

#
To change the World, use Greenfoot.setWorld(new {insert world name here}), put the method where ever you want, just be sure if Player is using it to put it in Player or call it from somewhere else In the new world, you would need to put the code to make a new Player where you want it. Examples with open-source: Here Here For Health, make an integer in the Actor you want to have health called "health" set at the starting health. Then, like some things you have already done, use a method in that Actor hit(), or attacked() for example and then have: health = health - 1; Search up some open-source games for full code examples. One with health: Here
There are more replies on the next page.
1
2