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

2013/11/20

Need Urgent Help With Putting A Duration on Powerups.

cheshire cheshire

2013/11/20

#
I have a school project due on Friday, and I am unable to put a duration on the powerups. I have three - one to make the player safe from the enemy, one to make them able to eat the enemy, and one to double the amount of points they get when they eat stars. I've tried:
    private long pstart, pend = 15000;
    
    private void powerup()
    {
        Actor powerup1;
        powerup1 = getOneObjectAtOffset(0, 0, powerup1.class);
        if (powerup1 != null)
        {
            World world;
            world = getWorld();
            world.removeObject(powerup1);
            play playWorld = (play) getWorld();  
            p1p p1px = playWorld.getP1P();
            p1px.setP(1);
            pstart = System.currentTimeMillis();
        }
        
        Actor powerup2;
        powerup2 = getOneObjectAtOffset(0, 0, powerup2.class);
        if (powerup2 != null)
        {
            World world;
            world = getWorld();
            world.removeObject(powerup2);
            play playWorld = (play) getWorld();  
            p1p p1px = playWorld.getP1P();
            p1px.setP(2);
            pstart = System.currentTimeMillis();
        }  
        
        Actor powerup3;
        powerup3 = getOneObjectAtOffset(0, 0, powerup3.class);
        if (powerup3 != null)
        {
            World world;
            world = getWorld();
            world.removeObject(powerup3);
            play playWorld = (play) getWorld();  
            p1p p1px = playWorld.getP1P();
            p1px.setP(3);
            pstart = System.currentTimeMillis();
        }      
        
        if (pstart + pend < System.currentTimeMillis())
        {
            play playWorld = (play) getWorld();  
            p1p p1px = playWorld.getP1P();
            p1px.setP(0);
        }
    }
and also
    private void powerup()
    {
        Actor powerup1;
        powerup1 = getOneObjectAtOffset(0, 0, powerup1.class);
        if (powerup1 != null)
        {
            World world;
            world = getWorld();
            world.removeObject(powerup1);
            play playWorld = (play) getWorld();  
            p1p p1px = playWorld.getP1P();
            p1px.setP(1);
            timer();
        }
        
        Actor powerup2;
        powerup2 = getOneObjectAtOffset(0, 0, powerup2.class);
        if (powerup2 != null)
        {
            World world;
            world = getWorld();
            world.removeObject(powerup2);
            play playWorld = (play) getWorld();  
            p1p p1px = playWorld.getP1P();
            p1px.setP(2);
            timer();
        }  
        
        Actor powerup3;
        powerup3 = getOneObjectAtOffset(0, 0, powerup3.class);
        if (powerup3 != null)
        {
            World world;
            world = getWorld();
            world.removeObject(powerup3);
            play playWorld = (play) getWorld();  
            p1p p1px = playWorld.getP1P();
            p1px.setP(3);
            timer();
        }      
    }

    private void timer()
    {
        int p1timer = 900;
        if (p1timer > 0)
        {
            p1timer--;
        }
        if (p1timer == 0)
        {
            play playWorld = (play) getWorld();  
            p1p p1px = playWorld.getP1P();
            p1px.setP(0);
        }
I've used variations of the second where I've put the timer code directly into the part where the powerup is collected, into the world, and into a separate actor/class altogether. P1P is the 'counter' that keeps track of player 1's powerups. For example, if they collect powerup 1, it'll set an integer P to 1. When there's no powerups, it's 0. What I need to do is find a way to have it set back to 0 after a certain amount of time, preferably 15 seconds.
cheshire cheshire

2013/11/20

#
Nevermind, I worked it out.
You need to login to post a reply.