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

2012/4/16

Im trying to get a certain period of time dedicated to a certain action.

2patrickMurphy 2patrickMurphy

2012/4/16

#
I have to make a game for school and it involves a worm that is represented through an arraylist. In my game i will have "powerups" that manipulate the worm for a duration of 5 seconds each. The code i currently have does not work. The error i have now is this, when the worm goes to the power up it freezes. Stops moving for the 5 seconds then it begins to collide with itself. (in other words the first part of the link stays put while the other parts collide with the first one). Thats my guess. Im gonna debug it today but if anyone has ideas or other ways to implement this idea let me know. here is the code.
 /**
     * Act - do whatever the Player1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveWorm(0);        
        PowerUp powerup = (PowerUp)getOneIntersectingObject(PowerUp.class);
        if( powerup != null)
        {
            hitPowerUp = true;
        }
        if(hitPowerUp == true)
        {
            powerUp();
            hitPowerUp = false;
        }        
        if((index() == 0)) {
            controls();
            bodyCrash();
        }
        else if(acts%1 == 0) {
            slowDown();
            followTheLeader();            
        }
        acts++;
    }    

    public void powerUp()
    {
        long t0 = System.currentTimeMillis();
        long t1 = 0;
        speed = true;
        do{
            t1 = System.currentTimeMillis();
            moveWorm(1);
        }while((t1 - t0)  < 5000);
        speed = false;
    }

    public void moveWorm(int variable)
    {        
        switch(variable)
        {
            case 1: move(10);
            break;
            case 2: move(-1);
            break;
            default: move(2);
            break;
        }
    }
2patrickMurphy 2patrickMurphy

2012/4/17

#
I made some adjustments just getting rid of if statements and method calls im thinking its an efficiency issue with greenfoot. But the problem stays the same. Only difference is it no longer collides with itself it just freezes moves alittle then moves back at regular speed. If you want to see the source code ill send it to you but im just gonna wait for request. Also is this a threading issue?
2patrickMurphy 2patrickMurphy

2012/4/17

#
my next guess is its a problem with the rest of the arraylist. I think only the part that touches the powerup goes faster while the rest stay motionless because they have"not touched" the powerup. Im going to be correcting this and see if it works better. yea so it already followed the first one before hand i just added extra stuff that did the same thing as before. I have no clue whats going on here so yea.....
danpost danpost

2012/4/17

#
The way your powerUp() method works, your whole scenario (everything) will freeze for the 5 seconds, except that the worm will jump (probably to an edge of the screen). This is because the other parts of the worm do not get a chance to act() during those 5 seconds (the 'do-while' is executing at that time, hogging up CPU time). Create an object boolean 'private boolean onPU = false;' and an object long 'private long t0 = 0;' When powerUp is hit, set onPU to true and t0 to currentTime. In act()
if (onPU)
{
    if (System.currentTimeMillis() < t0 + 5000)
    {
        moveWorm(1);
        return;
    }
    else
    {
        onPU = false;
    }
}
PowerUp powerup = (PowerUp)getOneIntersectingObject(PowerUp.class);
if( powerup != null)
{
    onPU = true;
    t0 = System.currentTimeMillis;
    return;
}
BTW, in the code you supplied, lines 9 through 17 is equivalent to
if (powerUp != null) powerUp();
2patrickMurphy 2patrickMurphy

2012/4/17

#
thanks i really didnt think about chaning the while to an if with +5000 seconds. its pretty much the same thing and also i completely rewrote everything making it as efficient as i could think gutting out mostly how everything is being executed. Ill put your idea into it if i still get an error ill repost it with the new code.
You need to login to post a reply.