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

2014/9/14

Levels

1
2
DarkSoulDemon DarkSoulDemon

2014/9/14

#
In my game I have it so when it reaches then points it goes to level 2 and then on level 2 you have to get 20 points to get to level 3 however when i am on level 2 and get ten points it restarts level to again. Please may someone help me it would be very much appreciated thank you
danpost danpost

2014/9/14

#
Need some code shown. Everything related to the 'level' variable as well as which class and type of class each part is in; also, the code where you increment the score and check for level changing, indicating what class and type of class it is in.
DarkSoulDemon DarkSoulDemon

2014/9/14

#
This is the counter code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;


/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
   public int score;
 
   
  
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
   {
      setImage(new GreenfootImage("Score: "+ score,24, Color.RED, Color.BLACK)); 
      // this sets the image to say Score but then add what ever the score is so it may look like this Score:10 and at the start it looks like Score:0
      NextLevel();
     
      // this calls on the method NextLevel1 so that the code runs 
        NextLevel2();
         // this calls on the method NextLevel2 so that the code runs 
        NextLevel3();
         // this calls on the method NextLevel3 so that the code runs 
}
   public void addScore()
    {
        score++;
        // this code adds a score of 1 each time an Rock is killed
   }
//    public void addScore1()
//    {
//        score1(+3);
//     }
   
//    public void reset() 
//    {
//         score =0;
//    }
   
   public void NextLevel()
   {
        if (score == 10
        // need to make a level varible
        ){
          Greenfoot.setWorld(new L2());
           Greenfoot.stop();
          
          
          // This code changes the back ground to level 2 when 3 points are scored
        } 
      
   }
 
   public void NextLevel2()
   {
      
        if (score == 20){
          Greenfoot.setWorld(new L3());
          //This code changes the back ground to level 3 when 20 points are scored 
         }  
   }
   
    public void NextLevel3()
    {
         if (score == 30){
          Greenfoot.setWorld(new Finished());
          //This code changes the back ground to the finished screen when 30 points are scored
         }  
   }
}import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;


/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
   public int score;
 
   
  
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
   {
      setImage(new GreenfootImage("Score: "+ score,24, Color.RED, Color.BLACK)); 
      // this sets the image to say Score but then add what ever the score is so it may look like this Score:10 and at the start it looks like Score:0
      NextLevel();
     
      // this calls on the method NextLevel1 so that the code runs 
        NextLevel2();
         // this calls on the method NextLevel2 so that the code runs 
        NextLevel3();
         // this calls on the method NextLevel3 so that the code runs 
}
   public void addScore()
    {
        score++;
        // this code adds a score of 1 each time an Rock is killed
   }
//    public void addScore1()
//    {
//        score1(+3);
//     }
   
//    public void reset() 
//    {
//         score =0;
//    }
   
   public void NextLevel()
   {
        if (score == 10
        // need to make a level varible
        ){
          Greenfoot.setWorld(new L2());
           Greenfoot.stop();
          
          
          // This code changes the back ground to level 2 when 3 points are scored
        } 
      
   }
 
   public void NextLevel2()
   {
      
        if (score == 20){
          Greenfoot.setWorld(new L3());
          //This code changes the back ground to level 3 when 20 points are scored 
         }  
   }
   
    public void NextLevel3()
    {
         if (score == 30){
          Greenfoot.setWorld(new Finished());
          //This code changes the back ground to the finished screen when 30 points are scored
         }  
   }
}
DarkSoulDemon DarkSoulDemon

2014/9/14

#
Do you need any more code
danpost danpost

2014/9/14

#
Each time you create a new level world, you are also creating a new Counter object with an initial 'score' value of zero. You can probably make the 'score' field 'static' so it retains the score accumulated in the previous levels (make sure you zero the 'score' value from you initial world constructor -- 'L1'? class -- as resetting the scenario will not zero the field). Remove your act method and place its block of code within the 'add' method after 'score++;'. If you leave the act method as is, you will be recreating the L2 world repeatedly (the score would remain 10 and the first act will recreate the L2 world). Also, you only need to check for level changing when the score itself changes.
DarkSoulDemon DarkSoulDemon

2014/9/14

#
How would i code that. I'm not sure what you mean by static and where to place the methods.
danpost danpost

2014/9/14

#
I gave an example of a 'static' field with the 'level' field in another discussion thread. 'static' fields are created when the project is compiled and are only reset programmatically or when the project is re-compiled. They belong to the class that they reside in. Non-static fields are not created until an object is created from the class, at which point the object is assigned a field with that name.
DarkSoulDemon DarkSoulDemon

2014/9/14

#
I have now made it a static int score
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;


/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
   public  static int score;
 
   
  
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
   {
      setImage(new GreenfootImage("Score: "+ score,24, Color.RED, Color.BLACK)); 
      // this sets the image to say Score but then add what ever the score is so it may look like this Score:10 and at the start it looks like Score:0
      NextLevel();
           // this calls on the method NextLevel1 so that the code runs 
        NextLevel2();
         // this calls on the method NextLevel2 so that the code runs 
        NextLevel3();
         // this calls on the method NextLevel3 so that the code runs 
}
   public void addScore()
    {
        score++;
        // this code adds a score of 1 each time an Rock is killed
   }
//    public void addScore1()
//    {
//        score1(+3);
//     }
   
//    public void reset() 
//    {
//         score =0;
//    }
   
   public void NextLevel()
   {
        if (score == 3
        // need to make a level varible
        ){
          Greenfoot.setWorld(new L2());       
          
          // This code changes the back ground to level 2 when 3 points are scored
        } 
      
   }
 
   public void NextLevel2()
   {
      
        if (score == 20){
          Greenfoot.setWorld(new L3());
          //This code changes the back ground to level 3 when 20 points are scored 
         }  
   }
   
    public void NextLevel3()
    {
         if (score == 30){
          Greenfoot.setWorld(new Finished());
          //This code changes the back ground to the finished screen when 30 points are scored
         }  
   }
}
but now when i get to level2 the game just constantly resets its self
danpost danpost

2014/9/14

#
danpost wrote...
... Remove your act method and place its block of code within the 'add' method after 'score++;'. If you leave the act method as is, you will be recreating the L2 world repeatedly (the score would remain 10 and the first act will recreate the L2 world). Also, you only need to check for level changing when the score itself changes.
This was stated earlier in this thread.
DarkSoulDemon DarkSoulDemon

2014/9/14

#
Thankyou i will try it
DarkSoulDemon DarkSoulDemon

2014/9/14

#
Thank you it worked Thank you so much you are LEGENDARY . Thank you so much just thank you (sorry it took a while for me to understand). Thank you
DarkSoulDemon DarkSoulDemon

2014/9/14

#
danpost you are a LEGEND thankyou
DarkSoulDemon DarkSoulDemon

2014/9/14

#
Just found a problem when i restart the score stays the same as when i died
danpost danpost

2014/9/14

#
danpost wrote...
... You can probably make the 'score' field 'static' so it retains the score accumulated in the previous levels (make sure you zero the 'score' value from you initial world constructor -- 'L1'? class -- as resetting the scenario will not zero the field)...
This was quoted from the same post as the previously quoted text.
DarkSoulDemon DarkSoulDemon

2014/9/14

#
waht is the code for that
There are more replies on the next page.
1
2