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

2018/11/12

Counter Problem

Wriggi Wriggi

2018/11/12

#
I'm having a problem with my counter, i with the form of the game being a zombie survival game i can't make it so the counter adds 1 when the bullet makes contact with the zombie.
INSIDE Counter Class

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

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
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;
    
    public Counter(){
    background = getImage();//gets the image from the class
    value = 0;
    target = 0;
    updateImage();
    }
    
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (value < target) {
        
            value ++;
            updateImage();
        }
        else if (value > target) {
        value --;
        updateImage();
        }
    }
    
    public void add(int score){

        target += score;
        
    }
    
    public int getValue(){
    return value;
    
    }
    
    public void setValue(int newValue)
    {
    target = newValue;
    value = newValue;
    updateImage();
    }
    
    public void updateImage()
    {
    GreenfootImage image = new GreenfootImage(background);
    GreenfootImage text = new GreenfootImage("" + value, 22, Color.YELLOW, transparent);
    image.drawImage(text, (image.getWidth()-text.getWidth())/2,
    (image.getHeight()-text.getHeight())/2);
    setImage(image);
    }
    
}



INSIDE Bullet Class

private Counter counter;

    
    public Bullet(Counter pointCounter){

        counter = pointCounter;
        
    }
    public void destroyEnemy(){

      
             
        if(collectItem(Enemy.class)){
            collect(Enemy.class);
            getWorld().removeObject(this);
           
            
        }
        else
        if(atWorldEdge()){getWorld().removeObject(this);}

        
    }

INSIDE PLAYER1 Class

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

/**
 * Write a description of class Player1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player1 extends Characters
{
    private int shotdelay;
    /**
     * Act - do whatever the Player1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        
        keyPress();
        fireBullet();
        turnAtEdge();
        
    }

    
    
    public void keyPress() {

        if(Greenfoot.isKeyDown("left")){
            turn(-5);
        }

        if (Greenfoot.isKeyDown("right")){
            turn(5);
        } 

        if (Greenfoot.isKeyDown("up")){
            move(5);    
        }
    }
private Counter counter;

    
    public Player1(Counter pointCounter){

        counter = pointCounter;
      
    }
    public void fireBullet(){
        if(shotdelay > 0) shotdelay --;

        if (shotdelay == 0 && Greenfoot.isKeyDown("space")){
            Bullet b = new Bullet(counter);
            b.setRotation(getRotation());
            getWorld().addObject(b, getX(), getY());
            b.move(20);
            shotdelay = 20;
            
            
        }

    }



    
    
    public void turnAtEdge()
    {
        if (atWorldEdge())
        {
            turn(5);
        }
    }
    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-5);
        }
        
        if (Greenfoot.isKeyDown("right"))
        {
            turn(5);
        }
    }
    

        public Player1() {
        GreenfootImage image = getImage();
        image.scale(50,100);
        setImage(image);
    }
}




danpost danpost

2018/11/12

#
Wriggi wrote...
I'm having a problem with my counter, i with the form of the game being a zombie survival game i can't make it so the counter adds 1 when the bullet makes contact with the zombie. << Code Omitted >>
What codes are in the collect(Class) and collectItem(Class) methods (called at lines 89 and 88 above)? and where are these methods located? Also, where is your attempt at scoring with the counter?
Wriggi Wriggi

2018/11/12

#
        public void collect(Class seebool)
    {
        Actor actor = getOneObjectAtOffset(0, 0, seebool);
        if(actor != null) {
            getWorld().removeObject(actor);
           
        }
    }
        public boolean collectItem(Class cls) {
        if (getOneIntersectingObject(cls) == null)
            return false;
        getWorld().removeObject(getOneIntersectingObject(cls));
        return true; 
    }
These are located in a characters subclass which is under the Actor class and contains bullet and Player1 and i have tried adding the score to the counter in multiple places such as in bullet, player and the characters class but none of them worked
danpost danpost

2018/11/12

#
Wriggi wrote...
i have tried adding the score to the counter in multiple places such as in bullet, player and the characters class but none of them worked
Show what you tried in the Bullet class.
Wriggi Wriggi

2018/11/12

#
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 Characters
{
    /**
     * 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);
        //edgeOfWorld();
        destroyEnemy();
        
    } 

    public void edgeOfWorld(){
    if(atWorldEdge()){
            
           
    }

    }
    
        
     public void collectEnemy()
    {

        if (canSee(Enemy.class))
        {
            collect(Enemy.class);
            counter.add(1);
        }
        
    }
    
    private Counter counter;

    
    public Bullet(Counter pointCounter){

        counter = pointCounter;
        
    }
    public void destroyEnemy(){

      
             
        if(collectItem(Enemy.class)){
            collect(Enemy.class);
            getWorld().removeObject(this);
            
            
        }
        else
        if(atWorldEdge()){getWorld().removeObject(this);}

        
    }

    


}
danpost danpost

2018/11/12

#
Wriggi wrote...
<< Code Omitted >>
How are you creating a Player1 object in your World subclass?
Wriggi Wriggi

2018/11/19

#
I have it set out with Actor with a sub of Characters which has bullet and player one in it.
danpost danpost

2018/11/19

#
Wriggi wrote...
I have it set out with Actor with a sub of Characters which has bullet and player one in it.
That did not answer my question and has nothing to do with your extension of World.
How are you creating a Player1 object in your World subclass?
Also, I could tell that much from the code already provided -- so that told me nothing new.
Wriggi Wriggi

2018/11/19

#
Ok sorry, i have found my error now anyway. Thanks for the help
You need to login to post a reply.