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

2016/4/3

Score in different worlds

rramos95 rramos95

2016/4/3

#
Hi, i want to put my score actor in three modes of my game, but i can´t do that, its only work in one.
danpost danpost

2016/4/3

#
If you use the class of the score only for the score object, then you could probably just set the int score field as 'static' (make sure your initial world zeroes the score in the constructor so resets include it also. If the actor class is called 'ScoreCounter' and the field with the score is 'score', then:
ScoreCounter.score = 0;
would be sufficient provided the field was made 'public' as well.
rramos95 rramos95

2016/4/3

#
Put this code in the different worlds?
danpost danpost

2016/4/3

#
rramos95 wrote...
Put this code in the different worlds?
Only in your initial world class constructor.
rramos95 rramos95

2016/4/3

#
import greenfoot.*;
import java.util.List;
import java.awt.Color;
import java.awt.Font;
/**
 * Write a description of class Pontuação here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Pontuacao extends Actor
{
    private GreenfootImage imagem;
    private int pontuação = 0;
    private Color CorMenu;
    
    /**
     * Constructor for the score.
     * 
     */
    public Pontuacao()
        {
         imagem = getImage();
         imagem.clear();
         imagem.scale(100,70);
         
         escreve("0");
        }

    
    /**
     * Act - do whatever the Pontuação 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.
        if(getWorld()!=null)
        {
            
            escreve(""+pontuação);
        }
        
    }  
    
    /**
       Método para escrever a pontuação
       */
    public void escreve(String pontos)
    {
        imagem.clear();
        imagem = this.getImage();
        CorMenu = new Color(255,204,153);
        imagem.setColor(CorMenu);
        imagem.fill();
        this.setImage(imagem);
        imagem = getImage( );
        Font letra = new Font("Arial", Font.BOLD,12);
        imagem.setFont(letra);
        imagem.setColor( Color.BLACK );
        int x = 5;
        int y = 20;
        imagem.drawString("Score: "+pontos, x+24, y);
        //imagem.drawString("Highscore: " + pontos ,x,y+20);
    }
    
    public void contaPontos()
    {
        pontuação++;  
    }
}
rramos95 rramos95

2016/4/3

#
That´s my score code, but when i put what you did to put in the world constructor, i can´t.
Super_Hippo Super_Hippo

2016/4/3

#
Your score variable isn't static. If you make pontuação static, the line will be this:
Pontuacao.pontuação = 0;
Btw, you could remove the act method and put line 41 after line 69.
rramos95 rramos95

2016/4/3

#
Thank you so much. Its 100% working!!
You need to login to post a reply.