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

2021/2/16

Different behavior in gif appearance depending on distance

BogdanMicu BogdanMicu

2021/2/16

#
Hi! I encountered a problem with a projectile which has to explode on touch with an actor. It has a delay to display the gif when at a long distance from the starting location and when it hits at a middle distance it just doesn't display it at all, or only a few frames after a delay. On short distance though, it works perfectly fine. It's instant and displayed properly. What might be the cause of this problem be? Here's the code for the bullet class:
import greenfoot.*;
public class bossBullet extends Misc
{
    public int speed =10;
    public int counter = 0;
    public int check = 1;
    public int nr = 0;
    public int ok = 1;
    public bossBullet(double mouseRotation)
    {
        setRotation((int)mouseRotation);
    }
    GifImage gif = new GifImage("bossBullet.gif");
    GifImage gif1 = new GifImage("bossBulletExpl.gif");
    public void act()
    {
        if(check==1)
        {
            setImage(gif.getCurrentImage());
            remove();
            move(speed);
            if((isTouching(Odobasian.class) || isTouching(Pepsi.class)) && ok==1)
            {
                ((level)getWorld()).health--;
                check=0; 
            }
        }else explosion();
        if(counter==60)getWorld().removeObject(this);
    }
    public void remove()
    {
        if(isTouching(portal.class))getWorld().removeObject(this);
        else if(isTouching(Surface.class) ){ok=0;explosion();}
        else if(getX()>=getWorld().getWidth() -1){ok=0;explosion();}
        else if(getX()<1){ok=0;explosion();}
        else if(getY()>=getWorld().getHeight() -1){ok=0;explosion();}
        else if(getY()<1){ok=0;explosion();}
    }  
    public void explosion()
    {
        counter++;
        setImage(gif1.getCurrentImage());
        if((isTouching(Odobasian.class) || isTouching(Pepsi.class)) && nr<1)
        {
            ((level)getWorld()).health-=1;
            nr++;
        }
    }
}
danpost danpost

2021/2/16

#
An explosion has a totally different behavior than a bullet. They should be separate classes altogether.
BogdanMicu BogdanMicu

2021/2/16

#
Thanks! That solved it and made the code cleaner, but I'm still not sure why it behaved the way that it did with the initial code. The outcome was dependant on its position, although the code doesn't even mention it or restrict it in any way.
danpost danpost

2021/2/16

#
BogdanMicu wrote...
I'm still not sure why it behaved the way that it did with the initial code. The outcome was dependant on its position, although the code doesn't even mention it or restrict it in any way.
I think it is because you create the explosion gif when you create the bullet -- not when the explosion starts.
BogdanMicu BogdanMicu

2021/2/16

#
danpost wrote...
BogdanMicu wrote...
I'm still not sure why it behaved the way that it did with the initial code. The outcome was dependant on its position, although the code doesn't even mention it or restrict it in any way.
I think it is because you create the explosion gif when you create the bullet -- not when the explosion starts.
That makes much more sense! Thanks! Btw, does the GifImage class have a method to check the frame the gif is on? If not, how could it be done?
danpost danpost

2021/2/16

#
BogdanMicu wrote...
danpost wrote...
BogdanMicu wrote...
I'm still not sure why it behaved the way that it did with the initial code. The outcome was dependant on its position, although the code doesn't even mention it or restrict it in any way.
I think it is because you create the explosion gif when you create the bullet -- not when the explosion starts.
That makes much more sense! Thanks! Btw, does the GifImage class have a method to check the frame the gif is on?
No. It does not.
If not, how could it be done?
By adding one:
public int getCurrentIndex()
{
    return currentIndex;
}
BogdanMicu BogdanMicu

2021/2/16

#
Thanks!
You need to login to post a reply.