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

2013/7/6

I keep getting an error cant find count++ how do i make my mainPlayer keep score of the coins it earns?

qsapp.3 qsapp.3

2013/7/6

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Main player. Gets controlled by user.
 * 
 * @author (Quinn Sapp) 
 * @version (5 July 2013)
 */
public class MainPlayer extends Animal
{
    private Label myLabel;
    
    public MainPlayer(Label label)
    {
        myLabel = label;
    }
    public void act()
    {
        checkKeyPress();
        lookForPoint();
        checkNextLevel();
        checkNextLevel2();
        checkNextLevel3();
        checkNextLevel4();
        checkNextLevel5();
    }// end act
    public void checkKeyPress(){
        if (Greenfoot.isKeyDown("left")){
            turn(-4);
        }
        if (Greenfoot.isKeyDown("right")){
            turn(4);
        }
        if (Greenfoot.isKeyDown("up")){
            turn(4);
        }
        if (Greenfoot.isKeyDown("down")){
            turn(4);
        }
        if (Greenfoot.isKeyDown("space")){
            move(2);
        }
    }//end checkKeys
    public void lookForPoint(){
       if (canSee(Point.class)){
        eat(Point.class);
        Greenfoot.playSound("au.wav");
        count++;
        myLabel.setText("Count: " + count);
       }
    }  //end lookForPoint
    public void checkNextLevel(){   
       if (getOneIntersectingObject(Finish.class) != null) {  
         //reached the end object;
         //start the new level;  
        Greenfoot.setWorld(new Level2());
   }  
  }//end MainPlayer Class
      public void checkNextLevel2(){   
       if (getOneIntersectingObject(Finish2.class) != null) {  
         //reached the end object;
         //start the new level;  
        Greenfoot.setWorld(new Level3());
   }  
  }
      public void checkNextLevel3(){   
       if (getOneIntersectingObject(Finish3.class) != null) {  
         //reached the end object;
         //start the new level;  
        Greenfoot.setWorld(new Level4());
   }  
  }
      public void checkNextLevel4(){   
       if (getOneIntersectingObject(Finish4.class) != null) {  
         //reached the end object;
         //start the new level;  
        Greenfoot.setWorld(new Level5());
   }  
  }
        public void checkNextLevel5(){   
       if (getOneIntersectingObject(Finish5.class) != null) {  
         //reached the end object;
         //start the new level;  
        Greenfoot.setWorld(new WINNER());
   }  
  }
}
danpost danpost

2013/7/6

#
You need to declare the field in the class (like you declared the Label object called 'myLabel').
private int count;
Zamoht Zamoht

2013/7/6

#
Under: private Label myLabel; Write: private int count; (Didn't see that danpost had answered).
qsapp.3 qsapp.3

2013/7/7

#
Thank you so much!
danpost danpost

2013/7/8

#
@Zamoht, I got in first by milliseconds. I got a notification of your posting on this discussion thread upon completion of the refresh that occurred showing my post was posted. This means that the order of events was (1) my post was posted (2) your post was posted (3) my page was refreshed (with notification of your post on this thread) Notifications, by the way, are one of the last things that are loaded on the page.
You need to login to post a reply.