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

2017/4/10

Reseting the world using an actor

1
2
danpost danpost

2017/4/12

#
Change line 20 to:
if (getWorld().getObjects(Player.class).isEmpty()) roam(); else goTo();
Then create a method called 'roam' and add code for random movement in it for the aliens.
AustinProkopishin AustinProkopishin

2017/4/12

#
ok that works but when the character dies the world pauses ( it doesn't crash). it seems to work fine when I hit run again but I don't know why it stops. I'm not getting any error messages.
danpost danpost

2017/4/12

#
AustinProkopishin wrote...
ok that works but when the character dies the world pauses ( it doesn't crash). it seems to work fine when I hit run again but I don't know why it stops. I'm not getting any error messages.
What code do you have for when the player is dying?
AustinProkopishin AustinProkopishin

2017/4/13

#
this is in my act method for my player
//checking for death
        if (isTouching(alien.class)) {
            removeTouching(alien.class);
            death();
        } 
        if (isTouching(Asteroid1.class)) {
            removeTouching(Asteroid1.class);
            death();
        } 
        if (isTouching(Asteroid2.class)) {
            removeTouching(Asteroid2.class);
            death();
        } 
and this is the death method
public void death()
    {
                lives--;
        getWorld().addObject (new explosion2(), getX(), getY());
        getWorld().removeObject(this);
    }
Super_Hippo Super_Hippo

2017/4/13

#
Change the 'if' in lines 6 and 10 to 'else if'. If you want to have more than one life, you should only remove the object if 'lives' is 0 after line 3.
AustinProkopishin AustinProkopishin

2017/4/13

#
Super_Hippo wrote...
Change the 'if' in lines 6 and 10 to 'else if'. If you want to have more than one life, you should only remove the object if 'lives' is 0 after line 3.
Thanks it fixed it! unfortunately I just realized a logic error in my lives code. Each time the player dies the world resets and he loses a life. when he gets to 0 it goes to a game over screen. the problem is when the world resets it resets the lives int to 3, so the player never loses a life and the game never ends. I don't know how to get around this since the variable is constantly resetting
danpost danpost

2017/4/13

#
Replace the following in your Player class:
if (deaths == 1) {
    Greenfoot.setWorld(new MyWorld());
}
with this:
if (deaths == 1) {
    MyWorld mw = new MyWorld();
    mw.setLives(((MyWorld)getWorld()).getLives()-1);
    Greenfoot.setWorld(mw);
}
and add the following to your MyWorld class
// methods
public void setLives{int num)
{
    lives = num;
}

public int getLives()
{
    return lives;
}
AustinProkopishin AustinProkopishin

2017/4/14

#
Earlier you asked me to move this
Greenfoot.setWorld(new MyWorld());
to my world class so that the explosion could play out
 if (getObjects(Player.class).isEmpty() && getObjects(explosion2.class).isEmpty())
        {
            Greenfoot.setWorld(new MyWorld());
        }  
so I put the new code in my MyWorld class but it is saying it cannot find symbol "getWorld"
danpost danpost

2017/4/14

#
AustinProkopishin wrote...
Earlier you asked me to move this
Greenfoot.setWorld(new MyWorld());
to my world class so that the explosion could play out
 if (getObjects(Player.class).isEmpty() && getObjects(explosion2.class).isEmpty())
        {
            Greenfoot.setWorld(new MyWorld());
        }  
so I put the new code in my MyWorld class but it is saying it cannot find symbol "getWorld"
Your right. So ignore my last post and try this:
// replace line 40 though 42 in MyWorld class (in previously given MyWorld code) with this
MyWorld mw = new MyWorld();
mw.lives = --lives;
Greenfoot.setWorld(mw);
I am not sure without testing if you need to use the methods I gave in my last response; but I do not think you need them.
AustinProkopishin AustinProkopishin

2017/4/14

#
It works! My project is nearly done thank you for all of your help!
AustinProkopishin AustinProkopishin

2017/4/14

#
I thought I was finished my project but now that I test it all out the game lags horribly, sometimes crawling at a snails pace! it gets worse the longer the world runs without being reset. I don't know what is wrong. I am not getting any error messages.
Yehuda Yehuda

2017/4/14

#
If you have too many actors in the world then there will be lag (so if you have an actor which adds more of itself to the world then there will be lag from running all the code).
AustinProkopishin AustinProkopishin

2017/4/14

#
Yehuda wrote...
If you have too many actors in the world then there will be lag (so if you have an actor which adds more of itself to the world then there will be lag from running all the code).
After reading your comment I realized I had put a section of code in the act method that should have been elsewhere, placing actors every act cycle. Thanks for your help.
AustinProkopishin AustinProkopishin

2017/4/14

#
I uploaded my finished project here: http://www.greenfoot.org/scenarios/19215 if you are interested in checking it out. Thanks a lot for all the help!
You need to login to post a reply.
1
2