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

2016/11/15

i want to make a destruction animation can you pleas help?

nathen nathen

2016/11/15

#
here is my code so far public void destroyEnemies() { //"Enemy" can be any class that you want the bullet to destroy. Actor enemy = getOneIntersectingObject(bug2.class); if(enemy != null) { getWorld().removeObject(enemy); getWorld().removeObject(this); } } where and how would i put a three pic animation?
danpost danpost

2016/11/15

#
You would need to do that before removing the enemy from the world (if you want the destruction animation to happen at that location). So, something like this:
if (enemy != null) {
    getWorld().addObject(new EnemyDestruction(), enemy.getX(), enemy.getY());
    getWorld().removeObject(enemy);
    getWorld().removeObject(this);
}
nathen nathen

2016/11/15

#
where would the animation go in your code?
danpost danpost

2016/11/15

#
nathen wrote...
where would the animation go in your code?
The animation would be in the EnemyDestruction class.
nathen nathen

2016/11/15

#
how would i wright that part i have 5 images to make in to an animation /how would i code for that animation all i have is this
                 String[] images =  {"expolsion1.png" ,"expolsion2.png","expolsion3.png"};
                 
                
                }
but it doesn't work
danpost danpost

2016/11/15

#
All that does is list the images you wish to use. You need to set each image to the actor at regulated times. This requires an int field to use as a timer. If you want each image to show for five act cycles then each time the timer is a multiple of five, you will change the image (or remove the object once all images have been shown):
private int timer;

public void act()
{
    if (timer == 25) getWorld().removeObject(this);
    else if (timer%5 == 0) setImage("expolsion"+(1+timer/5)+".png");
    timer++;
}
nathen nathen

2016/11/15

#
now my game just flat out dosnt work as soon as the bullet hits the bug the game stops
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bullet extends moverBullet
{
    private int timer;
      public void act()
      {
       setLocation(getX(),getY()-3);
        if(getY()<5)
        {
            getWorld().removeObject(this);
        }
        else if(isTouching (bug2.class))
        {
             if (isTouching(bug2.class))
             {
                 
              if (timer == 25) getWorld().removeObject(this);
              else if (timer%5 == 0) setImage("expolsion"+(1+timer/5)+".png");
              timer++;
      
                   
                
                }
             
          
             
           
             getWorld().removeObject(this);
        }
    }
   
}
nathen nathen

2016/11/15

#
nvm i fixed it but now i have the problem of the animation not working and the bug comes back
      private GreenfootImage image1 = new GreenfootImage ("bug2.png"); 
    private GreenfootImage image2 = new GreenfootImage ("bug22flap.png");
           private int speed = 2;
    private int leftTurn = 100;
    private int rightTurn = 480;
     int counter =0;
     private int timer;
    /**
     * Move in the direction we are currently moving in. Turn if we reach a turning point.
     */
    public void act()
    {
        
        
        setLocation ( getX() + speed, getY() );
        counter++;
        if(counter % 45 ==0){
            if (getImage().equals(image1)){
            setImage (image2);
          }
          else{
            
            
                setImage (image1);
        
          }
        }
         
        
    
        
        Actor actor = getOneIntersectingObject(null);
        if(actor != null) {
            actor.setLocation ( actor.getX() + speed, actor.getY() );
        }
        
        if (atTurningPoint()) {
            speed = -speed;
        }
        
        
        

           
       if (isTouching(bullet.class))
             {
                 
              if (timer == 25) getWorld().removeObject(this);
              else if (timer%5 == 0) setImage("explosion"+(1+timer/5)+".png");
              timer++;
      
                   
                
                }
 
        
        
        
    
    
      
        
        
    }
    
        /**
        * Test if we are at one of the turning points.
        */
      
       public boolean atTurningPoint()
       {
        return (getX() <= leftTurn || getX() >= rightTurn);
       }
danpost danpost

2016/11/15

#
I does not go in the bullet class nor the Bug class -- it goes in the EnemyDestruction class (a subclass of Actor just for the explosion animation).
nathen nathen

2016/11/15

#
so i did what you said and added a new actor which is the animation but when i go to implement the code i get an error
private int timer;
    private GreenfootImage enemy = new GreenfootImage ("bug1.png");
      public void act()
      {
       setLocation(getX(),getY()-3);
        if(getY()<5)
        {
            getWorld().removeObject(this);
        }
        
       if (isTouching(bug1.class)) {
       getWorld().addObject(new explosionanimation(), enemy.getX(), enemy.getY());
       getWorld().removeObject(enemy);
       getWorld().removeObject(this);
      }
    }
danpost danpost

2016/11/16

#
What is the error you are getting? Show entire error output.
You need to login to post a reply.