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

2018/6/24

Greenfoot freezes when saving the world (Mac)

tosteck tosteck

2018/6/24

#
Hi I'm using a Mac. Whenever I save the world and try to run the program, it works for a few seconds and then freezes. It works fine if I don't save the world. pls help
tosteck tosteck

2018/6/24

#
danpost danpost

2018/6/24

#
tosteck wrote...
I'm using a Mac. Whenever I save the world and try to run the program, it works for a few seconds and then freezes. It works fine if I don't save the world. pls help
Out of curiosity, please supply your World subclass code after saving the world.
tosteck tosteck

2018/6/25

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); Platform platform = new Platform(200, 30); addObject(platform, 300, 300); prepare(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Jumper jumper = new Jumper(); addObject(jumper,314,153); } }
tosteck tosteck

2018/6/25

#
Also my bad, this problem happens even if I haven't saved the world yet. It also doesn't freeze, the keyboard inputs stop working.
danpost danpost

2018/6/25

#
tosteck wrote...
Also my bad, this problem happens even if I haven't saved the world yet. It also doesn't freeze, the keyboard inputs stop working.
Okay -- let's look at the Jumper class.
tosteck tosteck

2018/6/25

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Jumoer here. * * @author (your name) * @version (a version number or a date) */ public class Jumper extends Actor { private final int GRAVITY = 1; private int velocity; public Jumper() { velocity = 0; } public void act() { fall(); if (Greenfoot.isKeyDown("space") && isOnSolidGround()) jump(); move(); } public void fall() { setLocation(getX(), getY() + velocity); if (isOnSolidGround()) { velocity = 0; while (isOnSolidGround()) { setLocation(getX(), getY() - 1); } setLocation(getX(), getY() + 1); } else if (velocity < 0 && didBumpHead()) velocity = 0; else velocity += GRAVITY; } public void jump () { velocity = -20; } public void move(){ int y = getY(); int x = getX(); if (Greenfoot.isKeyDown("A")) x-= 3; if (Greenfoot.isKeyDown("D")) x += 3; setLocation(x, y); } public boolean isOnSolidGround() { boolean isOnGround = false; if (getY() > getWorld().getHeight() - 100) isOnGround = true; int imageWidth = getImage().getWidth(); int imageHeight = getImage().getHeight(); if (getOneObjectAtOffset(imageWidth / -2, imageHeight / 2, Platform.class) != null || getOneObjectAtOffset(imageWidth / 2, imageHeight / 2, Platform.class) != null) isOnGround = true; return isOnGround; } public boolean didBumpHead() { boolean bumpedHead = false; int imageWidth = getImage().getWidth(); int imageHeight = getImage().getHeight(); if (getOneObjectAtOffset(imageWidth / -2, imageHeight / 2, Platform.class) != null || getOneObjectAtOffset(imageWidth / 2, imageHeight / 2, Platform.class) != null) bumpedHead = true; return bumpedHead; } }
danpost danpost

2018/6/25

#
tosteck wrote...
<< Code Omitted >>
The following part:
while (isOnSolidGround()) {
    setLocation(getX(), getY() - 1);
}
is probably the problem. Unless you are sure that at some point the actor will go from being on solid ground to NOT being on solid ground, this will loop forever.
danpost danpost

2018/6/25

#
I do have a Jump and Run Demo w/Moving Platform scenario you might be interested in looking at. The Who button along the bottom of the window shows the codes I used for the player.
tosteck tosteck

2018/6/25

#
I deleted while (isOnSolidGround()) { setLocation(getX(), getY() - 1); } setLocation(getX(), getY() + 1); but it still freezes. I'll check out your scenario though.
tosteck tosteck

2018/6/25

#
I uploaded my scenario to test it on the web and it works fine. :/
You need to login to post a reply.