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

2020/12/1

Highscore

1
2
3
4
5
6
danpost danpost

2020/12/7

#
NewbJava wrote...
If it is a subclass of MyWorld it wouldn't have a super call.
Yes -- it needs a super call with dimensions of MeteorWorld. MyWorld has been given 2 constructors -- one for the initial world which sets up the scoring and the other (with dimensions) to avoid resetting the high score.
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
If it is a subclass of MyWorld it wouldn't have a super call.
Yes -- it needs a super call with dimensions of MeteorWorld. MyWorld has been given 2 constructors -- one for the initial world which sets up the scoring and the other (with dimensions) to avoid resetting the high score.
I went back and read your earlier post saying I should make a subclass of world called MeteorWorld. What I accidently did was make a subclass of MyWorld. I went back and changed it to match your directions.
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
If it is a subclass of MyWorld it wouldn't have a super call.
Yes -- it needs a super call with dimensions of MeteorWorld. MyWorld has been given 2 constructors -- one for the initial world which sets up the scoring and the other (with dimensions) to avoid resetting the high score.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class MeteorsWorld extends MyWorld
{

    /**
     * Constructor for objects of class MeteorsWorld.
     * 
     */
    public MeteorsWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
    } 
    public void addMeteor() 
    { 
     addObject(new Meteors(),getWidth()-1,Greenfoot.getRandomNumber(getHeight()));
    }
    private void prepare() 
    {  
        Rocket Rocket = new Rocket(); 
        addObject(Rocket,100,200); 
        Rocket.setLocation(70,200);  
        addObject(scoreboard,55,380); 
        addObject(highscore,525,25);
         
         
    } 
    public void Switchscreen() 
 { 
     boolean noRockets = getObjects(Rocket.class).isEmpty(); 
     if (noRockets == true) 
     { 
        Greenfoot.setWorld(new Playagain()); 
  
  
   } 
}
}
import greenfoot.*;  
 
public class MyWorld extends World
{ 
    static Actor scoreDisplay;
    static Actor highScoreDisplay;
    public static int score;
    static int highScore;  
    Scoreboard scoreboard = new Scoreboard();  
    HighScore highscore = new HighScore();
      
    public MyWorld()
    {    
         
        super(600, 400, 1, false);   
        prepare(); 
        scoreDisplay = new Scoreboard();
        highScoreDisplay = new HighScore(); 
        score = -1;
        highScore = -1;
        adjustScore(1); 
         
         
         
          
           
          
    }   
       
     
    static void adjustScore(int amt)
    {
        score += amt;
        GreenfootImage img = new GreenfootImage("Score: "+score, 24, Color.RED, Color.BLACK);
         
        if (score > highScore)
        {
            highScore = score;
            img = new GreenfootImage("High score: "+highScore, 24, Color.RED, Color.BLACK);
            highScoreDisplay.setImage(img);
        }
    } 
     
    public void act() 
    {  
       Switchscreen();
       if(Greenfoot.getRandomNumber(20)<1) 
       {
       addMeteor();  
       } 
        
         
    }    
    
    public Scoreboard getScoreboard() 
    {  
        return scoreboard; 
          
          
          
          
    } 
}
import greenfoot.*;  

public class Meteors extends Obstacles
{
   MyWorld thisGame; 
    public void act() 
    {  
        
        move(); 
        meteor();
        remove(); 
        
    }   
    public void meteor() 
    {  
        Actor lasars = getOneIntersectingObject(Lasars.class); 
        if(lasars != null) 
        {   
        World MyWorld = getWorld(); 
        MyWorld myWorld = (MyWorld)MyWorld;  
        Scoreboard scoreboard = myWorld.getScoreboard(); 
        scoreboard.addscore(); 
        getWorld().removeObject(lasars);
        for(int i=0;i<10;i++) 
        { 
            int posx=-20+Greenfoot.getRandomNumber(40);  
            int posy=-20+Greenfoot.getRandomNumber(40);
            getWorld().addObject(new Animate(getImage()),getX()+posx,getY()+posy); 
        } 
        getWorld().addObject(new Explosion(),getX(),getY());  
        toRemove=true;
    
}   

} 
}
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
If it is a subclass of MyWorld it wouldn't have a super call.
Yes -- it needs a super call with dimensions of MeteorWorld. MyWorld has been given 2 constructors -- one for the initial world which sets up the scoring and the other (with dimensions) to avoid resetting the high score.
is this now correct?
danpost danpost

2020/12/7

#
NewbJava wrote...
is this now correct?
In MyWorld, remove the following lines: 9, 10, 16, 46 thru 50 and 55 thru 62. Then refer back to my posting of the class to see what should be in the act method.
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
is this now correct?
In MyWorld, remove the following lines: 9, 10, 16, 46 thru 50 and 55 thru 62. Then refer back to my posting of the class to see what should be in the act method.
import greenfoot.*;  
 
public class MyWorld extends World
{ 
    static Actor scoreDisplay;
    static Actor highScoreDisplay;
    public static int score;
    static int highScore;  
    
      
    public MyWorld()
    {    
         
        super(600, 400, 1, false);   
         
        scoreDisplay = new Scoreboard();
        highScoreDisplay = new HighScore(); 
        score = -1;
        highScore = -1;
        adjustScore(1); 
         
         
         
          
           
          
    }   
       
     
    static void adjustScore(int amt)
    {
        score += amt;
        GreenfootImage img = new GreenfootImage("Score: "+score, 24, Color.RED, Color.BLACK);
         
        if (score > highScore)
        {
            highScore = score;
            img = new GreenfootImage("High score: "+highScore, 24, Color.RED, Color.BLACK);
            highScoreDisplay.setImage(img);
        }
    } 
     
    public void act() 
    {  
        Greenfoot.setWorld(new MeteorsWorld());
        
         
    }    
    
    
}
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
is this now correct?
In MyWorld, remove the following lines: 9, 10, 16, 46 thru 50 and 55 thru 62. Then refer back to my posting of the class to see what should be in the act method.
I took out everything you told me too and referred back to your original post and put in what was in the act method
danpost danpost

2020/12/7

#
Now, call prepare from MeteorsWorld constructor. Also include lines to add the display actors into your MeteorsWorld world.
NewbJava NewbJava

2020/12/7

#
danpost wrote...
Now, call prepare from MeteorsWorld constructor. Also include lines to add the display actors into your MeteorsWorld world.
when I called the prepare method and put it into the act() method in MyWorld there was error saying " cannot find symbol method prepare() "
danpost danpost

2020/12/7

#
NewbJava wrote...
when I called the prepare method and put it into the act() method in MyWorld there was error saying " cannot find symbol method prepare() "
Read instruction again.
NewbJava NewbJava

2020/12/7

#
danpost wrote...
Now, call prepare from MeteorsWorld constructor. Also include lines to add the display actors into your MeteorsWorld world.
import greenfoot.*;  
 
public class MyWorld extends World
{ 
    static Actor scoreDisplay;
    static Actor highScoreDisplay;
    public static int score;
    static int highScore;  
    
      
    public MyWorld()
    {    
         
        super(600, 400, 1, false);   
         
        scoreDisplay = new Scoreboard();
        highScoreDisplay = new HighScore(); 
        score = -1;
        highScore = -1;
        adjustScore(1); 
         
         
         
          
           
          
    }   
       
     
    static void adjustScore(int amt)
    {
        score += amt;
        GreenfootImage img = new GreenfootImage("Score: "+score, 24, Color.RED, Color.BLACK);
         
        if (score > highScore)
        {
            highScore = score;
            img = new GreenfootImage("High score: "+highScore, 24, Color.RED, Color.BLACK);
            highScoreDisplay.setImage(img);
        }
    } 
     
    public void act() 
    {  
        Greenfoot.setWorld(new MeteorsWorld()); 
        prepare(); 
        
         
    }    
    
    
}
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
when I called the prepare method and put it into the act() method in MyWorld there was error saying " cannot find symbol method prepare() "
Read instruction again.
public class MeteorsWorld extends MyWorld
{

    /**
     * Constructor for objects of class MeteorsWorld.
     * 
     */
    public MeteorsWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);  
        prepare(); 
        
    } 
    public void addMeteor() 
    { 
     addObject(new Meteors(),getWidth()-1,Greenfoot.getRandomNumber(getHeight()));
    }
    private void prepare() 
    {  
        Rocket Rocket = new Rocket(); 
        addObject(Rocket,100,200); 
        Rocket.setLocation(70,200);  
        addObject(scoreboard,55,380); 
        addObject(highscore,525,25);
         
         
    } 
    public void Switchscreen() 
 { 
     boolean noRockets = getObjects(Rocket.class).isEmpty(); 
     if (noRockets == true) 
     { 
        Greenfoot.setWorld(new Playagain()); 
  
  
   } 
} 












}
danpost danpost

2020/12/7

#
danpost wrote...
call prepare from MeteorsWorld constructor
NewbJava NewbJava

2020/12/7

#
danpost wrote...
NewbJava wrote...
when I called the prepare method and put it into the act() method in MyWorld there was error saying " cannot find symbol method prepare() "
Read instruction again.
I think this is correct now
danpost danpost

2020/12/7

#
NewbJava wrote...
I think this is correct now
Except for the name of your actors in prepare at lines 24 and 25.
There are more replies on the next page.
1
2
3
4
5
6