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

2014/4/29

Problem with pointcounter (reference problem)

kasperk22 kasperk22

2014/4/29

#
Hello guys.. I have been struggling for a while now with getting a PointCounter to work properly. There are two actors, that should "trigger" the PointCounter to add up. In my world, i have created a reference to an object. With this object, there are no problems either, because i can create the object directly in the world. But i also want to somehow create a working reference to an object, that gets created by another object. This i cannot make work correctly. Here are the codes for my game so far: WORLD CODE
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ZombieWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ZombieWorld extends World
{
    int ZombieDelay = 0;
    PointCounter pointCounter = new PointCounter();
    Skydemand skydemand = new Skydemand();
    Køremand køremand = new Køremand(pointCounter);
    Hjerne hjerne = new Hjerne ();
    Zombie zombie = new Zombie();
    GreenfootSound shooting =new GreenfootSound("Shooting.wav");
    /**
     * Constructor for objects of class ZombieWorld.
     */
    public ZombieWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1100, 600, 1); 
        Prepare();
        setPaintOrder(Skydemand.class,Hjerne.class,Køremand.class,Bullet.class,Zombie.class,Splat.class);
    }
    
    public void act()
    {
      ZombieAdd();
    }
    
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void Prepare()
    {
        addObject(hjerne, getWidth()/2, getHeight()/2);
        addObject(skydemand, getWidth()/2+65, getHeight()/2);
        addObject(køremand, getWidth()/2, getHeight()/2-80);
        addObject(pointCounter,50,25);
    }
    
    private void ZombieAdd()
    {
        ZombieDelay++;
        
        if (ZombieDelay>200)
        {
            RandomPlacement();
            ZombieDelay=0;
        }
    }
    
     /**
     * generates the "Zombies" in random places at the edge of the world
     */
    public void RandomPlacement()    
    {      
        int chooser = Greenfoot.getRandomNumber(4);
        if (chooser==0)
        {
        int x = Greenfoot.getRandomNumber(getWidth());    
        int y = 0;
        addObject (new Zombie(),x,y);  
    }
           if (chooser==1)
        {
        int x = Greenfoot.getRandomNumber(getWidth());    
        int y = getHeight();
        addObject (new Zombie(),x,y);  
    }
           if (chooser==2)
        {
        int x = 0;    
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject (new Zombie(),x,y);  
    }
           if (chooser==3)
        {
        int x = getWidth();    
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject (new Zombie(),x,y);  
    } 
    }  
}
MY SHOOTER:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Skydemand here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Skydemand extends SmoothMover
{
    int change = 0;
    int speed = 2;
    GreenfootSound swap = new GreenfootSound("swap.wav");
    GreenfootSound clockwiseMove = new GreenfootSound("ClockwiseMove.wav");
    GreenfootSound counterClockwiseMove = new GreenfootSound("CounterClockwiseMove.wav");
    GreenfootSound shooting =new GreenfootSound("Shooting.wav");
    
    /**
     * Act - do whatever the Skydemand wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      moving();
      checkKeys();
    }   
    
    private void checkKeys()
    {
        String key = Greenfoot.getKey();
          if("space".equals(key))
        {
            fire();
        }
        if ("w". equals(key))
        {
            change=change+180;
            swap.play();
        }
         if ("s". equals(key))
        {
            change=change-180;
            swap.play();
        }
    }
    
    /**
     * moving the object
     */
    private void moving()
    {
        if (Greenfoot.isKeyDown("d")) 
        {
           change=change+speed; 
           clockwiseMove.play();
           clockwiseMove.setVolume(75);
        }
        
        if (Greenfoot.isKeyDown("a"))
        {
           change=change-speed; 
           counterClockwiseMove.play();
           counterClockwiseMove.setVolume(70);
        }
        setRotation(change);  
        setLocation(getWorld().getWidth()/2, getWorld().getHeight()/2);  
        move(65);
    }
    
    /**
     * Fire the cannon
     */
    private void fire()
    {
        ZombieWorld zombieWorld = new ZombieWorld();
        Bullet bullet = new Bullet(zombieWorld.pointCounter);
        getWorld().addObject(bullet,getX(),getY());
        bullet.setRotation(getRotation());
        bullet.move(40.0);
        shooting.play();
    }
    
}
BULLET:
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 Mover
{
    int Detected = 0;
    private int life = Greenfoot.getRandomNumber(5)+20;
    GreenfootSound hit = new GreenfootSound("Hit.wav"); 
    GreenfootSound miss = new GreenfootSound("Miss.wav");
    private PointCounter pointCounter;
    
      public Bullet(PointCounter pointcounter)
    {
        pointCounter = pointcounter;
    }
    
    /**
     * 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(15.0);
        lifeChecker();
    }
        
    private void lifeChecker()
    {
     life--;
     if (life==0)
     {
            getWorld().addObject(new Splat(),getX(), getY());
            getWorld().removeObject(this);
            miss.play();
            miss.setVolume(90);
     }
        
     if (life!=0)//checks whether the object has been removed yet or not.
     {
            checkCollision();
     }
     }
        
         private void checkCollision()
    {
     Actor zombie = getOneIntersectingObject(Zombie.class);
     if (zombie != null)
     {
        getWorld().removeObject(zombie);
        getWorld().addObject (new Splat(), getX(), getY());
        hit.play();
        pointCounter.setValue(pointCounter.getValue() + 1);
        getWorld().removeObject(this);
     }
    }
    
}
   
I guess there are no need for the Pointcounter's code to be written in here, but i can send it, if needed :)
danpost danpost

2014/4/29

#
kasperk22 wrote...
But i also want to somehow create a working reference to an object, that gets created by another object.
This is very vague and incomplete. Please elaborate in detail.
kasperk22 kasperk22

2014/4/30

#
What i want is to safe a reference of the PointCounter in the bullet. But since it's not generated by a world class, but an actor class, i don't know how to safe a reference in the bullets. The reason why i want to safe a reference to the PointCounter in the bullet is because i want to add a point in the pointcounter each time "checkCollision ->zombie!=null". i hope you understand what i mean now... it's pretty hard for me to explain Thanks in advance :-)
davmac davmac

2014/4/30

#
What i want is to safe a reference of the PointCounter in the bullet.
You already have this.
But since it's not generated by a world class, but an actor class, i don't know how to safe a reference in the bullets.
When you create the Bullet (line 76 of SkyDemand class):
        Bullet bullet = new Bullet(zombieWorld.pointCounter);  
... you are passing in the reference to the pointCounter. In this case, it seems to come from the world. However, you can supply any value you like (as long as it is the right type) - you don't need to use 'zombieWorld.pointCounter'.
kasperk22 kasperk22

2014/4/30

#
So, you are saying that the way i create a reference to "køremand" in the world (line 14), i can do it exactly the same way in my shooter, when i create new bullets?
davmac davmac

2014/4/30

#
Well, yes, you can create objects and save a reference to them in your shooter, just as you do in your world. I'm not sure if that's what you really want, as I don't quite understand the problem you are having.
kasperk22 kasperk22

2014/4/30

#
Well, i have a "car" i can control. Each time this car drives into a "zombie" it adds 1 point to my "carpointcounter". and this does work. But i also have a shooter that can shoot bullets. The problem is that every time the bullets hit a "zombie", it does not add any points to my "shooterpointcounter" (yes, there are two different counters, but their codes should be pretty much the same).
kasperk22 kasperk22

2014/4/30

#
But for some reason it still does not work (i am pretty sure there is a reference to PointCounter from bullet, since there are no errors... but it still does not count any points
davmac davmac

2014/4/30

#
Ok, but I don't see this 'shooterpointcounter' in the code you posted above. Can you post your current code?
kasperk22 kasperk22

2014/4/30

#
Ofc, here it is
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 SkydemandPoint 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 SkydemandPoint()  
    {  
        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("P1:" + value, 22, Color.BLACK, transparent);  
        image.drawImage(text, (image.getWidth()-text.getWidth())/2,   
        (image.getHeight()-text.getHeight())/2);  
        setImage(image);  
    }  
}
davmac davmac

2014/4/30

#
I still don't see "shooterpointcounter" ...
danpost danpost

2014/4/30

#
Line 75 in your Skydemand class is creating a NEW world object with a NEW pointcounter. This counter is not the same one that your initial world created and will not hold the same value. You need to get a reference to the pointcounter that is already created in the current world. Change line 75 to:
ZombieWorld zombieWorld = (ZombieWorld)getWorld();
kasperk22 kasperk22

2014/4/30

#
davmac wrote...
I still don't see "shooterpointcounter" ...
yea sorry, i'm a dane and Skydemand is kinda the same as shooter(in danish).. sorry for the mistake
danpost wrote...
Line 75 in your Skydemand class is creating a NEW world object with a NEW pointcounter. This counter is not the same one that your initial world created and will not hold the same value. You need to get a reference to the pointcounter that is already created in the current world. Change line 75 to:
ZombieWorld zombieWorld = (ZombieWorld)getWorld();
oh, i see. yea you are actually right.. creating a new world doesen't help at all.. So each time i need to declare a variable(of a world class), that is the metod i am supposed to use? - and by the way, it works perfectly now. Thanks alot.. appreciate it :)
You need to login to post a reply.