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

2013/5/13

GIF Help Fast Please :)

FCG FCG

2013/5/13

#
In my game I have a gif that walk back and forth with a speed of 1, and when it hits a wall the gif changes it speed to -1. How can I make my gif flip it self when it changes speed to -1 and stay that way until it changes it speed again.
danpost danpost

2013/5/13

#
Check the GreenfootImage API documentation for what methods might come in handy, here.
FCG FCG

2013/5/13

#
danpost wrote...
Check the GreenfootImage API documentation for what methods might come in handy, here.
I used the getImage().mirrorHorizontally(); when the speed is -1 but the image keeps flipping. How can I make it stay while the speed =-1.
danpost danpost

2013/5/13

#
That is the correct method, but not the correct place for it. You should have something like this
if ((speed == -1 && getX() < 10) || (speed == 1 && getX() > getWorld().getWidth()-10))
{
    getImage().mirrorHorizontally();
    speed = -speed;
}
FCG FCG

2013/5/13

#
danpost wrote...
That is the correct method, but not the correct place for it. You should have something like this
if ((speed == -1 && getX() < 10) || (speed == 1 && getX() > getWorld().getWidth()-10))
{
    getImage().mirrorHorizontally();
    speed = -speed;
}
I did that and the image didn't flip horizontally. Does it make a difference since the image is a gif?
danpost danpost

2013/5/13

#
It should not make any difference that the image is a gif.
FCG FCG

2013/5/13

#
danpost wrote...
It should not make any difference that the image is a gif.
Then how can I flip it, if the previous way didn't work. Also I'm using the gif class for my gif Image.
danpost danpost

2013/5/13

#
Oh, so you are using an animation! After calling 'super.act();', execute this statement:
if (speed < 0) getImage().mirrorHorizontally();
And remove the image flipping from within the 'if' statement given previously (just negate 'speed').
FCG FCG

2013/5/13

#
danpost wrote...
Oh, so you are using an animation! After calling 'super.act();', execute this statement:
if (speed < 0) getImage().mirrorHorizontally();
And remove the image flipping from within the 'if' statement given previously (just negate 'speed').
Now the image keeps flipping back and forth when it is -1.
danpost danpost

2013/5/13

#
It might be better if you show what code you have so far.
FCG FCG

2013/5/13

#
danpost wrote...
It might be better if you show what code you have so far.
there isn't much of the code
public void addedToWorld(World world) { setImage("dog.gif"); //You must use the setImage method and not just right click + setImage in the GUI. } public void act() { super.act();
im using the gif sub class that is available in Greenfoot: http://www.greenfoot.org/scenarios/260 So I want the image to flip horizontally and stay that way when speed is -1.
danpost danpost

2013/5/14

#
Apparently, as the GifActor class is right now, there is no way to mirror the animation without creating a new 'gif' file with mirrored images in it, as you can only set the 'gif animation' images by using 'setImage(String)'. But, maybe there is a way to modify the class. Add a method to the GifActor class that mirrors all the images in its gif array and call it each time your actor changes direction.
public void mirrorImagesHorizontally()
{
    for (int i=0, i<images.length; i++) images[i].mirrorHorizontally();
}
Then, you would use the following:
if ((speed == -1 && getX() < 10) || (speed == 1 && getX() > getWorld().getWidth()-10))
{
    speed = -speed;
    mirrorImagesHorizontally();
}
FCG FCG

2013/5/14

#
danpost wrote...
Apparently, as the GifActor class is right now, there is no way to mirror the animation without creating a new 'gif' file with mirrored images in it, as you can only set the 'gif animation' images by using 'setImage(String)'. But, maybe there is a way to modify the class. Add a method to the GifActor class that mirrors all the images in its gif array and call it each time your actor changes direction.
public void mirrorImagesHorizontally()
{
    for (int i=0, i<images.length; i++) images[i].mirrorHorizontally();
}
Then, you would use the following:
if ((speed == -1 && getX() < 10) || (speed == 1 && getX() > getWorld().getWidth()-10))
{
    speed = -speed;
    mirrorImagesHorizontally();
}
The images still keep flipping when speed is -1. They don't stay.
danpost danpost

2013/5/14

#
I am sure you caught it, but there was a typo in the 'mirrorImagesHorizontally' method. The 'for' statement has a comma that should be a semi-colon. If you are placing your actor too close (10 or less pixels) to the left or right side of the world, you may get that. If that is the case, you can either change the '10's in the code above to a smaller number or start your character further from the edge of the world. Other than that, I cannot say what the problem is. I have tested the code given in the gif animation scenario you mentioned above and it seems to work properly.
You need to login to post a reply.