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

2013/10/13

Remove Object after ~4sec if it is not been clicked on

Syntac Syntac

2013/10/13

#
public void explosion()
    {
        if(Greenfoot.mouseClicked(this))
            {
                getWorld().removeObject(this);
            }
                else 
                {
                    if(itimeTillDetonation > 40)
                    {
                        soundc.play();
                        getWorld().removeObject(this);
                        Greenfoot.stop();
                    }
                }
    }
If I click on the bomb then it should be removed. If I don´t hit the bomb after ~ 4sec the bomb explodes (soundc), then it should be removed and the game is over. The problem is now that if I hit the bomb, still the explosion sound appears.
danpost danpost

2013/10/13

#
Maybe you are clicking on the bomb just when the 'else' part becomes true. Try increasing the time till detonation. You may need to show more code from the class so we can have a better insight as to what might be needed (like the control of the value of 'itimeTillDetonation'; the act method; and possibly even more).
Syntac Syntac

2013/10/13

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bombe here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bombe extends Actor
{
    public int iGameOver = 0;
    public int iTime = 0;
    public int itimeTillDetonation =  0;
    GreenfootSound soundb = new GreenfootSound("BackgroundMusic.wav");    
    GreenfootSound soundc = new GreenfootSound("Explosion.wav");
    /**
     * Act - do whatever the Bombe wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        explosion();        
        itimeTillDetonation++;        
        iTime++;
        gameOver();
        iGameOver++;
    } 
    public void explosion()  
    {  
        soundb.play();
        if(iTime > 1200)
            {
                soundb.stop();
            }  
        if(Greenfoot.mouseClicked(this))  
            {  
                getWorld().removeObject(this);  
            }  
                else   
                {  
                    if(itimeTillDetonation > 100)  
                    {  
                        soundb.stop();
                        soundc.play();  
                        getWorld().removeObject(this);  
                        Greenfoot.stop();  
                    }  
                }  
    }    
    //Beende Spiel nach ca. 1min
    public void gameOver()
    {
        if(iGameOver > 1200)
        {
            Greenfoot.stop();
        }
    }
}
Ok, the sound problem is now solved- Thank you! Next problem. I can´t hit the bomb. I clicked the bomb like 1000 times but it doesn´t disappear. Only if the else part becomes true.
danpost danpost

2013/10/13

#
I do not know if what I suggest has anything to do with it or not; but, maybe you are checking for mouse clicks elsewhere in your scenario and it is catching them there instead of here.
You need to login to post a reply.