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

2012/10/12

Making a bomb

1
2
Stephon231 Stephon231

2012/10/12

#
how can i have the class Bomb use the Explode() mehtod after 3sec
danpost danpost

2012/10/12

#
Add an instance int counter to the Bomb class. Call it... tMinus (or something to let you know what it is for). Start the counter by setting its value to 600 or so (you will have to adjust to suit). In the act() method, add the following:
if (tMinus > 0)
{
    tMinus--;
    if (tMinus == 0) Explode();
}
Stephon231 Stephon231

2012/10/12

#
would the instance look like private int counter tminus = 600;
danpost danpost

2012/10/12

#
If you want the Bomb to be armed immediately upon creation, yes.
Stephon231 Stephon231

2012/10/12

#
do i need a timer class
danpost danpost

2012/10/12

#
No. Just put it in the Bomb class. And since you are arming the bomb immediately upon creation and will be removing the Bomb after it explodes, your act can be very simple. Like this
public void act()
{
    tminus--;
    if (tminus == 0) Explode();
}
Stephon231 Stephon231

2012/10/12

#
it isn't working it says that it wants it to public int counter; tMinus = 600; but i changed it to that and it says that it is an invalid declaration
danpost danpost

2012/10/12

#
Do you have 'public int counter; tMinus = 600;'? (with two semi-colons in the line) If not, post what you have in the Bomb class at this point.
Stephon231 Stephon231

2012/10/12

#
ya is there anouther way
danpost danpost

2012/10/12

#
It should read
private int  tMinus = 600;
Sorry, I did not see the error in what you posted earlier.
danpost danpost

2012/10/12

#
I called it a counter, but I named it 'tMinus'. I guess that is what confused you. But, it is an instance variable called 'tMinus', used as a counter (or timer).
Stephon231 Stephon231

2012/10/12

#
so should it look like this because it still is asking for ;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bomb here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bomb extends Actor
{
     
    private int  tMinus = 600;  
                      
   
 
    public void act() 
    {
        if (tMinus > 0)  
    {  
           tMinus--;  
           if (tMinus == 0) Explode();  
        }
danpost danpost

2012/10/12

#
No, it should look like this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bomb here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Bomb extends Actor
{
    private int tMinus = 600;

    public void act()
    {
        tMinus--;
        if (tMinus == 0) Explode();
    }

    private void Explode()
    {
        // some code
    }
}
Stephon231 Stephon231

2012/10/12

#
what is 600 secounds, milisecounds what
danpost danpost

2012/10/12

#
600 is frame count (or act cycles). 3 seconds would be determined by the number of frames per second that your scenario runs at. That is why I said the number (600) would have to be adjusted.
There are more replies on the next page.
1
2