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

2016/3/20

Animation code being skipped? or is it just too fast?

Randy. Randy.

2016/3/20

#
If the ship is hit by a laser beam, the following happens...
if(getOneIntersectingObject(TopLaser.class) != null)
        {
            setImage(Explosion1);    
            setImage(Explosion2);
            setImage(Explosion3);
            setImage(Explosion4);
            setImage(Explosion5);
            setImage(Explosion6);
            setImage(Explosion7);
            setImage(Explosion8);
            setImage(Explosion9);
            setImage(Explosion10);
            setImage(Explosion11);
            setImage(Explosion12);
            setImage(Explosion13);
            setImage(Explosion14);
            setImage(Explosion15);
            GameOver gameOver = new GameOver();
            getWorld().addObject(gameOver, getWorld().getWidth()/2, getWorld().getHeight()/2); 
            Restart button = new Restart();
            getWorld().addObject(button, getWorld().getWidth()/2, getWorld().getHeight()/3); 
            getWorld().removeObject(this);
        }
There's no errors, the only issue is you never see the explosion, it just gets removed, is the code being read too fast or is it skipping over the image changing?
danpost danpost

2016/3/20

#
As the code executes, each successive 'setImage' command overrides the previous ones. The only delay between each 'setImage' line is the time it takes the processor to execute the lines. The screen is refreshed between act cycles. By processing all the lines in one act cycle, only the last one will take effect. You need to set one image and have it show for one or more act cycles before changing it to the next for it to have a chance of being visually displayed. You should have a separate class for the explosion (an explosion is not a ship). Start with this:
if(getOneIntersectingObject(TopLaser.class) != null)
{
    getWorld().addObject(new Explosion(), getX(), geetY());
    getWorld().removeObject(this);
}
The world class act method could have this:
if (getObjects(Ship.class).isEmpty() && getObjects(Explosion.class).isEmpty())
{
    addObject(new GameOver(), getWidth()/2, getHeight()/2);
    addObject(new Restart(), getWidth()/2, getHeight()/3);
}
An Explosion class is included in the LunarLander sample scenario that is provided with the greenfoot download, if you wish to look over it.
You need to login to post a reply.