Jump to content
  • Advertisement
Sign in to follow this  
TheCompBoy

[JAVA]Explosion animation problem

This topic is 2526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys!
I've been looking on the internet now after i tried myself for 2 hours without any succes, I tryed with loops i tried with timers.. Nothing worked for me..

My problem is when you kill a monster in my game i whant the to explode.. And for that i need like 5 different pictures but i don't know how to make it like show picture 1 then sleep then show picture 2 then sleep and so on.. Cus if i just write


if(health <= 0){
setGraphic(explosion);
setGraphic(explosion2);
setGraphic(explosion3);
setGraphic(explosion4);
setGraphic(explosion5);
this.destroy();
}


then the pictures will show so fast that the user wont even see that it changes picture..

Please help me! Or show me where i can read about the problem.
Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Well a simplish solution is to add a 'timeTillNextFrame' float variable to your class. Then every time your update function is called just minus the time since the last update from your 'timeTillNextFrame'. Then if it is less than or equal to zero change your explosion sprite.

Also, timeTillNextFrame should be measured in milliseconds.

Share this post


Link to post
Share on other sites
Am not sure im understanding 100% what you mean.. What i whant to do is something like this:


if(health <= 0){
setGraphic(explosion);
sleep(500);
setGraphic(explosion2);
sleep(500);
setGraphic(explosion3);
sleep(500);
setGraphic(explosion4);
sleep(500);
setGraphic(explosion5);
sleep(500);
this.destroy();
}


But i need something else like a timer instead of the sleep.. And i need some help to do it.. Or how i should do it.

Share this post


Link to post
Share on other sites
Well what I told you was kinda a timer. I forgot that Java has timers, so you could use Java's timers, it wouldn't be as efficient because I think it makes a new thread...

This is what I was talking about, it's in C# and XNA, but hopefully you can convert.



public void Update(GameTime gameTime)
{
timeTillNextFrame -= ((float)gameTime.ElapsedGameTime.Milliseconds / 1000f); // converts milliseconds to seconds

if ( !done && timeTillNextFrame <= 0) // If its time to change the picture
{
currentFrame++;
if (currentFrame > maxFrames)
done = true;

SetGraphic(picture[currentFrame]); // This is where you would change your picture

timeTillNextFrame = 1f; // I set it to 1 so that the picture changes once every second
}

}
Edited by beatlefan

Share this post


Link to post
Share on other sites
Hmm alright i will do some research about what timer i need.

EDIT: I found realy no solution i tried soo much.. Some help please!

Share this post


Link to post
Share on other sites

Bump


Why didn't you try what I did, it works fine.

Perhaps you can't figure out how to calculate the time that has past?

Try this:


private long lastTime, currentTime;
public void Update()
{
lastTime = currentTime;
currentTime = System.currentTimeMillis();

timeTillNextUpdate -= ( (currentTime - lastTime) / 1000f); // divide by a thousand to make it into seconds (unless game is updating slowly, it will probably be a fraction of a second)

// then just do what I posted in my last post
}

Share this post


Link to post
Share on other sites
I will try.. But there is nothing in the java that you created with the update metod "GameTime gametime"

EDIT: Didn't realy work..

Share this post


Link to post
Share on other sites

I will try.. But there is nothing in the java that you created with the update metod "GameTime gametime"



That's why I said it was in XNA...


[color=#1C2837][size=2]EDIT: Didn't realy work..


hmm... Well it should, can you post your code?

Share this post


Link to post
Share on other sites
Where i commented Thread.sleep thats were i need the application to sleep without using the "Sleep" because that freezes the whole application.

public void collisionResponse(Entity e){
health = health - 50;
if(health <= 0){
graphic = 1;
if(graphic == 1){
setGraphic(explosion);
//Thread.sleep(500);
graphic++;
}
if(graphic == 2){
setGraphic(explosion2);
//Thread.sleep(500);
graphic++;
}
if(graphic == 3){
setGraphic(explosion3);
//Thread.sleep(500);
graphic++;
}
if(graphic == 4){
setGraphic(explosion4);
//Thread.sleep(500);
graphic++;
}
if(graphic == 5){
setGraphic(explosion5);
//Thread.sleep(500);
this.destroy();
graphic = 0;
}
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!