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

2018/6/11

how can i check if my scoreboard's score is higher than needed

roonie01 roonie01

2018/6/11

#
import greenfoot.*;  

public class ScoreBoard extends Actor
{
    // GLOBAL DECLARATIONS
    // -------------------
    FontImage pic;
    int score=0;

    // CONSTRUCTOR   // runs one time only
    // -----------
    public ScoreBoard()    
    {
        pic = new FontImage("score:"+score,36,Color.BLUE,new Color(0,0,0,0),"Square.ttf");
        setImage(pic);
    } // end constructor

    public void increaseScore(int newPts)
    {//increases score 
        score=score+ newPts;
        pic = new FontImage("score:"+score,36,Color.BLUE,new Color(0,0,0,0),"Square.ttf");
        setImage(pic);
    }   //end increase score

   
    // ACT METHOD   // runs repeatedly
    // ----------
    public void act() 
    {

    } // end act

} // end class
//and this is my actor who is affected
import greenfoot.*;
public class Survivor extends Actor
{
    // GLOBAL DECLARATIONS
    // -------------------
    int speed = 6;
    int fn=0;  // fn - frame number: TRACK ARRAY INDEX FOR CURRENT PICTURE 
    int dn =0; //dn is direction number first index in 2nd array
    int actCount =0;
    int score=0;
    static ScoreBoard sb;
    static MyWorld w;
    static stage2 w2;
    static stage3 w3;
    GreenfootImage[][] images = new GreenfootImage[2][3];

    // CONSTRUCTOR   // runs one time only 
    // -----------
    public Survivor()    
    {         
        // load images into an array of pics
        // ---------------------------------
        for(int i=0; i<3; i++)
        {
            images[0][i] = new GreenfootImage("playerRight"+i+".png");
        } // end for
        for(int i=0; i<3; i++)
        {
            images[1][i] = new GreenfootImage("playerLeft"+i+".png");
        } // end for
        // set the current image
        // ----------------------
        setImage(images[0][0]);  

    } // end constructor

    // ACT METHOD   // runs repeatedly
    // ----------
    public void act() 
    {
        setLocation(getX(),getY());
        checkForDirectionalControll();
        isTagged();
        switchWorld();

    } // end act

    public void checkForDirectionalControll()
    {

        actCount++;
        // keyboard control
        // -----------------
        if(Greenfoot.isKeyDown("right")&&actCount>=10)
        {
            setLocation(getX()+speed, getY());
            dn = 0; // dn 0 for right 1 for left
            switchImage();
            actCount =0;
        } // end if
        if(Greenfoot.isKeyDown("left")&&actCount>=10) 
        {
            setLocation(getX()-speed, getY());

            dn = 1;
            switchImage();
            actCount =0;
        } // end if
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-speed);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+speed);
        }
        //end checkForDirectionalControll
    } 

    public void switchImage()
    {
        // increase frame number, then make sure we haven't gone off the edge of the array
        // -------------------------------------------------------------------------------
        fn++;
        if(fn>=3)
        {

            fn=0;
        } // end if

        // set the image of the actor to the new pic
        // ------------------------------------------
        setImage(images[dn][fn]);   

    } // end switchImage

    public void isTagged()
    {
        if(isTouching(Zombie.class))
        {
            removeTouching(Zombie.class);

            w = (MyWorld)getWorld();  
            sb = w.getScoreboard();
            sb.increaseScore(1000);
        }
    }

    public void switchWorld()
    {   
       
        if(getWorld()instanceof MyWorld)
        {
            sb = w.getScoreboard();
            if(sb>=10100) 
            {
                Greenfoot.setWorld(new stage2());
                score=0;
            }
        }
        if(getWorld()instanceof stage2)
        {
            sb = w.getScoreboard();
            if(sb.getScore>=100100)
            {
                Greenfoot.setWorld(new stage3());
            }
        }

    }
}
roonie01 roonie01

2018/6/11

#
can i get some help checking the scoreBoard
You need to login to post a reply.