• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
TheCompBoy

[JAVA]Explosion animation problem

22 posts in this topic

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

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

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!
0

Share this post


Link to post
Share on other sites
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.
1

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:

[code]
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();
}[/code]

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.
0

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.

[code]

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
}

}
[/code] Edited by beatlefan
1

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!
0

Share this post


Link to post
Share on other sites
[quote name='TheCompBoy' timestamp='1311377670' post='4839147']
Bump
[/quote]

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:
[code]

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
}
[/code]
0

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..
0

Share this post


Link to post
Share on other sites
[quote name='TheCompBoy' timestamp='1311378842' post='4839151']
I will try.. But there is nothing in the java that you created with the update metod "GameTime gametime"
[/quote]


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

[quote name='TheCompBoy' timestamp='1311378842' post='4839151']
[color=#1C2837][size=2]EDIT: Didn't realy work..[/size][/color]
[/quote]

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

0

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.

[code] 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;
}
}
}[/code]
0

Share this post


Link to post
Share on other sites
Well you should make a boolean called Dead, and when health is less than or equal to zero, Dead = true. Then in your Update function have:

[code]
if(Dead)
{
//then implement the update function I wrote
}
[/code]
That's mostly all you have to do. Also when you go to check for collision stuff, you would probably want to check if Dead equals false, and if it does continue checking if there is a collision (you would do this so a bullet, or something, wouldn't do damage to something that's already dead).

If you still can't get that to work you can use timers.
0

Share this post


Link to post
Share on other sites
TheCompBoy, what you are facing is a common problem that people face when new-ish to programming. Once you get it, though, it'll make perfect sense.

Every frame, your object is updated, right?
So when it dies, give it a variable called something like 'isDead'... but don't delete it yet!
At the same time, give it a variable called 'timeOfDeath' and set it to the current time.

Then every time you update it, check if enough time has passed, and then change the graphic.

Look at this update function: (It's in fake code, and you can easily figure out how to convert it to Java)
[code]Update
{
if(isDead == false)
{
setGraphic(normal)
}
else
{
amountOfTimePassedSinceDeath = currentTime - timeOfDeath;

if(amountOfTimePassedSinceDeath > 2.5 seconds)
deleteObject
else if(amountOfTimePassedSinceDeath > 2.0)
setGraphic(explosion5);
else if(amountOfTimePassedSinceDeath > 1.5)
setGraphic(explosion4);
else if(amountOfTimePassedSinceDeath > 1.0)
setGraphic(explosion3);
else if(amountOfTimePassedSinceDeath > 0.5)
setGraphic(explosion2);
else
setGraphic(explosion1);
}
}[/code]
2

Share this post


Link to post
Share on other sites
Alright.. Still im getting some errors from beatlefan's posts since there is no such thing as GameTime in the java library plus i don't realy know what to replace it with.
0

Share this post


Link to post
Share on other sites
[quote name='TheCompBoy' timestamp='1311382153' post='4839169']
Alright.. Still im getting some errors from beatlefan's posts since there is no such thing as GameTime in the java library plus i don't realy know what to replace it with.
[/quote]

I told you what to do a couple posts up...

[quote name='beatlefan']
[code]
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
}
[/code]
[/quote]
0

Share this post


Link to post
Share on other sites
[quote name='beatlefan' timestamp='1311382760' post='4839172']
[quote name='TheCompBoy' timestamp='1311382153' post='4839169']
Alright.. Still im getting some errors from beatlefan's posts since there is no such thing as GameTime in the java library plus i don't realy know what to replace it with.
[/quote]

I told you what to do a couple posts up...

[quote name='beatlefan']
[code]
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
}
[/code]
[/quote]
[/quote]

Exacyly but in your code it says: // then just do what I posted in my last post
And thats why i added what you posted before also..
0

Share this post


Link to post
Share on other sites
[quote name='TheCompBoy' timestamp='1311382856' post='4839173']
Exacyly but in your code it says: // then just do what I posted in my last post
And thats why i added what you posted before also..
[/quote]


???

Well the code should be something like:
[code]
bool done = false;

private long lastTime, currentTime;
private float timeTillNextUpdate = 1f;
public void Update()
{
if(Dead)
{
lastTime = currentTime;
currentTime = System.currentTimeMillis();

timeTillNextUpdate -= ( (currentTime - lastTime) / 1000f);


if ( !done && timeTillNextFrame <= 0)
{
currentFrame++;
if (currentFrame > maxFrames)
done = true;

SetGraphic(picture[currentFrame]);

timeTillNextFrame = 1f;
}
}
}
[/code]

or you could do something similar to Servant of the Lord's code


[code]

private long timeOfDeath, currentTime;
private float amountOfTimePassedSinceDeath;
public void Update()
{

if(isDead == false)
{
setGraphic(normal)
}
else
{
currentTime = System.currentTimeMillis();
amountOfTimePassedSinceDeath = ( (currentTime - timeOfDeath) / 1000f);

if(amountOfTimePassedSinceDeath > 2.5)
deathAnimationDone = true;
else if(amountOfTimePassedSinceDeath > 2.0)
SetGraphic(explosion5);
else if(amountOfTimePassedSinceDeath > 1.5)
SetGraphic(explosion4);
else if(amountOfTimePassedSinceDeath > 1.0)
SetGraphic(explosion3);
else if(amountOfTimePassedSinceDeath > 0.5)
SetGraphic(explosion2);
else
SetGraphic(explosion1);
}
}
[/code] Edited by beatlefan
0

Share this post


Link to post
Share on other sites
This is what i did.. And doesn't work at all.. Not showing any of the sprites.
[code]
public void collisionResponse(Entity exe){
health = health - 50;
if(health <= 0){
isDead = true;

if(isDead){
currentTime = System.currentTimeMillis();
amountOfTimePassedSinceDeath = ( (currentTime - timeOfDeath));

if(amountOfTimePassedSinceDeath > 2500)
deathAnimationDone = true;
else if(amountOfTimePassedSinceDeath > 2000)
setGraphic(explosion5);
else if(amountOfTimePassedSinceDeath > 1500)
setGraphic(explosion4);
else if(amountOfTimePassedSinceDeath > 1000)
setGraphic(explosion3);
else if(amountOfTimePassedSinceDeath > 500)
setGraphic(explosion2);
else
setGraphic(explosion);

}

if(deathAnimationDone){
this.destroy();
}
}
}[/code]
0

Share this post


Link to post
Share on other sites
Well that's cause you have to put everything after 'isDead = true' into an Update function that should be called before you draw everything, because no time (except maybe 1 or 2 milliseconds) will past in your collisionResponse function so the graphic won't change.

Also, make sure you set timeOfDeath.

0

Share this post


Link to post
Share on other sites
[quote name='TheCompBoy' timestamp='1311384361' post='4839178']
This is what i did.. And doesn't work at all.. Not showing any of the sprites.
[code]
public void collisionResponse(Entity exe){
health = health - 50;
if(health <= 0){
isDead = true;
}

if(isDead){
currentTime = System.currentTimeMillis();
amountOfTimePassedSinceDeath = ( (currentTime - timeOfDeath));

if(amountOfTimePassedSinceDeath > 2500)
deathAnimationDone = true;
else if(amountOfTimePassedSinceDeath > 2000)
setGraphic(explosion5);
else if(amountOfTimePassedSinceDeath > 1500)
setGraphic(explosion4);
else if(amountOfTimePassedSinceDeath > 1000)
setGraphic(explosion3);
else if(amountOfTimePassedSinceDeath > 500)
setGraphic(explosion2);
else
setGraphic(explosion);

}



if(deathAnimationDone){
this.destroy();
}
}[/code]
[/quote]

You misplaced the this.destroy();, it will never reach it where you had it. Also the brackets were placed kind of weirdly so I fixed that. I have the feeling that the problem may lie in the update method more so then this method because everything you have written is correct. Make sure that when the thing dies you make sure to set timeOfDeath = System.currentTimeMillis(); otherwise you wouldn't get anything.
2

Share this post


Link to post
Share on other sites
That code shouldn't be in a 'collision response' function. It should be in a 'update' or 'tick' function that runs every frame.

This goes is in your collision response function:
[code]
public void collisionResponse(Entity exe){
health = health - 50;
if(health <= 0){
isDead = true;
timeOfDeath = System.currentTimeMillis();
}
}

[/code]

And [i]this [/i]goes in your 'called every frame' function.
[code]
public void update()
{
if(isDead){
currentTime = System.currentTimeMillis();
amountOfTimePassedSinceDeath = ( (currentTime - timeOfDeath));

if(amountOfTimePassedSinceDeath > 2500)
deathAnimationDone = true;
else if(amountOfTimePassedSinceDeath > 2000)
setGraphic(explosion5);
else if(amountOfTimePassedSinceDeath > 1500)
setGraphic(explosion4);
else if(amountOfTimePassedSinceDeath > 1000)
setGraphic(explosion3);
else if(amountOfTimePassedSinceDeath > 500)
setGraphic(explosion2);
else
setGraphic(explosion);

}

if(deathAnimationDone){
this.destroy();
}
}
}[/code]
1

Share this post


Link to post
Share on other sites
Oh man!! It realy works now.. Thanks dude!! Thousand thanks! Just gona edit the time now so it looks better :D

Thanks again.. You saved my day, I will now try remember this and try do the same thing but in different ways so i learn from it.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0