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

2015/9/26

Help me please to solve my program

noob_tux noob_tux

2015/9/26

#
Hi, i'm new in here. Help me to solve my program. I make my first game, I wanna add score counter in my game, but the counter doesn't work. I hope my score counter work when the bullet from survivor killing the zombie. It's the code from the background world
import greenfoot.*;

/**
 * Write a description of class bg here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bg extends World
{
    
    /**
     * Constructor for objects of class bg.
     * 
     */
    public bg()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
    }
    
    
    
    
    private void prepare()                                          
    {
        Counter counter = new Counter ();
        addObject (counter, 626,586);
        Bullet bullet = new Bullet(counter);
        addObject (bullet, 103,535);
        spawn spawn = new spawn();
        addObject(spawn, 794, 18);
        spawn2 spawn2 = new spawn2();
        addObject(spawn2, 12, 15);
        mansurvivor mansurvivor = new mansurvivor();
        addObject(mansurvivor, 103, 535);
        car car = new car();
        addObject(car, 92,297);
        brick brick = new brick();
        addObject(brick, 764,575);
        brick2 brick2 = new brick2();
        addObject(brick2, 406,372);
        brick3 brick3 = new brick3();
        addObject(brick3, 200,106);
      
        return;
    }
    
}
It's the bullet code
import greenfoot.*;

/**
 * Write a description of class bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends players
{
    private int points = 0;
    private Counter counter;
    public Bullet(Counter pointCounter)
    {
        counter = pointCounter;
    }
    
    public Bullet(int rotation)
    {
        setRotation(rotation);
    }
             
    /**
     * Act - do whatever the bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(10);
        killing();
        remove();
    }    
          
    public void killing ()
    {
        if(canSee(zombie.class))
        {
            kill(zombie.class);
            counter.add(5);
        }
        if(canSee(zombie3.class))
        {
            kill(zombie3.class);
            counter.add(20);
        }
    }
    public void remove()
    {
        setLocation(getX(), getY());
        if(getY()>595)  
        {
            getWorld().removeObject(this);
            if (getWorld() == null) return;
        }
        setLocation(getX(), getY());
        if(getY()<5)  
        {
            getWorld().removeObject(this);
            if (getWorld() == null) return;
        }
        setLocation(getX(), getY());
        if(getX()<5)  
        {
            getWorld().removeObject(this);
            if (getWorld() == null) return;
        }
        setLocation(getX(), getY());
        if(getX()>795)  
        {
            getWorld().removeObject(this);
            if (getWorld() == null) return;
        }
        Actor actor = getOneIntersectingObject(Walls.class);
        if(actor != null)
        {
            getWorld().removeObject(this);
            if (getWorld() == null) return;
        }
        
    } 
    
    
}
and this is code from counter
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A simple counter with graphical representation as an actor on screen.
 * 
 * @author mik
 * @version 1.0
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter()
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Add a new score to the current counter value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return value;
    }

    /**
     * Set a new counter value.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
I hope you can help me. thank you before
danpost danpost

2015/9/26

#
Issues: (1) the Bullet class should extend the Actor class (a Bullet is not a player); (2) an object of the Bullet class should not be created as part of preparing the world; bullets should be created (by the player, I presume) as needed during play; (3) all 'setLocation' lines in the 'remove' method of the Bullet class do absolutely nothing (places the object at the location it currently is located) and can be deleted; (4) all 'if (getWorld() == null) return;' lines in the 'remove' method of the Bullet class can be reduced to 'return;' as the lines previous to them remove the actor from the world (no need to ask something you already know to be true)
noob_tux noob_tux

2015/9/26

#
danpost wrote...
Issues: (1) the Bullet class should extend the Actor class (a Bullet is not a player); (2) an object of the Bullet class should not be created as part of preparing the world; bullets should be created (by the player, I presume) as needed during play; (3) all 'setLocation' lines in the 'remove' method of the Bullet class do absolutely nothing (places the object at the location it currently is located) and can be deleted; (4) all 'if (getWorld() == null) return;' lines in the 'remove' method of the Bullet class can be reduced to 'return;' as the lines previous to them remove the actor from the world (no need to ask something you already know to be true)
Thank you. Now I have problem with collision bullet with walls. Would you help me ?
danpost danpost

2015/9/26

#
noob_tux wrote...
Thank you. Now I have problem with collision bullet with walls. Would you help me ?
Explain and show related code.
noob_tux noob_tux

2015/9/27

#
danpost wrote...
noob_tux wrote...
Thank you. Now I have problem with collision bullet with walls. Would you help me ?
Explain and show related code.
So lucky, I solve my code for collision. thank you very much for always reply my post :)
You need to login to post a reply.