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

2014/1/24

Hey

kasperk22 kasperk22

2014/1/24

#
My world code
import greenfoot.*;  
  
public class MyWorld extends World  
{  
    public int a = 0;
    private int threshold = 100; //increase to make the time until a random thing is added longer...  
    private int thresholdWave = 3; //increase to make the time until a random thing is added longer... 
    /**
     * Creates a world with the size of 1100x500 cells, where every cell is just 1 pixel
     */
    
    public MyWorld()
    {    
        super(1100, 500,1);
        addObject(new Counter(), 50, 25);
        
    }
    
public void act()  
    {  
         a++;  
        if(a == threshold)  
        { 
           RandomPlacement(); 
            a = 0;  
        }  
    }  
    public void RandomPlacement()  
    {  
        Counter counter = new Counter();
        int x = Greenfoot.getRandomNumber(getWidth());  
        int y = 10;  
    addObject (new Rock(counter),x,y);
    }
    {
    if (Greenfoot.mouseClicked(this)){ // Hvis der er trykket med musen på objektet sker følgende
       
            int x= Greenfoot.getRandomNumber (getWidth());//generer tilfældigt nummer
            int y= Greenfoot.getRandomNumber (getHeight());//generer tilfældigt nummer
            
            
        }
}
}
My Rock code
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();
            counter.rockExploded();
        }
    }    
    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 );
            
    }
}

}
My Counter code
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();
    }
    public void rockExploded()
    {
        add(5);
    }

    /**
     * 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);
    }
}
Zamoht Zamoht

2014/1/24

#
Do you have a question or a problem or did you just want to share your code?
danpost danpost

2014/1/24

#
Remove lines 65 through 68 from the Counter class. Remove lines 13 through 17 from the Rock class. In the 'explode' method of the Rock class, add the following line before the 'removeObject' line:
((Counter)getWorld().getObjects(Counter.class).get(0)).add(5);
This will (1) get the world that the rock is in ('getWorld()'); (2) get a list of all Counter objects in that world ('getObjects(Counter.class)'); (3) get the first element from the list of counters in the world ('get(0)'); (4) type cast the counter object as of type Counter ('(Counter)'); (5) execute the 'add' method with an argument int value of '5' for that counter ('add(5)'); Notice the progression: (1) Actor class method 'getWorld' returns a World object (2) World class method 'getObjects(Class)' returns a List object with elements of Object counters (3) List class method 'get(int)' returns an Object counter (4) Object counter is typecast as a Counter object (5) Counter class method 'add' executes on that counter For (1), 'getWorld' is executed on 'this' by default and this object is an Rock object (as well as an Actor object, since Rock extends Mover and Mover extends Actor (and Actor extends Object)). For (4), the typecasting is necessary to inform the system that the object is indeed an object of the Counter class and methods of that class can be executed on it.
kasperk22 kasperk22

2014/1/24

#
hmm i think i fixed it... thanks
kasperk22 kasperk22

2014/1/24

#
But if I insert the
((Counter)getWorld().getObjects(Counter.class).get(0)).add(5); 
I actually get an nullpointerexception error
danpost danpost

2014/1/24

#
Are you still using 'new Rock()' to create a new rock or are you now passing the Counter object to the Rock constructor? Show the world class code as you have it now.
You need to login to post a reply.