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

2017/12/7

Problem with showing the images

qazxsw21 qazxsw21

2017/12/7

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

import java.util.*;

/**
 * An explosion. It starts by expanding and then collapsing. 
 * The explosion will explode other obejcts that the explosion intersects.
 * 
 * @author Poul Henriksen
 * @author Michael Kölling
 * @version 1.1
 */
public class Explosion extends Actor
{
    /** How many images should be used in the animation of the explostion */
    private final static int IMAGE_COUNT= 8;

    /** 
     * The images in the explosion. This is static so the images are not
     * recreated for every object (improves performance significantly).
     */
    private static GreenfootImage[] images;

    /** Current size of the explosion */
    private int imageNo = 0;

    /** How much do we increment the index in the explosion animation. */
    private int increment=1;

    /**
     * Create an explosion.
     */
    public Explosion() 
    {
        initialiseImages();
        setImage(images[0]);    
        GreenfootImage baseImage = new GreenfootImage("explosion-big.png");

    }  

    /**
     * EXPLODE!
     */
    public void act()
    { 
        setImage(images[imageNo]);

        imageNo += increment;
        if (imageNo >= IMAGE_COUNT) {
            increment = -increment;
            imageNo += increment;
        }

        if (imageNo < 0) {
            getWorld().removeObject(this);
        }
    }

    /** 
     * Create the images for explosion.
     */
    public synchronized static void initialiseImages() 
    {
        GreenfootImage baseImage = new GreenfootImage("explosion-big.png");
        if (images == null) {
            //GreenfootImage baseImage = new GreenfootImage("explosion-big.png");
            int maxSize = baseImage.getWidth();
            int delta = maxSize / IMAGE_COUNT;
            int size = 0;
            images = new GreenfootImage[IMAGE_COUNT];
            for (int i=0; i < IMAGE_COUNT; i++) {
                size = size + delta;
                images[i] = new GreenfootImage(baseImage);
                images[i].scale(size, size);
            }
        }
    }

}
So this code should work this way: When the pilot hits the enemy, the enemy explodes and show the explosion, but what's happening is that it does show the explosion, only once a while, and i couldn't find the error, please let me know if you need more information from me to solve this problem
danpost danpost

2017/12/7

#
At what speed is your scenario running (what percent across from left to right is the handle in the speed slider)?
qazxsw21 qazxsw21

2017/12/11

#
Right in the middle.
Super_Hippo Super_Hippo

2017/12/11

#
Try to change line 62 to 'static' and remove lines 35, 37, 65 and 75.
danpost danpost

2017/12/11

#
You can remove line 66 as well (it is a comment and is duplicated a couple lines up as well). To clarify, Hippo suggests that line 62 be just this (only):
static
The code will execute during compilation, when the Class object (Explosion.class) is created. I do not thing that any of these changes will correct your issue, however. It might help if you show the code for the class that creates the objects that explode.
qazxsw21 qazxsw21

2017/12/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * enemy fload along in the bloodstream. They are bad. Best to destroy
 * them if you can.
 * 
 * @author Michael Kölling
 * @version 0.1
 */
public class Enemy extends Actor
{
    private int speed;
    
    /**
     * Constructor. Nothing to do so far.
     */
    public Enemy()
    {
        speed = Greenfoot.getRandomNumber(4)+1;
        
        
    }

    /**
     * Float along the bloodstream, slowly rotating.
     */
    public void act() 
    {
        
        move(-speed);
        
         explode();
         removeEnemy();
        
        

    }
     
    
    private void explode()
    {
        
        if(isTouching(Spaceship.class))
          {
             getWorld().addObject(new Explosion(), getX(), getY());
             getWorld().removeObject(this);
             Greenfoot.playSound("explosion.wav");
             
         }
         
         if (getX() == 0)
        
       {
        getWorld().removeObject(this);
        }
         
        
    }
    
    private void removeEnemy()
    {
        
    
     }
}
qazxsw21 qazxsw21

2017/12/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This is a white blood cell. This kind of cell has the job to catch 
 * enemy and remove them from the blood.
 * 
 * @author Michael Kölling
 * @version 0.1
 */
public class Spaceship extends Actor
{
    private int count = 0;
    
    /**
     * Act: move up and down when cursor keys are pressed.
     */
    public void act() 
    {
        checkKeyPress();

        checktouching();
        eatspaceship();
        endgame();
        getWorld().showText("score:" + count,60,51);
      
    }
    
    /**
     * Check whether a keyboard key has been pressed and react if it has.
     */
    private void checkKeyPress()
    {
        if (Greenfoot.isKeyDown("up")) 
        {
            setLocation(getX(), getY()-5);
        }
        
        if (Greenfoot.isKeyDown("down")) 
        {
            setLocation(getX(), getY()+5);
        }
        if (Greenfoot.isKeyDown("left")) 
        {
            setLocation(getX()-6, getY());
        }
        if (Greenfoot.isKeyDown("right")) 
        {
            setLocation(getX()+6, getY());
        }
    }
    
    private void checktouching()
    {    
      if(isTouching(Enemy.class))
          {
             
             count = count +20;
             
         }
      if(isTouching(coin.class))
      {
              count = count +5;
              Greenfoot.playSound("collectCoins.wav");
             removeTouching(coin.class);
      } 
    }
    
    
   
    public void eatspaceship()
    {
      if(isTouching(innocent.class))
                {
                    count = count - 100;
                    Greenfoot.playSound("CatMeow.wav");
                    removeTouching(innocent.class);
                }
    
    }
    
    public void endgame()
    {
    if(count<0)
    {
    Greenfoot.stop();
    }
    }
}
qazxsw21 qazxsw21

2017/12/14

#
if (getX() == 0) { getWorld().removeObject(this); } This part of the code was in the RemoveEnemy method, i moved it around to test the error.
danpost danpost

2017/12/14

#
Change line 51 in the Enemy class to 'else if ...'.
You need to login to post a reply.