I have a landmine class that when touched explodes and I want the explosion to be an animation. I have all the images needed for my animation named tile(frame number).png but i dont know how to cycle through the images to make an animation
Explosion code:
Landmine code:
When the hunter touches the landmine it gives me a NullPointerException
Here is the exact error message:
java.lang.NullPointerException
at Explosion.<init>(Explosion.java:24)
at Landmine.checkHitHunter(Landmine.java:28)
at Landmine.act(Landmine.java:35)
at greenfoot.core.Simulation.actActor(Simulation.java:567)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
at greenfoot.core.Simulation.runContent(Simulation.java:193)
at greenfoot.core.Simulation.run(Simulation.java:183)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Explosion here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Explosion extends Actor
{
/**
* Act - do whatever the Explosion wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
GreenfootImage[] explosionAni;
GreenfootImage[] currAnim;
int animTimer;
public Explosion()
{
//this.getImage().setTransparency(250);
GreenfootImage image = getImage();
for (int i = 0; i < 14; i++)
{
explosionAni[i] = new GreenfootImage("images/tile00" + i + ".png");
explosionAni[i].scale (400, 350);
}
}
public void act()
{
this.getImage().setTransparency(getImage().getTransparency() - 3); //slowly becomes transparent
Hunter hunter = (Hunter)getOneIntersectingObject(Hunter.class);
if (hunter != null)
{
getWorld().removeObject(hunter);
}
/*if (this.getImage().getTransparency() < 5)
{
getWorld().removeObject(this);
}*/
}
private void setAnimation(GreenfootImage[] anim)
{
currAnim = anim;
animTimer = -1;
setImage();
}
private void setImage() //sets image to the current iamge for a certain amount of time
{
animTimer = (animTimer+1)%(10*currAnim.length);
if(animTimer%10 == 0)
{
setImage(currAnim[animTimer/10]);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
/**
* Write a description of class Landmine here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Landmine extends Actor
{
/**
* Act - do whatever the Landmine wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Landmine()
{
GreenfootImage image = getImage();
image.scale(image.getWidth() - 150, image.getHeight() - 120);
setImage(image);
}
public void checkHitHunter()
{
ArrayList<Hunter> h = (ArrayList<Hunter>)getObjectsAtOffset(0,0, Hunter.class);
if (h.size() > 0)
{
getWorld().addObject (new Explosion(), this.getX(), this.getY());
getWorld().removeObject(this);
}
}
public void act()
{
checkHitHunter();
}
}
