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

2013/8/22

text help

Gingervitis Gingervitis

2013/8/22

#
I am helping a friend with a program for school and the text doesn't get displayed on the screen for some reason.... Here is the label code and the world code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Label here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Label extends Actor
{
    public int score;
    public Label(int intialScore)//int initialScore )
    {
        score = intialScore;
        setImage(new GreenfootImage( 200, 30 ));
        changeScore();
    }
    
    public void act()
    {
        changeScore();
    }

    public void addScore()    
    {
        score++;
        changeScore();
    }

    public void changeScore()
    {
        GreenfootImage img = getImage();
        img.clear();
        //img.setColor( new Color( 100, 100, 100 ));
        img.setColor( Color.BLACK );
        //img.setFont( new Font("Comic Sans MS", Font.BOLD, 24) );
        score++; // add points to current score
        img.drawString( "Damage: " + score , 100, 100);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class SpaceWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SpaceWorld extends World
{

    /**
     * Constructor for objects of class SpaceWorld.
     * 
     */
    //private int DAMAGE = 0;
    private Label label;
    public int intialScore;
    public SpaceWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        prepare();
        
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
    subclass     * objects and add them to the world.
     */
    private void prepare()
    {
        
        Label label = new Label(intialScore);
        addObject(label, 100, 100);
        Spaceship spaceship = new Spaceship(label);
        addObject(spaceship, 234, 205);
 
        EnergyBulllet energybullet = new EnergyBulllet();
        addObject(energybullet, 134, 0);
        EnergyBulllet energybullet2 = new EnergyBulllet();
        addObject(energybullet2, 367, 0);
        

        
    }
    
    public void score()
    {
        label.addScore();
    }
}
Kartoffelbrot Kartoffelbrot

2013/8/22

#
I think you forgot setImage(img); in changeScore() as the last method call. Maybe it will work, if you implement it. P.S.: I had made an image like you user image for greenfoot, too :D
Gingervitis Gingervitis

2013/8/22

#
still doesn't work.....
8bitcarrotjuice 8bitcarrotjuice

2013/8/22

#
Wouldn't it be easier to do this:
//label class
public int score=0;
public void act()
    {
        score++;
        GreenfootImage textImg=new GreenfootImage("Score: "+score, 16, Color.black, new Color(0, 0, 0, 0));
        setImage(textImg);
}

//world class
//add this to your normal world method
public WorldClass()
{
     setPaintOrder(Label.class);
}
Gingervitis Gingervitis

2013/8/22

#
ok that worked, now my friend needs the text to add a point when the rocket hits something... Here is my code but i keep getting null pointer exceptions
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class CrazyCar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Spaceship extends Animal
{    
    /**
     * Act - do whatever the CrazyCar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     private int score= 0;
     private Label label;
     private int damage;
     private boolean ifHit;
    public void act() 
    {
        setRotation(-90);
        turnAtEdge();
        checkKeyPress();
        hit();        
    }   
    
    public Spaceship(Label label)
    
    {
        
    }
    
    public void hit()
    {
        if(canSee(EnergyBulllet.class))
        {            
           ifHit = true;
        }
        
        if (ifHit = true)
        {
            label.addScore();
        }
        else
        {
            return;
        }
    }
    
     public void turnAtEdge()
    {
        if( atWorldEdge()) {turn(17);}
    }
    
    public void randomTurn()
    {
        if (Greenfoot.getRandomNumber(100) < 10 && !atWorldEdge() )
        {
            turn(Greenfoot.getRandomNumber(90)-45);
        }
    }
     public void checkKeyPress()
    {
        if( Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-12,getY());
        }
        if( Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+12, getY());
        }
        if(Greenfoot.isKeyDown("down"))
        {
            move(-14);
        }
        if(Greenfoot.isKeyDown("up"))
        {
            move(10);
        }
    
    }
    

}
danpost danpost

2013/8/22

#
It is probably your label doing that. Try adding 'this.label = label;' at line 31.
Gingervitis Gingervitis

2013/8/22

#
that worked but now the label doesn't get displayed.....
danpost danpost

2013/8/23

#
Did you add it into the world in your World subclass?
Gingervitis Gingervitis

2013/8/23

#
Everything works. Thank you!
You need to login to post a reply.