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

2014/1/24

How to use variables from other actors

kasperk22 kasperk22

2014/1/24

#
Hey guys. I have spend hours on figuring out how to access the variables from my actor called "counter" with my actor called "Rock". The thing is, Rock keeps being generated and i want to add 1 point each time a rock is destroyed. however i can't access the variables from counter (so i can add the point). The way i generate more Rocks is currently by using this code in my world actor: public int a=0; public void act() { a++; if(a == threshold) { RandomPlacement(); a = 0; } } public void RandomPlacement() { int x = Greenfoot.getRandomNumber(getWidth()); int y = 10; addObject(new Rock() ,x,y); //keep in mind that it's decided by random if it adds it or not. } I really need help with this one... please respond :)
kasperk22 kasperk22

2014/1/24

#
oops forgot to add this
private int threshold = 100;
danpost danpost

2014/1/24

#
Please post the rock class code and show how all Counter objects in your world are created.
kasperk22 kasperk22

2014/1/24

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

/**
 * Write a description of class Rock here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rock extends Mover
{
    private static final int NUM_FRAGMENTS = 50;
    private int rotate = Greenfoot.getRandomNumber(9)+1;
    private Counter counter;
   public Rock(Counter pointCounter)
    {
        counter = pointCounter;
    }
    /**
     * Act - do whatever the Rock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(), getY()+1);// the actor's y coordinate placement is added by one each time the act button is pressed.
        setRotation(getRotation () +rotate);// The rotation of Rock is increasing by randomly generated number(1-10) each time act is pressed.
        move();//the rotation affects the x coordinate of Rock. 
        if (Greenfoot.mouseClicked(this)){
            explode();
        }
    }    
    public void explode()
    {
        placeDebris (getX(), getY(),NUM_FRAGMENTS);
        getWorld(). removeObject(this);
      
    }
    private void placeDebris(int x, int y, int numFragments)
    {
        for (int i=0; i < numFragments; i++) {
            getWorld().addObject (new Debris(), x, y );
            
    }
}

}
kasperk22 kasperk22

2014/1/24

#
public void act()  
    {  
         a++;  
        if(a == threshold)  
        {  
             RandomPlacement();
            a = 0;  
        }  
    }  
    public void RandomPlacement()  
    {  
        int x = Greenfoot.getRandomNumber(getWidth());  
        int y = 10;  
        addObject(new Rock() ,x,y); //keep in mind that it's decided by random if it adds it or not. 
    }  
kasperk22 kasperk22

2014/1/24

#
All counter objects in my world are the Rocks :)
danpost danpost

2014/1/24

#
Aren't you getting an error saying something like the following? constructor Rock in class Rock cannot be applied to given types; required: Counter; found: no arguments; reason: actual and formal argument lists differ in length
kasperk22 kasperk22

2014/1/24

#
Yup i am :)
danpost danpost

2014/1/24

#
kasperk22 wrote...
Yup i am :)
Well, that is telling you that you are using 'new Rock()' with no Counter object supplied within the parenthesis. You need to create your Counter object in the world class and use an instance field to hold a reference to that object. Then as you create your rocks, pass a reference to that counter to the Rock class constructor by placing the name of that reference in the parenthesis of the constructor call.
kasperk22 kasperk22

2014/1/24

#
danpost wrote...
kasperk22 wrote...
Yup i am :)
Well, that is telling you that you are using 'new Rock()' with no Counter object supplied within the parenthesis. You need to create your Counter object in the world class and use an instance field to hold a reference to that object. Then as you create your rocks, pass a reference to that counter to the Rock class constructor by placing the name of that reference in the parenthesis of the constructor call.
But i thought i already did that in this code
  public Rock(Counter pointCounter)  
    {  
        counter = pointCounter;  
    } 
danpost danpost

2014/1/24

#
kasperk22 wrote...
But i thought i already did that in this code
  public Rock(Counter pointCounter)  
    {  
        counter = pointCounter;  
    } 
This is the code or method that runs when a new instance of the Rock class is instantiated -- when 'new Rock(Counter)' is called. The counter is passed from the calling statement to this method and the statement in the method saves that counter in an instance field for the new rock being created. No Counter object is being created here. Nowhere is 'new Counter' being used to create one.
kasperk22 kasperk22

2014/1/24

#
Hey... i am having trouble understanding completely what i need to do, so i created a whole new discussion to show the entire code http://www.greenfoot.org/topics/5453
You need to login to post a reply.