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

2016/2/26

Need help about lives in game ASAP

Milan00 Milan00

2016/2/26

#
Well i added "lives" in my game, it's red bar which when asteroid hits spaceship get reduced by 1/3 of it's size... But when they colide it reduce it by 1/3 then another 1/3 and another 1/3, as long as they are touching it keeps reducing and game ends so fast. So i need help, how do i make it reduce only 1/3 when it hits 1 asteroid not all lives. Here's the code that is in the Negativac(Asteroid) class:
public void collide()
    {
        Actor GlavniLik = getOneIntersectingObject(GlavniLik.class);

        if (GlavniLik != null)
        {
            World myWorld = getWorld();
            AvoiderWorld avoiderworld = (AvoiderWorld)myWorld;
            HealthBar healthbar = avoiderworld.getHealthBar();
           
            if(touchingGlavniLik == false)
            {
                healthbar.loseHealth();   
                touchingGlavniLik = true;
                
            }    
            i=i+1;
        } else {
            touchingGlavniLik = false;
        }

    }
And this is the code in the AvoiderWorld class:
public void act()
    {

        if(theNegativac.getI()==3)
        {
            removeObjects(getObjects(Negativac.class));
            removeObjects(getObjects(GlavniLik.class));
            addObject(new GameOver(),450,350);
            addObject(new PlayAgainDugme(),450,350);
            addObject(new ExitDugme(), 450,390);
            setPaintOrder(Counter.class);
            SpaceMusic.stop();
        }
        else{
            int score=theCounter.getValue();
            if(Greenfoot.getRandomNumber(limit1) < 20 && theNegativac.getI()!=3)
            {
                addObject(new Negativac(), Greenfoot.getRandomNumber(getWidth()-20)+10, - 30);
                theCounter.add(5);

            }
        }
As you can see it should add 1 to variable "i" when they colide but it keeps adding as long as they are touching. Help pls ASAP
danpost danpost

2016/2/26

#
You should be able to just change line 5 of the 'collide' method to:
if (!touchingGlavniLik && GlavniLik != null)
Milan00 Milan00

2016/2/26

#
danpost wrote...
You should be able to just change line 5 of the 'collide' method to:
if (!touchingGlavniLik && GlavniLik != null)
Nothing changed, it still reduces all lives when they collide. :/
danpost danpost

2016/2/26

#
Okay, now try changing line 18 to this:
} else if (GlavniLik == null) {
danpost danpost

2016/2/27

#
By the way, with the first change, you should remove the condition on line 11 and just execute the statements on lines 13 and 14. Also, you can simply a bit more by setting 'touchingGlavniLik' after the main 'if' block and remove the 'else' part altogether:
public void collide()
{
    Actor glavniLik = getOneIntersectingObject(GlavniLik.class);
    if (!touchingGlavniLik && glavniLik != null)
    {
        ((AvoiderWorld)getWorld()).getHealthBar().loseHealth();
        i++;
    }
    touchingGlavniLik = glavniLik != null;
}
Milan00 Milan00

2016/2/28

#
I got this after after changing the code
java.lang.IllegalArgumentException: Could not open sound file: Laser.mp3
	at greenfoot.sound.SoundExceptionHandler.handleIOException(SoundExceptionHandler.java:67)
	at greenfoot.sound.SoundFactory.createSound(SoundFactory.java:120)
	at greenfoot.Greenfoot.playSound(Greenfoot.java:163)
	at GlavniLik.followMouse(GlavniLik.java:29)
	at GlavniLik.act(GlavniLik.java:22)
	at greenfoot.core.Simulation.actActor(Simulation.java:594)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:552)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
Caused by: java.io.FileNotFoundException: Could not find file: Laser.mp3
	at greenfoot.util.GreenfootUtil.getURL(GreenfootUtil.java:544)
	at greenfoot.sound.SoundFactory.createSound(SoundFactory.java:99)
	... 7 more
java.lang.IllegalArgumentException: Could not open sound file: Laser.mp3
	at greenfoot.sound.SoundExceptionHandler.handleIOException(SoundExceptionHandler.java:67)
	at greenfoot.sound.SoundFactory.createSound(SoundFactory.java:120)
	at greenfoot.Greenfoot.playSound(Greenfoot.java:163)
	at GlavniLik.followMouse(GlavniLik.java:29)
	at GlavniLik.act(GlavniLik.java:22)
	at greenfoot.core.Simulation.actActor(Simulation.java:594)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:552)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
Caused by: java.io.FileNotFoundException: Could not find file: Laser.mp3
	at greenfoot.util.GreenfootUtil.getURL(GreenfootUtil.java:544)
	at greenfoot.sound.SoundFactory.createSound(SoundFactory.java:99)
	... 7 more
danpost danpost

2016/2/28

#
That is an unrelated error. The compiler does not continue to compile after it finds an error. It reports one error at a time. Once one issue is fixed, the compiler will no longer give the same error, but continue on either until the project is totally compiled or another error is found and reported.
Milan00 Milan00

2016/3/1

#
I fixed it, but it still has problems game does not end after 3 hits it reduces the lives to 0 but it is like i have 30 not 3 of them
Milan00 Milan00

2016/3/2

#
I fixed it :D
You need to login to post a reply.