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

2012/12/13

adding a shield

1
2
3
vonmeth vonmeth

2012/12/14

#
When I tested it, explosions still happened when something ran into it. It should be fine where it is. Where it is, it just prevents damage, not the rest from happening.
davemib123 davemib123

2012/12/14

#
when a bullet interacts with the shield there is no explosion. the bullet flies through it.
vonmeth vonmeth

2012/12/14

#
if(!Hero.shieldActive){ 
        Bar bar = cityWorld.getBar();
        bar.subtract(10);  
        }  
That is all that is in the if statement, right?
        getWorld().addObject(new explosionSmall(), getX(), getY());  
        getWorld().removeObject(this);  
That is outside of it?
davemib123 davemib123

2012/12/15

#
ha lol, i missed the closing brace for the if statement
davemib123 davemib123

2012/12/15

#
the shield works fine. i've tried to adapt the code for the weapon it works but the image doesnt stay for longer than a second. another problem is that from the scoreboard if I press "Play Again" the scoreboard sound still plays.
vonmeth vonmeth

2012/12/15

#
First, I suggest you fix how you handle your gameOver(). They way to fix it is to follow the suggestion I gave earlier. Right now, you are creating tons and tons of ScoreBoard objects, over and over. Each act cycle, a new Scoreboard is created. This ended up causing a "java.lang.OutOfMemoryError: Java heap space" error on my part. It will work fine with it like this.
    private boolean finished;

    public void act()  
    {   
        if (bar.value == 0 && !finished){
            gameOver();
        }
        if(!finished)
        {
        droneSpawn ++;   
        if (droneSpawn > 150){
            addObject(new Drone(),850,Greenfoot.getRandomNumber(300)); 
            droneSpawn = 0;
        }
        hunterSpawn ++;   
        if (hunterSpawn > 500){
            int hunterSelection = Greenfoot.getRandomNumber(10);
            if (hunterSelection >= 6){
                addObject(new HunterRam(),850,Greenfoot.getRandomNumber(215)+44); 
                hunterSpawn = 0;
            } 
            if (hunterSelection <= 5){
                addObject(new HunterPattern(),Greenfoot.getRandomNumber(200)+520,400); 
                hunterSpawn = 0;
            }
        }
        bomberSpawn ++;   
        if (bomberSpawn > 1200){
            int bomberSelection = Greenfoot.getRandomNumber(10);
            if (bomberSelection >= 6){
                addObject(new BomberRam(),850,Greenfoot.getRandomNumber(73)+114); 
                bomberSpawn = -300;
            } 
            if (bomberSelection <= 5){
                addObject(new BomberPattern(),950,Greenfoot.getRandomNumber(73)+114); 
                bomberSpawn = -300;
            } 
        }
        powerupSpawn ++;   
        if (powerupSpawn > 800){
            int powerupSelection = Greenfoot.getRandomNumber(10);
            if (powerupSelection >= 7){
                addObject(new HealthPowerup(),850,Greenfoot.getRandomNumber(300)); 
                powerupSpawn = 0;
            } 
            if (powerupSelection <= 3){
                addObject(new ShieldPowerup(),850,Greenfoot.getRandomNumber(300)); 
                powerupSpawn = 0;
            } 
            if ((powerupSelection >= 4) & (powerupSelection <=6)){
                addObject(new WeaponPowerup(),850,Greenfoot.getRandomNumber(300)); 
                powerupSpawn = 0;
            } 
        }
        }
    } 

    public void gameOver()
    {
        bgSound.stop();
        scoreSound.playLoop();
        removeObjects(getObjects(null)); 
        Saver saver = new Saver();
        saver.saveHighscore(theCounter.getValue());

        ScoreBoard board = new ScoreBoard(getWidth(), getHeight());
        addObject(board, getWidth() /2, getHeight() /2);
        addObject(new PlayAgain(), 393, 26);
        finished = true;
    }
You need to stop the sound before you go to a new City. Make GreenfootSound scoreSound public, or make a method that is public that can stop the sound for you., and then enter in the following for PlayAgain class.
    public void act() 
    {
        if (Greenfoot.mousePressed(this) )  
        {  
            City city = (City) getWorld();
            city.scoreSound.stop();
            Greenfoot.setWorld(new City());
        } 
    }    
I'm not really sure what you mean with "i've tried to adapt the code for the weapon it works but the image doesnt stay for longer than a second. " Sorry.
davemib123 davemib123

2012/12/15

#
thanks for the suggestions, I will give them a go in the morning. In terms of the weapon. If you have a look at the hero class you will see that I have setup a weaponActive boolean and weaponCounter int variable similar to the shield. On line 105 I have a method to change the ship colour when the weaponActive is set to true. The problem is when I collect the powerup (currently a big green circle) the multi-bullets is active on the hero but the image (Weapon.png) for the hero does not stay.
vonmeth vonmeth

2012/12/15

#
The problem is in your act code for the Hero:
        if(shieldCounter>0){ 
            shieldCounter--;  
        }
        if(shieldCounter==0) {  
            setImage("Hero.png");  
            shieldActive = false;  
        } 
        
        if(weaponCounter>0){ 
            weaponCounter--;  
        }
        if(weaponCounter==0) {   
            weaponActive = false;  
        } 
What would have here, step by step, if your shieldCounter is 0, and your weaponCounter is currently at 30.
davemib123 davemib123

2012/12/15

#
Shield should have the image set to Hero.png becuase it is 0 and Weapon then set to Weapon.png becuase it is set at 30.
vonmeth vonmeth

2012/12/15

#
Yes, but "if(shieldCounter==0)" is still being evaluated even when you have a Weapon power-up still active. That is immediately setting your image back to "Hero.png". Your hero is only set to "Weapon.png" when it gets the power up. This will fix it for you:
        if(shieldCounter>0){ 
            shieldCounter--;  
        }
        if(shieldCounter==0 && shieldActive) {  
            setImage("Hero.png");  
            shieldActive = false;  
        } 
        
        if(weaponCounter>0){ 
            weaponCounter--;  
        }
        if(weaponCounter==0 && weaponActive) {   
            weaponActive = false;
            setImage("Hero.png");
        } 
davemib123 davemib123

2012/12/15

#
ah rite checking two conditions together. makes sense :). many thanks
davemib123 davemib123

2012/12/16

#
vonmeth wrote...
You need to stop the sound before you go to a new City. Make GreenfootSound scoreSound public, or make a method that is public that can stop the sound for you., and then enter in the following for PlayAgain class.
    public void act() 
    {
        if (Greenfoot.mousePressed(this) )  
        {  
            City city = (City) getWorld();
            city.scoreSound.stop();
            Greenfoot.setWorld(new City());
        } 
    }    
I made the scoreSound variable public and have a public method in the City class. In the playAgain class I have it like this:
 public void act() 
    {
        if (Greenfoot.mousePressed(this) )  
        {  
           City city = (City) getWorld();  
        city.started();  
        Greenfoot.setWorld(new City());
        } 
    }   
In the city class, the started method is:
 public void started()
    {
        scoreSound.stop();
        bgSound.playLoop();
    }
what happens is that the scoreSound stops, the background sound plays. But when I press stop the background sound continues playing. Even though I have this method:
  public void stopped()
    {
        bgSound.pause();
        scoreSound.pause();
    }
the stopped method works fine when I press stop before the scoreboard is shown. hope that makes sense?
vonmeth vonmeth

2012/12/16

#
The reason that is not working is because when you created a "new world" after pressing PlayAgain, you told the bgsound from the "old world" to start playing. It will continue playing in your "new world." Your "new world" has its own bgsound. It starts off not playing (the started/stopped methods are ran when you start/unpause the scenario, not when the world is created). You can see this by pausing, and unpausing after you've hit the PlayAgain. You will hear the "new world" bgsound playing along with the "old worlds" bgsound.
davemib123 davemib123

2012/12/16

#
if you do not mind, could you give me a hint to what I should be trying out?
vonmeth vonmeth

2012/12/16

#
    GreenfootSound scoreSound;
    private boolean finished;

    public void stopped()
    {
        bgSound.pause();
        scoreSound.pause();
    }

    public void started()
    {
       if(finished)scoreSound.playLoop();  // so when pausing/unpausing, the correct sound plays
       if(!finished)bgSound.playLoop();
    }

    public void act()  
    {   
        if (bar.value == 0 && !finished){
            gameOver();
        }
        if(!finished)
        {
        bgSound.playLoop(); // so the sound plays automatically when we create the world
        droneSpawn ++;   
        if (droneSpawn > 150){
            addObject(new Drone(),850,Greenfoot.getRandomNumber(300)); 
            droneSpawn = 0;
        }
        hunterSpawn ++;   
        if (hunterSpawn > 500){
            int hunterSelection = Greenfoot.getRandomNumber(10);
            if (hunterSelection >= 6){
                addObject(new HunterRam(),850,Greenfoot.getRandomNumber(215)+44); 
                hunterSpawn = 0;
            } 
            if (hunterSelection <= 5){
                addObject(new HunterPattern(),Greenfoot.getRandomNumber(200)+520,400); 
                hunterSpawn = 0;
            }
        }
        bomberSpawn ++;   
        if (bomberSpawn > 1200){
            int bomberSelection = Greenfoot.getRandomNumber(10);
            if (bomberSelection >= 6){
                addObject(new BomberRam(),850,Greenfoot.getRandomNumber(73)+114); 
                bomberSpawn = -300;
            } 
            if (bomberSelection <= 5){
                addObject(new BomberPattern(),950,Greenfoot.getRandomNumber(73)+114); 
                bomberSpawn = -300;
            } 
        }
        powerupSpawn ++;   
        if (powerupSpawn > 800){
            int powerupSelection = Greenfoot.getRandomNumber(10);
            if (powerupSelection >= 7){
                addObject(new HealthPowerup(),850,Greenfoot.getRandomNumber(300)); 
                powerupSpawn = 0;
            } 
            if (powerupSelection <= 3){
                addObject(new ShieldPowerup(),850,Greenfoot.getRandomNumber(300)); 
                powerupSpawn = 0;
            } 
            if ((powerupSelection >= 4) & (powerupSelection <=6)){
                addObject(new WeaponPowerup(),850,Greenfoot.getRandomNumber(300)); 
                powerupSpawn = 0;
            } 
        }
        }
    } 

    public void gameOver()
    {
        bgSound.stop();
        scoreSound.playLoop();
        removeObjects(getObjects(null)); 

        Saver saver = new Saver();
        saver.saveHighscore(theCounter.getValue());

        ScoreBoard board = new ScoreBoard(getWidth(), getHeight());
        addObject(board, getWidth() /2, getHeight() /2);
        addObject(new PlayAgain(), 393, 26);
        finished = true;
    }
public class PlayAgain extends Actor
{
    /**
     * Act - do whatever the PlayAgain wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mousePressed(this) )  
        {  
            City city = (City) getWorld();
            city.scoreSound.stop();
            Greenfoot.setWorld(new City());
        } 
    }    
}
It works fine like this on my end.
There are more replies on the next page.
1
2
3