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

2012/4/20

One object eating then recreating it in a different position.

1
2
lgbrf lgbrf

2012/4/21

#
public int lifeCounter = 3;
lgbrf lgbrf

2012/4/21

#
so yes its at 3 so 3-2 2-1 1-0 should have 3 lifes, but apparently not, game over screen doesnt pop up either
danpost danpost

2012/4/21

#
Post world class act() method.
lgbrf lgbrf

2012/4/21

#
public Space() { super(500, 550, 1); //intro.play(); populate(); killPlayer(lifeCounter); } this one?
danpost danpost

2012/4/21

#
That is the world class constructor, not the world class act() method. The world class constructor is only called once, each time an instance of this world is created. The act() method is called once each cycle; that is, the world and all objects in the world each get a chance to act once, to complete a cycle; when one cycle is complete, it starts another. Your world class should look like this:
import greenfoot.*;

public class Space extends World
{
    int lifeCounter = 3;

    public Space()
    {
        super(500, 550, 1);
        // intro.play();
        populate();
    }

    public void populate()
    {
        // populate code
    }

    public void act()
    {
        checkLifeStatus();
    }

    private void checkLifeStatus()
    {
        if (getObjects(Player.class).isEmpty())
        {
            lifeCounter--;
            if (lifeCounter == 0) gameOver();
            addObject(new Player(), 250, 510);
        }
    }

    private void gameOver()
    {
        System.out.println("GAME OVER");
        Greenfoot.stop();
    }
}
I changed the name of the method to something a little more appropriate; and the variable does not need to be passed to it (as it is in this class).
lgbrf lgbrf

2012/4/21

#
Now my character keeps being created so it makes a line of them..
lgbrf lgbrf

2012/4/21

#
Whole Code:
import greenfoot.*;  // imports Actor, World, Greenfoot, GreenfootImage

/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class Space extends World
{
    public int lifeCounter = 3;
    GreenfootSound intro = new GreenfootSound("pacman_beginning.wav");
    /**
     *  Constructor for objects of class Space.
     */
    public Space() 
    {
        super(500, 550, 1);
        //intro.play();
        populate();
    }
    public void act()
    {
        checkLifeStatus(lifeCounter);
    }
    private void gameOver()
    {
        System.out.println("GAME OVER");
        Greenfoot.stop();
    }

    private void checkLifeStatus(int lifeCounter){
        if (getObjects(Player.class).isEmpty()); 
        {  
            lifeCounter -- ;  
            if (lifeCounter == 0) gameOver();
            addObject(new Player(), 250, 510);  
        }  
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void populate()
    {
        Player player = new Player();
        addObject(player, 250, 510);
        Tetrimino tetrimino0 = new Tetrimino(0);
        tetrimino0.setSpeed(2);
        addObject(tetrimino0,30,50);
        Tetrimino tetrimino1 = new Tetrimino(1);
        tetrimino1.setSpeed(4);
        addObject(tetrimino1,30,100);
        Tetrimino tetrimino2 = new Tetrimino(2);
        tetrimino2.setSpeed(1);
        addObject(tetrimino2,30,150);
        Tetrimino tetrimino3 = new Tetrimino(3);
        tetrimino3.setSpeed(3);
        addObject(tetrimino3,30,200);
        Invader invader0 = new Invader(0);
        invader0.setSpeed(4);
        addObject(invader0,30,300); 
        Invader invader1 = new Invader(1);
        invader1.setSpeed(8);
        addObject(invader1,30,350);
        Invader invader2 = new Invader(2);
        invader2.setSpeed(2);
        addObject(invader2,30,400); 
        Invader invader3 = new Invader(3);
        invader3.setSpeed(6);
        addObject(invader3,55,450);

    }
}
danpost danpost

2012/4/21

#
Sorry, line 30 should be 'else addObject(new Player(), 250, 510);'.
lgbrf lgbrf

2012/4/21

#
ive fixed all that but the glitch still happens where it keeps reespawning, making a solid line
danpost danpost

2012/4/21

#
Crumb buckets!
danpost wrote...
Sorry, line 30 should be 'else addObject(new Player(), 250, 510);'.
I meant line 38 (not line 30). You should have caught that, though. 1) Did you re-compile? 2) Check through your code (all classes) and 'find' all 'addObject' text and see if there are any other addObject(new Player()... or addObject(player.class object... statements that might be causing this. 3) Post back results, or fixes.
lgbrf lgbrf

2012/4/21

#
yeah i figured it was 38, i recompile each time, and there was only the other add object in the populate(); so i removed that, still glitched??
You need to login to post a reply.
1
2