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

2016/12/3

How do i make a timer delete an object after 60 seconds

ruvdog25808 ruvdog25808

2016/12/3

#
Hey guys i want to make this "NyanClone" get deleted 60 seconds after it is spawned in by a power up so how do i do it exactly the code i have so far in the clone class is
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class NyanClone here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class NyanClone extends Actor
{
    private int timer = 0;
    private int shotTimer = 0;
    int objectsX;
    int objectsY;
    /**
     * Act - do whatever the NyanClone wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (shotTimer > 0)
        {
            shotTimer = shotTimer - 7;
        }
        else if (Greenfoot.isKeyDown("space")) 
        {
            Greenfoot.playSound("Lazer.wav");
            getWorld().addObject(new ShotClone(this), getX(), getY());
            shotTimer = 400; 
        }
        if (Greenfoot.isKeyDown ("Left"))
        {
            move(-1);
        }
        if (Greenfoot.isKeyDown ("Right"))
        {
            move(1); 
        }
        if (Greenfoot.isKeyDown ("up"))
        {
            setLocation(getX(),getY()-1);           
        }
        if (Greenfoot.isKeyDown ("down"))
        {
            setLocation(getX(),getY()+1);           
        } 
        checkCollision();
    }  

    public void checkCollision()
    {   Actor GameOver;
        GameOver = getOneIntersectingObject(Asteroid.class);
        if(GameOver != null)
        {                      
            getWorld().removeObject(this);                         
        }
    }

    public void deleteClone()
    {
        if(this.getClass() != null)
        {   
            if (timer ==60)
            {
                timer--;
                if(timer == 0)
                {
                    getWorld().removeObject(this);

                }
            }
        }
    }
}
Thanks in advance :)
danpost danpost

2016/12/3

#
I, first, do not see where the 'deleteClone' method is being called from (I presume you would want to call it from the 'act' method). Then, line 61 has an 'if' statement that will never be false (maybe you meant to use 'getWorld' instead of 'getClass' -- since the 'checkCollision' method could possibly remove the actor from the world), Finally, line 63 uses '60' as the number of act cycles for the life of the actor, which is only about one second of time (use something more in the range of 3000 to 3300).
You need to login to post a reply.