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

2014/9/20

Problem with resetting score to zero

1
2
3
DarkSoulDemon DarkSoulDemon

2014/9/21

#
The Start class is the start screen tells you all the the instructions with no score field you press are to get to level1 and play the game and thank you for the information about rock12 and rock13 i will change that. I will try adding privatw at the beginning of line 13
danpost danpost

2014/9/21

#
So, you should have all your levels extending the Start class now. I noticed you have a Counter as well as the 'score' field. If the Counter class has an int value or int score declared in it, then again you are using multiple fields for the same value. Your Start class would be the appropriate location for your Counter field and any attempt to access the score field should be through the Counter object assigned to that field.
DarkSoulDemon DarkSoulDemon

2014/9/21

#
Still did not work
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
  
// i need to make an level varible
/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level1 extends Start
{
     Counter counter = new  Counter();
     private static int score;     
         
    public void act()  
    {  
     
    }  
     /**
     * Constructor for objects of class Space.
     * score=0;
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super();  
        prepare();
        score = 0; 
        
      
    } 
    public Counter getCounter()
     {
       return counter;
 
    }
 
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Rocket rocket = new Rocket();
        addObject(rocket,14, 243);

        Rock rock =new Rock();
        addObject(rock, 673, 344);

        Rock rock2 = new Rock();
        addObject(rock2, 638, 55);

        Rock rock3 = new Rock();
        addObject(rock3, 672, 265);

        Rock rock4 = new Rock();
        addObject(rock4, 363, 225);

        Rock rock5 = new Rock();
        addObject(rock5, 697, 98);

        Rock rock6 = new Rock();
        addObject(rock6, 793, 269);

        Rock rock7 = new Rock();
        addObject(rock7, 832, 425);

        Rock rock8 = new Rock();
        addObject(rock8, 468, 348);

        Rock rock9 = new Rock();
        addObject(rock9, 523, 202);

        Rock rock10 = new Rock();
        addObject(rock10, 382, 69);

        Rock rock11 = new Rock();
        addObject(rock11, 380, 422);

        Rock rock12 = new Rock();
        addObject(rock12, 478, 115);

        Rock rock13 = new Rock();
        addObject(rock13, 477, 115);

        Rock rock14 = new Rock();
        addObject(rock14, 301, 90);

        Spawn spawn = new Spawn();
        addObject(spawn, 978, 486);

        addObject(counter, 47, 61);
        
   
    }
}
danpost danpost

2014/9/21

#
Move the Counter field and the 'getCounter' method to your Start class and remove everything related to the 'static int score' field from the Level1 class.
danpost danpost

2014/9/21

#
From there, we can work on accessing the 'score' field from your actor classes, which should end up to be something like this within your code blocks:
Start world = (Start)getWorld();
Counter counter = world.getCounter;
counter.methodName(arguments);
where 'methodName' could be any public method in the Counter class ("add', 'getValue' or 'getScore', 'setValue' or 'setScore', etc.) and 'arguments', of course, is the list of data the method requires.
DarkSoulDemon DarkSoulDemon

2014/9/21

#
This is the code for Start.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Start here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Start extends World
{
static int score;
 Counter counter = new  Counter();
    /**
     * Constructor for objects of class Start.
     * 
     */
    public Start()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(475, 350, 1); 
        score=0;
        prepare();
    }
     public Counter getCounter()
     {
       return counter;
 
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Reset reset = new Reset();
        addObject(reset, 464, 347);
        Dialogs dialogs = new Dialogs();
        addObject(dialogs, 473, 342);
    }
}
danpost danpost

2014/9/21

#
You can remove line 11 and line 21 from the Start class (the score is saved within the Counter object). Also, you should set the Counter field from the Level1 class, not in the Start class. Set it to a new Counter object in the Level1 constructor before calling 'prepare':
public Level1()
{
    super();
    counter = new Counter();
    prepare();
}
This will ensure that a new Counter (and score with value of zero) is created when the first level starts. You can then make the Counter field in the Start class a 'static' field so the Counter object is not lost when changing levels:
static Counter counter;
As long as your Counter field is 'public' (which is the default when the access modifier is not explicitly given), you can access the Counter object from any class in your project by simply using 'Start.counter'. So, doing something like this would be possible:
Start.counter.add(10);
DarkSoulDemon DarkSoulDemon

2014/9/21

#
This comes up when i try to compile non-static variable counter cannot be referenced from a static context <> the part in between here is where the message appears how would that make it that when i reset it goes to 0
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
  
// i need to make an level varible
/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level1 extends Start
{
//      Counter counter = new  Counter();
     private static int score;  
     static Counter counter;
         
    public void act()  
    {  
     
    }  
     /**
     * Constructor for objects of class Space.
     * score=0;
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super();  
        prepare();
        Counter counter = new  Counter();
        Start<.counter.>add(1); 
        
      
    } 
    public Counter getCounter()
     {
       return counter;
 
    }
 
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Rocket rocket = new Rocket();
        addObject(rocket,14, 243);

        Rock rock =new Rock();
        addObject(rock, 673, 344);

        Rock rock2 = new Rock();
        addObject(rock2, 638, 55);

        Rock rock3 = new Rock();
        addObject(rock3, 672, 265);

        Rock rock4 = new Rock();
        addObject(rock4, 363, 225);

        Rock rock5 = new Rock();
        addObject(rock5, 697, 98);

        Rock rock6 = new Rock();
        addObject(rock6, 793, 269);

        Rock rock7 = new Rock();
        addObject(rock7, 832, 425);

        Rock rock8 = new Rock();
        addObject(rock8, 468, 348);

        Rock rock9 = new Rock();
        addObject(rock9, 523, 202);

        Rock rock10 = new Rock();
        addObject(rock10, 382, 69);

        Rock rock11 = new Rock();
        addObject(rock11, 380, 422);

        Rock rock12 = new Rock();
        addObject(rock12, 478, 115);

        Rock rock13 = new Rock();
        addObject(rock13, 477, 115);

        Rock rock14 = new Rock();
        addObject(rock14, 301, 90);

        Spawn spawn = new Spawn();
        addObject(spawn, 978, 486);

        addObject(counter, 47, 61);
        
   
    }
}
danpost danpost

2014/9/21

#
danpost wrote...
Move the Counter field and the 'getCounter' method to your Start class and remove everything related to the 'static int score' field from the Level1 class.
For one thing, you did not do this. For another, it does not look like you did the following properly:
danpost wrote...
You can remove line 11 and line 21 from the Start class (the score is saved within the Counter object). Also, you should set the Counter field from the Level1 class, not in the Start class. Set it to a new Counter object in the Level1 constructor before calling 'prepare':
public Level1()
{
    super();
    counter = new Counter();
    prepare();
}
This will ensure that a new Counter (and score with value of zero) is created when the first level starts. You can then make the Counter field in the Start class a 'static' field so the Counter object is not lost when changing levels:
static Counter counter;
As long as your Counter field is 'public' (which is the default when the access modifier is not explicitly given), you can access the Counter object from any class in your project by simply using 'Start.counter'. So, doing something like this would be possible:
Start.counter.add(10);
DarkSoulDemon DarkSoulDemon

2014/9/22

#
I have done this but know when i press "R" the game pauses
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
  
// i need to make an level varible
/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level1 extends Start
{
    //Counter counter = new  Counter();
//      private static int score;  
     static Counter counter;
//        Start.counter.add(10);     
    public void act()  
    {  
     
    }  
     /**
     * Constructor for objects of class Space.
     * 
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super();  
        prepare();
        counter = new  Counter();
      
        
      
    } 
    public Counter getCounter()
     {
       return counter;
 
    }
 
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Rocket rocket = new Rocket();
        addObject(rocket,14, 243);

        Rock rock =new Rock();
        addObject(rock, 673, 344);

        Rock rock2 = new Rock();
        addObject(rock2, 638, 55);

        Rock rock3 = new Rock();
        addObject(rock3, 672, 265);

        Rock rock4 = new Rock();
        addObject(rock4, 363, 225);

        Rock rock5 = new Rock();
        addObject(rock5, 697, 98);

        Rock rock6 = new Rock();
        addObject(rock6, 793, 269);

        Rock rock7 = new Rock();
        addObject(rock7, 832, 425);

        Rock rock8 = new Rock();
        addObject(rock8, 468, 348);

        Rock rock9 = new Rock();
        addObject(rock9, 523, 202);

        Rock rock10 = new Rock();
        addObject(rock10, 382, 69);

        Rock rock11 = new Rock();
        addObject(rock11, 380, 422);

        Rock rock12 = new Rock();
        addObject(rock12, 478, 115);

        Rock rock13 = new Rock();
        addObject(rock13, 477, 115);

        Rock rock14 = new Rock();
        addObject(rock14, 301, 90);

        Spawn spawn = new Spawn();
        addObject(spawn, 978, 486);

        addObject(counter, 47, 61);
        
   
    }
}
This is the code for R
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
  
// i need to make an level varible
/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level1 extends Start
{
    //Counter counter = new  Counter();
//      private static int score;  
     static Counter counter;
//        Start.counter.add(10);     
    public void act()  
    {  
     
    }  
     /**
     * Constructor for objects of class Space.
     * 
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super();  
        prepare();
        counter = new  Counter();
      
        
      
    } 
    public Counter getCounter()
     {
       return counter;
 
    }
 
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Rocket rocket = new Rocket();
        addObject(rocket,14, 243);

        Rock rock =new Rock();
        addObject(rock, 673, 344);

        Rock rock2 = new Rock();
        addObject(rock2, 638, 55);

        Rock rock3 = new Rock();
        addObject(rock3, 672, 265);

        Rock rock4 = new Rock();
        addObject(rock4, 363, 225);

        Rock rock5 = new Rock();
        addObject(rock5, 697, 98);

        Rock rock6 = new Rock();
        addObject(rock6, 793, 269);

        Rock rock7 = new Rock();
        addObject(rock7, 832, 425);

        Rock rock8 = new Rock();
        addObject(rock8, 468, 348);

        Rock rock9 = new Rock();
        addObject(rock9, 523, 202);

        Rock rock10 = new Rock();
        addObject(rock10, 382, 69);

        Rock rock11 = new Rock();
        addObject(rock11, 380, 422);

        Rock rock12 = new Rock();
        addObject(rock12, 478, 115);

        Rock rock13 = new Rock();
        addObject(rock13, 477, 115);

        Rock rock14 = new Rock();
        addObject(rock14, 301, 90);

        Spawn spawn = new Spawn();
        addObject(spawn, 978, 486);

        addObject(counter, 47, 61);
        
   
    }
}
DarkSoulDemon DarkSoulDemon

2014/9/22

#
Sorry reread what you said but i don't know where to place tart.counter.add(10); this is what i have done but i still have the same problem
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
  
// i need to make an level varible
/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level1 extends Start
{
    //Counter counter = new  Counter();
//      private static int score;  
      static Counter counter;
//      Start.counter.add();
//        Start.counter.add(10);     
    public void act()  
    {  
     
    }  
     /**
     * Constructor for objects of class Space.
     * 
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(); 
        counter = new  Counter();
        prepare();
        
      
        
      
    } 
    public Counter getCounter()
     {
       return counter;
 
    }
 
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Rocket rocket = new Rocket();
        addObject(rocket,14, 243);
        

        Rock rock =new Rock();
        addObject(rock, 673, 344);

        Rock rock2 = new Rock();
        addObject(rock2, 638, 55);

        Rock rock3 = new Rock();
        addObject(rock3, 672, 265);

        Rock rock4 = new Rock();
        addObject(rock4, 363, 225);

        Rock rock5 = new Rock();
        addObject(rock5, 697, 98);

        Rock rock6 = new Rock();
        addObject(rock6, 793, 269);

        Rock rock7 = new Rock();
        addObject(rock7, 832, 425);

        Rock rock8 = new Rock();
        addObject(rock8, 468, 348);

        Rock rock9 = new Rock();
        addObject(rock9, 523, 202);

        Rock rock10 = new Rock();
        addObject(rock10, 382, 69);

        Rock rock11 = new Rock();
        addObject(rock11, 380, 422);

        Rock rock12 = new Rock();
        addObject(rock12, 478, 115);

        Rock rock13 = new Rock();
        addObject(rock13, 477, 115);

        Rock rock14 = new Rock();
        addObject(rock14, 301, 90);

        Spawn spawn = new Spawn();
        addObject(spawn, 978, 486);

        addObject(counter, 47, 61);
        
   
    }
}
danpost danpost

2014/9/22

#
I will not ask again. Remove line 14 and line 35 through 39 from the Level1 class. These lines of code belong in the 'Start' class, not in the 'Level1' class. Then, post your revised 'Start' and 'Level1' classes for further instruction, if needed. The 'Start.counter.add(10);' line was just an example of its use -- not something you need to worry about until you get your counter accessible and working.
DarkSoulDemon DarkSoulDemon

2014/9/22

#
Okay i have removed the lines i believe This is the Start code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Start here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Start extends World
{
static int score;
      static Counter counter;

    /**
     * Constructor for objects of class Start.
     * 
     */
    public Start()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(475, 350, 1); 
        score=0;
        prepare();
    }
     public Counter getCounter()
     {
       return counter;
 
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Reset reset = new Reset();
        addObject(reset, 464, 347);
        Dialogs dialogs = new Dialogs();
        addObject(dialogs, 473, 342);
    }
}
This is level1 code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
  
// i need to make an level varible
/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level1 extends Start
{    public void act()  
    {  
     
    } 
    
     /**
     * Constructor for objects of class Space.
     * 
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(); 
        counter = new  Counter();
        prepare();           
      
    } 
     
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Rocket rocket = new Rocket();
        addObject(rocket,14, 243);
        

        Rock rock =new Rock();
        addObject(rock, 673, 344);

        Rock rock2 = new Rock();
        addObject(rock2, 638, 55);

        Rock rock3 = new Rock();
        addObject(rock3, 672, 265);

        Rock rock4 = new Rock();
        addObject(rock4, 363, 225);

        Rock rock5 = new Rock();
        addObject(rock5, 697, 98);

        Rock rock6 = new Rock();
        addObject(rock6, 793, 269);

        Rock rock7 = new Rock();
        addObject(rock7, 832, 425);

        Rock rock8 = new Rock();
        addObject(rock8, 468, 348);

        Rock rock9 = new Rock();
        addObject(rock9, 523, 202);

        Rock rock10 = new Rock();
        addObject(rock10, 382, 69);

        Rock rock11 = new Rock();
        addObject(rock11, 380, 422);

        Rock rock12 = new Rock();
        addObject(rock12, 478, 115);

        Rock rock13 = new Rock();
        addObject(rock13, 477, 115);

        Rock rock14 = new Rock();
        addObject(rock14, 301, 90);

        Spawn spawn = new Spawn();
        addObject(spawn, 978, 486);

        addObject(counter, 47, 61);
        
   
    }
}
danpost danpost

2014/9/22

#
Ok. Now remove lines 11 and 22 from the Start class and show your Counter class code.
DarkSoulDemon DarkSoulDemon

2014/9/23

#
Do you mean from 11 to 22 to remove or do you mean for me to remove this line public void act() and {
There are more replies on the next page.
1
2
3