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

2014/1/25

Change font size

1
2
3
kasperk22 kasperk22

2014/1/26

#
I was trying to put the
((Start)getWorld().getObjects(Start.class).get(0)).decScore();  
code into this code
 else if (atWorldEdge())    
        {    
             getWorld().removeObject(this);   
            
        } 
danpost danpost

2014/1/26

#
Well, now it would be
((LifeCounter)getWorld().getObjects(LifeCounter.class).get(0)).decScore();
and it should work if placed first in the 'if' block.
kasperk22 kasperk22

2014/1/27

#
Hmm again it seem to create a NPE...
danpost danpost

2014/1/27

#
Show how you are using it (include some code around it).
kasperk22 kasperk22

2014/1/27

#
MyWorldcode:
import greenfoot.*;    // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
    
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...      
    GreenfootSound backgroundMusic = new GreenfootSound("BACKGROUND.wav");
    LifeCounter lifeCounter = new LifeCounter();
    Counter counter = new Counter();
/**
 * Counter counter = new Counter(); does three things: 
 * it declares a variable (so it can be used later in the code)
 * It sets a java oberator keyword ( this creates the object)
 * The operator is followed by a call to a contructor. This initializes (gives an initial value) the new object
 */
	/** 
     * Creates a world with the size of 1100x500 cells, where every cell is just 1 pixel 
     */  
      
    public MyWorld()  
    {      
        super(1100, 500,1);  
        addObject(counter, 50, 25);  
        background();
        addObject (new Instructions(), 557, 250);
        addObject (lifeCounter, 565, 40); 
    }  
      
/**
 * Each time an act is happening, one is added to a. If a becomes equal to threshold, it will execute RandomPlacement and then set a as 0 again (loop)
 */
    public void act() 
    {    
         a++;    
        if(a == threshold)    
        {   
           RandomPlacement();   
            a = 0;    
        }    
        youmiss();
    }    
    /**
     * initializes x as a random number from 0 to the width of the map abd y as 10. 
     * then an object is generated with reference to counter in order for the object to refer codes to the codes written in counter.
     * The placement of the object is set by the initialized numbers.
     */
    public void RandomPlacement()    
    {      
        int x = Greenfoot.getRandomNumber(getWidth());    
        int y = 10;    
    addObject (new Rock(counter),x,y);  
    }  
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. 
     */  
    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.setValue(counter.getValue() + 1);  
        }  
         else if (atWorldEdge())    
        {    
             getWorld().removeObject(this);   
             ((LifeCounter)getWorld().getObjects(LifeCounter.class).get(0)).decScore();  
            
        } 
    }      
              
    /**
     * Returns true if Y coordinate is less than 10 or greater than the height of the world -10
     * if not it returns false
     */
    public boolean atWorldEdge()  
              
    {       
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)    
            return true;    
        else    
            return false;    
    }    
LifeCounter Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)    
import java.awt.Color;  
public class LifeCounter extends Actor  
{  
    private int score = 3;  
  
    public LifeCounter()  
    {  
        score = 3;  
        update();  
    }  
  
    public void decScore()  
    {  
        score--;  
        update();  
    }  
  
    /**  
     * Return the current counter value.  
     */    
    public int getValue()    
    {    
        return score;    
    }    
  
    /**  
     * Set a new counter value.  
     */    
    public void setValue(int newValue)    
    {    
        score = newValue;    
        update();    
    }    
  
private void update()  
{  
    setImage(new GreenfootImage("Lives: "+score, 20, Color.black, null));  
}  
}  
danpost danpost

2014/1/27

#
danpost wrote...
Well, now it would be
((LifeCounter)getWorld().getObjects(LifeCounter.class).get(0)).decScore();
and it should work if placed first in the 'if' block.
Read the last few words (again?).
kasperk22 kasperk22

2014/1/27

#
But it's not ment to decscore if it's pressed... whenever it gets to the bottom of the world, i want to decrease the score by one :)
kasperk22 kasperk22

2014/1/27

#
ooooh now i understand... of course it need to be before thhe removeObject command, sorry i read it aas it said "if placed in the first "if" block, Sorry for that :P
You need to login to post a reply.
1
2
3