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

2021/2/5

Retry button for pause menu

1
2
BogdanMicu BogdanMicu

2021/2/5

#
Hi! How can I access a property of my world in one of my actors. I'm trying to make a Retry button and I want to check for each level if it's active through a boolean "active". Here's what I tried so far:
public class retryButton extends Buttons
{
    public void act() 
    {
        if(Lume.isActive())Greenfoot.setWorld(new Lume());
    }    
}
Lume is the first level and it has a method isActive which returns whether it's active or not.
public boolean isActive()
    {
        return active;
    }
BogdanMicu BogdanMicu

2021/2/5

#
Also, not related to the first question. I'm programming a platformer and I've got this movement:
public void act()
{
    int dx=0;
    
    if (Greenfoot.isKeyDown("d")) 
    {
        dx+=4;
        animationCounter++;
        mersAnm();
        check = 1;
    }
    if (Greenfoot.isKeyDown("a"))
    {
        dx-=4; 
        animationCounter++;
        mersInvAnm();
        check = 2;
    }
    if(dx==0) idleAnm();
    if (Greenfoot.isKeyDown("space") && onGround) 
    {
        animationCounter++;
        jump();
    }
    setLocation(getX()+dx, getY());
    if (isTouching(Ground.class)) setLocation(getX()-dx, getY()); 
    if (!onGround)fall();
}
public void jump()
{
    vSpeed = -11;
    onGround = false;
}
 
public void fall()
{
    setLocation(getX(), getY()+vSpeed);
    if (isTouching(Ground.class))
    {
        setLocation(getX(), getY()-vSpeed);
        onGround = true;
    }
    else vSpeed += acceleration;
}
How can I check if the actor is ON one of the objects belonging to the ground class, not necessarily touching it. (the actor starts floating when going off a platform or touching it's side). I tried to replace the condition to start the fall function with .isTouching(Ground.class), but then the movement looks very buggy and chunky and it also doesn't solve the floating when touching one of the sides of the platform.
danpost danpost

2021/2/5

#
BogdanMicu wrote...
Hi! How can I access a property of my world in one of my actors. I'm trying to make a Retry button and I want to check for each level if it's active through a boolean "active".
public boolean isActive()
If you place this method in all your levels, or any world for that matter, I fail to see when it would ever be false.
if(Lume.isActive())
Lume is a class name ... and is NOT a reference to any particular world. Use:
if(getWorld() instanceof Lume)
danpost danpost

2021/2/5

#
BogdanMicu wrote...
Also, not related to the first question. I'm programming a platformer and I've got this movement: << Code Omitted >>
Please refer to the movement code in the Who class of my Jump and Run Demo w/Moving Platform scenario.
BogdanMicu BogdanMicu

2021/2/6

#
The movement in your game is exactly what I'm trying to reproduce, but when I implemented it the whole program froze after 1 or 2 seconds of movement of the character. I think it gets stuck in a loop somewhere, but I can't figure out where. I checked in comparison with your code and I think I didn't miss anything. Here's how I used it:
static final int gravitate = 2;
static final int hop = 30;
public int animationCounter = 0;
public int check = 1;
public int speedX = 4;
public int speedY = 0;
public void act()
{
    orizontala();
    verticala();
}

public void orizontala()
{
    int worldWidth = getWorld().getWidth();
    int myWidth = getImage().getWidth();
    int dx=0;
    if (Greenfoot.isKeyDown("d")) 
    {
        dx++;
        animationCounter++;
        mersAnm();
        check = 1;
    }
    if (Greenfoot.isKeyDown("a"))
    {
        dx--; 
        animationCounter++;
        mersInvAnm();
        check = 2;
    }
    if(dx==0) idleAnm();
    setLocation(getX()+dx*speedX, getY());
    if(getX()<myWidth/2)setLocation(myWidth/2,getY());
    if(getX()>worldWidth-myWidth/2)setLocation(worldWidth-myWidth/2, getY());
    while(getOneIntersectingObject(null)!=null)setLocation(getX()-dx,getY());
}

public void verticala()
{
    int worldHeight=getWorld().getHeight();
    int myHeight=getImage().getHeight();
    boolean onGround = false;
    speedY+=gravitate;
    setLocation(getX(), getY()+speedY);
    if(getY() > worldHeight-myHeight/2)
    {
        setLocation(getX(), worldHeight-myHeight/2);
        speedY=0;
        onGround = true;
    }
    int dy = (int) Math.signum(speedY);
    while(getOneIntersectingObject(null)!=null)
    {
        setLocation(getX(), getY()-dy);
        if(dy>0)onGround = true;
        speedY=0;
    }
    if(onGround&&Greenfoot.isKeyDown("space"))speedY=-hop;
}
BogdanMicu BogdanMicu

2021/2/6

#
danpost wrote...
BogdanMicu wrote...
Hi! How can I access a property of my world in one of my actors. I'm trying to make a Retry button and I want to check for each level if it's active through a boolean "active".
public boolean isActive()
If you place this method in all your levels, or any world for that matter, I fail to see when it would ever be false.
if(Lume.isActive())
Lume is a class name ... and is NOT a reference to any particular world. Use:
if(getWorld() instanceof Lume)
The retry button is placed in the pauseMenu, which is another world. When you press 'esc' it loads that world. The problem was: how do I know which world to load after clicking the retry button. I chose to create an active variable to each level, which turns to true only after esc is pressed, but before it switches to the other world so that in the retry button I can check each world and choose the one which has the active variable true. After it identifies the world, it sets the active variable back to false and loads that world. The actual problem was how do I access it? To my knowledge, you can't get a variable straight out of another world, so I created a method which returns it. I understood that you can't get it straight out of the class. My logic process on that was that each separate level has it's own class, that's why I tried to reference Lume directly. The getWorld() will reference to pauseMenu, not to the world lume, which won't exist if I switch out of it (I think so at least). So, how can I access that variable, or if I can't access it, how can I implement this retry button?
danpost danpost

2021/2/6

#
BogdanMicu wrote...
The movement in your game is exactly what I'm trying to reproduce, but when I implemented it the whole program froze after 1 or 2 seconds of movement of the character.
Line 32 should be (not an issue fix, however):
if (dx == 0) { idleAnm(); return; }
For issue, is the player intersecting another actor when it freezes?
BogdanMicu BogdanMicu

2021/2/6

#
Yea, the Ground is set as an actor. It is a purple rectangle covering the lower quarter of the screen.
danpost danpost

2021/2/6

#
BogdanMicu wrote...
The retry button is placed in the pauseMenu, which is another world
The add a World parameter to the pauseMenu constructor and then pass the active world to it and save it in a World field. So, instead of getWorld(), you would use the name of the field.
danpost danpost

2021/2/6

#
BogdanMicu wrote...
Yea, the Ground is set as an actor. It is a purple rectangle covering the lower quarter of the screen.
Make sure your player is placed into the world (just) above the ground.
danpost danpost

2021/2/6

#
BogdanMicu wrote...
I thought of using the Ground class for all platforms and such.
Not sure how wise that would be. Maybe have Ground and Platform extend a common Surface class?
BogdanMicu BogdanMicu

2021/2/6

#
The player is placed 10 pixels above the ground actor and it still freezes.
BogdanMicu BogdanMicu

2021/2/6

#
danpost wrote...
BogdanMicu wrote...
The retry button is placed in the pauseMenu, which is another world
The add a World parameter to the pauseMenu constructor and then pass the active world to it and save it in a World field. So, instead of getWorld(), you would use the name of the field.
Ok, but how do I access that method. With Lume.isActive() it gave off this error: non-static method isActive() cannot be referenced from a static context. Also, do I save the World parameter as a string or should it have a special format?
danpost danpost

2021/2/6

#
BogdanMicu wrote...
how do I access that method. With Lume.isActive() it gave off this error: non-static method isActive() cannot be referenced from a static context.
The isActive method is not needed.
Also, do I save the World parameter as a string or should it have a special format?
Save it as a World object.
BogdanMicu BogdanMicu

2021/2/6

#
Yes, but the initial question still lies in: How do I figure out which level was active before the world switched? If not with isActive(), then how? This was my best try taking into consideration everything you said, but using the isActive().
public class retryButton extends Buttons
{
    public void act() 
    {
        Lume world = new Lume();
        if(world.isActive() && Greenfoot.mouseClicked(this))Greenfoot.setWorld(new Lume());
    }    
}
I know it doesn't work because the world it initiates is a new one, not the actual level which should have the method with the right value, but the only other way I can think of to solve this would be to make a pauseMenu for each level, which would not be ideal.
There are more replies on the next page.
1
2