Jump to content

Need help with sprite animation in this game.

- - - - -

  • Please log in to reply
3 replies to this topic

#1
TheCompBoy

TheCompBoy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 272 posts
Hello guys..
My problem today is when i was creating my game everything is working fine but (Its a space shooter game like Space Invaders)
But when i wanted the enemies to like explode when they die (Using sprites) i bumped on some problem.. I don't realy know how to make
the game wait one second before it switches to next sprite.. And i can't use Thread.sleep for this because that will freeze the whole game.. And i don't realy know how i would use the timer for this.
I would realy like some help please.. But i also whant to learn from this.
Here is the method for bullet coliding with the enemy:
public void collisionResponse(Entity exe){

		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;

			}

		}

	}

And where i did a comment like //Thread.sleep(500); Thats where i need the sleep or wait..

Thanks!

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
All of these if statements aren't helping you. I think I understand what you're trying to do, you're trying to display five different graphics sequentially to make an explosion effect. This can be done with a simple for loop, except that you want to ensure that the user can see the explosion so the frames must last a certain amount of time, and a for loop with Thread.sleep() can't do that since the Thread.sleep() method will cause the Event Dispatch Thread or some user necessary thread to stop. We need a separate thread, we'll use java.util.Timer and java.util.TimerTask for this, as the Timer classes are on separate threads.

if (health <= 0)
{
  java.util.Timer timer = new java.util.Timer();
  java.util.TimerTask task = new java.util.TimerTask() {
    TimerTask init()
    {
      explosions = new ExplosionType[] { explosion, explosion2, explosion3, explosion4, explosion5 };
      explnum = 0;
      return this;
    }

    @Override
    public void run()
    {
      if (explnum >= explosions.length)
      {
        cancel();
      }
      else
      {
        setGraphic(explosions[explnum++])
      }
    }

    ExplosionType[] explosions;
    int explnum;
  }.init(); // init is a dirty hack to get a ctor with an anonymous class.
  timer.setAtFixedRate(task, 0, 500);
}
I didn't test it but it should work. You may also need to do additional clean-up work in the if block of run(), dependent on the needs of your program.
Wow I changed my sig!

#3
farrell2k

farrell2k

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
I would do something like this: In the class that represents the object that is exploding I would of course create an array of the images, then I'd create a method called explode() which would be called only when a boolean isExploding == true;

Image[] explosionImages = {1.png, 2,png, 3.png};

long timeOnScreenPerImage = 1000; //each images stars on screen for one second.

long lastTimeOnScreen = 0; //when the current image was last shown on screen.

int currentImage = 0; // the current image from explosionImages;

private void explode() {
    if (System.currentTimeMillis() - lastTimeOnScreen < timeOnScreenPerImage)
        return;
    else
        currentImage++

    g.drawImage(currentImage); // You want to draw the currentImage in your draw(paint) method, not here.

    if (currentImage > explosionImages.length)
        currentImage = 0;
}

of you could do something like this in an explode method:

int currentFrame = 0; // the current image in image array.
int frameSpeed = 10 // display each image 10x before switching to the next one.
int timer = 0; //timer to determine when to switch frames.

private void explode() {
   
timer++;
if (timer % frameSpeed = 0) {
    timer = 0;
    currentImage++;
}
if (currentImage > explosionImages.length)
   currentImage = 0;

Then again g.drawImage(currentImage) in a paint method of you game loop.


Then when my ship is exploding, I'd call this explode method in the update portion of my game loop. No threading needed.

#4
TheCompBoy

TheCompBoy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 272 posts
I solved it like this:

This is the method that gets called after it collides with the enemy

    public void collisionResponse(Entity exe){

        health = health - 50;

        if(health <= 0){

            setHitBox(0,0,0,0);

            isDead = true;

            timeOfDeath = System.currentTimeMillis();

        }

    }

And here is the code i put in the update method:

            if(isDead){

                currentTime = System.currentTimeMillis();

                amountOfTimePassedSinceDeath=((currentTimeb -timeOfDeath));


                if(amountOfTimePassedSinceDeath > 800){

                    this.destroy();

                    isDead = false;

                }

                else if(amountOfTimePassedSinceDeath > 700)

                    setGraphic(explosion5);

                else if(amountOfTimePassedSinceDeath > 500)   

                    setGraphic(explosion4);

                else if(amountOfTimePassedSinceDeath > 300)    

                    setGraphic(explosion3);

                else if(amountOfTimePassedSinceDeath > 200) 

                    setGraphic(explosion2);

                else

                    setGraphic(explosion);

            }

Its now working Flawless!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users