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

2014/3/7

Create image from "Dead" object

miniclausen1995 miniclausen1995

2014/3/7

#
So I am making a game, where your main objective is to shoot birds and I want a blood. That means I want a picture of blood to show whenever i shoot one of the birds
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

This is my bird class
/**
 * Write a description of class Bee here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bird extends AnimatedActor
{
    public int i=1;
    
    public Bird()
    {
        super("bird", ".png", 10);
    }
    
    /**
     * Fly to the left. When we hit the edge, disappear.
     */
    public void act() 
    {
        super.act();  // this takes care of the image animation
        move(-5.0);
        if (atWorldEdge()) {
            getWorld().removeObject(this);
            //((TestWorld) getWorld()).countBird();
            Greenfoot.stop();
        }
    }
    
    public void location ()
    {
       super.setLocation(getX (), getY() -5);
       if (getX() == 0) {
            ((TestWorld) getWorld()).gameOver();
        }
    }

    
    public void pop() 
    {
        Greenfoot.playSound("pop.wav");
        getWorld().removeObject(this);
      
        }
    }
    

And this is my target class, used to shoot the birds
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Target here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Target extends Actor
{
    /**
     * Act - do whatever the Target wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
          if(Greenfoot.mouseMoved(null)) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            setLocation(mouse.getX(), mouse.getY());
        }
        
        // Pop
        if(Greenfoot.mouseClicked(null)) {
            int x = getImage().getWidth()/2;
            int y = getImage().getHeight()/2;
            Bird bird = (Bird) getOneObjectAtOffset(x , y, Bird.class);
            if(bird != null) {
               bird.pop();
              ((TestWorld) getWorld()).countBird();
            }
        }
    }    
    
}
danpost danpost

2014/3/7

#
You should be able to 'addObject' a new Blood object at the location the bird is at in the 'pop' method BEFORE removing 'this' (once you remove 'this', 'getWorld' will start returning 'null', so you will lose reference to the world).
miniclausen1995 miniclausen1995

2014/3/13

#
I found out how to add the image, but I want it to be random if a condition is met, so I made a while loop, which has just caused the images to switch until the game ends.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Blood here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Blood extends Actor

{
    
    /**
     * Act - do whatever the Blood wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       {
        int a = Greenfoot.getRandomNumber(3);
        while (a == 0)
        {
            setImage("blood0.png"); break;
        }
        while (a == 1)
        {
            setImage("blood1.png"); break;
        }
        while (a == 2)
        {
            setImage("blood2.png"); break;
        }
        while (a == 3)
        {
            setImage("blood3.png"); break;
        }
            
       }    
    }
}
danpost danpost

2014/3/13

#
Those are really not loops since you 'break' from them immediately. I would call them un-glorified 'if's. You could change all instances of 'while' to 'if' and remove all the 'break' statements. However, this is still more than is needed. You could simplify the method down to two lines of code within the method block with the following:
public void act()
{
    int a = Greenfoot.getRandomNumber(3);
    setImage("blood"+a+".png");
}
or even to one line with this:
public void act()
{
    setImage("blood"+Greenfoot.getRandomNumber(3)+".png");
}
You need to login to post a reply.