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

2013/3/23

Drawing Strings

SlothKing SlothKing

2013/3/23

#
 if( losewin == 0)
        {
            GreenfootImage scoreboard = new GreenfootImage (100,30);
            scoreboard.drawString ("You Win! Score: " + scoreKeep, 200, 200);
            setImage (scoreboard);
        }
        if (losewin == 1)
        {
            GreenfootImage scoreboard = new GreenfootImage (100,30);
            scoreboard.drawString ("You Lose! Score: " + scoreKeep, getWorld().getWidth()/2, getWorld().getHeight()/2);
            setImage (scoreboard);
        }
Hello I am having trouble drawing a string to say you lose or win with the score after it I will put the code of my two win and loss scenarios. I have made a seperate class for then and it takes two parameters 0 for win or 1 for loss and the other one is the players score. When I try to run them I get an error but it compiles.
SlothKing SlothKing

2013/3/23

#
public class Score extends Actor
{
    public int scoreKeep = 0;
    /**
     * Act - do whatever the Score wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }  


    public Score(int losewin, int score)
    {
        scoreKeep = score;
        if( losewin == 0)
        {
            GreenfootImage scoreboard = new GreenfootImage (100,30);
            scoreboard.drawString ("You Win! Score: " + scoreKeep, 200, 200);
            setImage (scoreboard);
        }
        if (losewin == 1)
        {
            GreenfootImage scoreboard = new GreenfootImage (100,30);
            scoreboard.drawString ("You Lose! Score: " + scoreKeep, getWorld().getWidth()/2, getWorld().getHeight()/2);
            setImage (scoreboard);
        }

    }
}
Heres the code in my Score class. I forgot to post it all in the first post,
SlothKing SlothKing

2013/3/23

#
 public int scoreKeep = 0;
    /**
     * Constructor for objects of class Platforms.
     * 
     */
    public Platforms()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1,false);

        prepare();
    }
    
    public void winGame()
    {
            addObject(new Score(0,scoreKeep),getWidth()/2,getHeight()/2);
    }
    
    public void loseGame()
    {
       addObject(new Score(1,scoreKeep),getWidth()/2,getHeight()/2);
    }
    
    public void addScore()
    {
        scoreKeep = scoreKeep + 10;
    }
Heres the code from my world class that uses the Score class to create a scoreboard
SlothKing SlothKing

2013/3/23

#
when I pick up a coin in my game it calls the addscore and then when my character dies or makes it to the end it is supposto call on the loseGame().
danpost danpost

2013/3/23

#
The Score objects are probably being added to the world (stop the scenario, right click on the center of your world and see what type of object was clicked on (actor or world); click on 'Inspect' to see what the actual object is). The problem is you are drawing the string at (200, 200) on your image that is only (100, 30) in size. To make the image of the object the size of the world, move all the code in the constructor to the 'addedToWorld' method, except for saving the parameters:
import greenfoot.*;
import java.awt.Color;

public class Score extends Actor
{
    public int losewinKeep, scoreKeep;

    public Score(int losewin, int score)
    {
        losewinKeep = losewin;
        scoreKeep = score;
    }

    public void addedToWorld(World world)
    {
        GreenfootImage scoreboard = new GreenfootImage(world.getWidth(), world.getHeight());
        String text = "";
        if (losewinKeep == 0) text = "You Win!\nScore: " + scoreKeep;
        if (losewinKeep == 1) text = "You Lose!\nScore: " + scoreKeep;
        GreenfootImage txtImg = new GreenfootImage(text, 48, Color.yellow, new Color(0, 0, 0, 0));
        scoreboard.drawImage((scoreboard.getWidth()-txtImg.getWidth())/2, (scoreboard.getHeight()-txtImg.getHeight())/2);
        setImage(scoreboard);
    }
}
I modified your code because drawString would most probably produce text too small for your purposes.
SlothKing SlothKing

2013/3/23

#
Thank you again danpost!
You need to login to post a reply.