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

2012/2/3

how can i change sprites in game on collision

1
2
craigv craigv

2012/2/3

#
i want the image to switch to one explosive sprite to another explosive sprite right after then remove itself from the world, when a different object strikes with this one and triggers the effects. Any help is great, thanks
Morran Morran

2012/2/3

#
Craigv, you'll need two more variables, an "int" called "frame" to keep track of the animation, and a "boolean" called "destroyed" to keep track of whether this object has been destroyed or not. After you've added those, you try this:
public void act()
{
 if(!destroyed) {
  //put your normal act() method here...
 }
 else {
  explode();
 }
}
private void explode()
{
 frame++;
 if(frame == 1)
   setImage(yourFirstSprite);
 else if(frame == 4)
   setImage(yourSecondSprite); //you can have as many frames to the animation as you like.
 else if(frame == 7)
   setImage(yourThirdSprite);
 else if(frame == 10)  //remove self after the explosion animation is over
   getWorld().removeObject(this);
}
And then when you check for collisions, try this: use whatever collision check you normally use, only, when you actually get a collision, set "destroyed" equal to true.
craigv craigv

2012/2/6

#
when i'm writing setImage(firstSprite); theres an error message : cannot find symbol - variable firstSprite whats a way to fix this error?
Morran Morran

2012/2/6

#
Hey CraigV, the problem is that I "firstSprite" typed in first sprite as just a place holder. I did'nt know what the name of your first explosion image was, so I just put in firstSprite. If your first explosion image is "Explode.png" than use that in place of "firstSprite". If your second image's name is "ExplosionFinished.png" use that in place of "yourSecondSprite". If you only plan to use two explosion images, and not three, just delete these lines of code
  
 else if(frame == 7)  
   setImage(yourThirdSprite);  
If you plan to use a third image for your explosion, just replace "yourThirdSprite" with whatever the name of your third sprite is. One more thing, remember to include the type of file (.png, .bmp, .jpg) at the end of your image's name.
craigv craigv

2012/2/8

#
Hey, I know it was a place holder, when I switched it to what my sprite was called I still get the error. No matter what I do I get the error message that it can not find the variable castle! :( to create the sprite i just create a new actor with a new image right? because thats what i'm doing, and with that and the code, its just not working out.
danpost danpost

2012/2/8

#
I do not see where the variable 'castle' comes into play. You have not provided enough information to allow us to help in regards to this error. Post the code about which the error occurs.
Morran Morran

2012/2/8

#
The image is called "castle"? It isn't that Greenfoot is not finding the image, it's just that you're not using the right syntax. When you call setImage(), the name of your image should be put in between quotation marks, and it should have at the end the file format. Ex.
setImage("castle.png");
You need the quotation marks so that Greenfoot knows that it's a name and not a variable that you are using. Get back to me if there's something wrong still.
craigv craigv

2012/2/10

#
hey, so I got the setImage to work, but now i'm encountering another problem. when my ball hits another ball, the sprite animation will begin, but will stop midway and wont finish and remove. I changed the code a bit because I didnt properly set up the boolean portion of this , because that confuses me still haha. anyways my code is this:
{
        Actor collided;
         collided = getOneIntersectingObject(ball.class); 
        if (collided!=null)
        {
        frame++;
        if(frame == 1)
        setImage("sprite1.png");
        else if(frame == 3)
        setImage("sprite2.png");
        else if(frame == 5)
        setImage("sprite3.png");
        else if(frame == 7)
        setImage("sprite4.png");
        else if(frame == 9)
        setImage("sprite5.png");
        else if(frame == 11)
        getWorld().removeObject(this);
        }
danpost danpost

2012/2/10

#
Remove lines 5 and 19 and see what happens! EDITED: On second thought how does frame increase after the collision is over and the animation needs to continue? You should probably use the code Morran provided above with the act() and explode() methods. You just need
private int frame = 0;
private boolean destroyed = false;
public void act()
...
private void explode()
...
craigv craigv

2012/2/10

#
nope same problem occuring, is it because it will only work when the two objects are intersecting?
danpost danpost

2012/2/10

#
Edited last post. Lines 3 through 6 represent the code Morran provided.
danpost danpost

2012/2/10

#
Also in your act, you would have
        Actor collided;
        collided = getOneIntersectingObject(ball.class); 
        if (collided!=null)
        destroyed = true;
danpost danpost

2012/2/10

#
I would re-word the act() part to
if (!destroyed && getOneIntersectingObject(ball.class) != null) destroyed = true;
if (destroyed)
{
    explode();
    return;
}
//  The code for when not destroyed goes here
craigv craigv

2012/2/10

#
i get an illegal start of expression on
if (!=destroyed && getOneIntersectingObject(ball.class) != null) destroyed = true;
the red indicator is in between the ( and ! at the start of the line
danpost danpost

2012/2/10

#
Hold on.
There are more replies on the next page.
1
2