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

2014/8/1

scoreboard issues

Amightymuffin Amightymuffin

2014/8/1

#
so i got a dolphin class that removes the wombats from my game but the scoreboard comes up with the error cannot find symbol- variable dolphin when i try to place it before i run the game. dolphin class
public class Dolphin extends Actor
{
       
     public int WombatsStopped=0;
    private int direction, speed;
    int delay = 1; 
    public Dolphin(int dir)
    {
        direction = dir;
        speed = 15;
    }
    

    /**
     * Act - do whatever the Dolphin wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setRotation(direction);
        move(7);
        Stuff();
        
    }
    
        public void Stuff()
        {

            Actor actor = getOneIntersectingObject (Wombat.class);
            {
            
                int xCoord = getX();
                int yCoord=getY();
                if (xCoord >= 1799 || xCoord <=1 ||yCoord >= 999 || yCoord <=1 )
                {
                    World Road = getWorld();
                    Road.removeObject (this);
                }
                 if (actor != null)
                 {
                Road RoadWorld = (Road)getWorld();
                RoadWorld.removeObject (actor);
                RoadWorld.removeObject (this);
                 WombatsStopped++;
                }
            }
           
        }
    
    
    public int getscore()
    {
        return WombatsStopped;
    }
}
Scoreboard class:
public class Counter extends Actor
{
    private static final Color TEXT_COLOR = new Color(200, 0, 0);
    private static final Color TRANSPARENT_COLOR = new Color(0, 0, 0, 0);
 
    private Dolphin dolphin ;
 
 
        public Counter(Dolphin dolphin)
        {
            this.dolphin = dolphin;
            updateImage();
         }
 
        public void act() {
         updateImage();
        }
    
        private void updateImage()
        {
         String text = "Wombats Stopped" + dolphin.getscore();
         GreenfootImage image = new GreenfootImage(text, 20, TEXT_COLOR, 
         TRANSPARENT_COLOR);
         setImage(image);
        }
} 
danpost danpost

2014/8/1

#
What code do you have in your world class as far as creating and adding the Counter and Dolphin objects into the world?
Amightymuffin Amightymuffin

2014/8/1

#
well i have a different class that fires dolphins at the wombats, the dolphins are just the projectile that deletes them
danpost danpost

2014/8/1

#
It appears that you problem is in the methods used in creating those objects (whatever class that code resides in). It looks 'off' that you are passing a Dolphin object to the Counter object when it (the Counter object) is being created. It makes it appear you are creating a Counter object each time you create a Dolphin object (which obviously will not work for you). That is, of course, unless you are re-cycling the same Dolphin object, when only one Dolphin object is ever in the world at any one time and you are re-using that same Dolphin object each time a projectile (dolphin) is shot.
Amightymuffin Amightymuffin

2014/8/1

#
the code that spawns in the dolphin if space is entered
public class Baby2 extends MOVIG
{
    int delay = 1;

    /**
     * Act - do whatever the Baby2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkkeys();
    }    
    
    private void checkkeys()
    {
        if (Greenfoot.isKeyDown("D") )
        {
                       
                       turnright();
        }
        if (Greenfoot.isKeyDown("A") )
        {
                       
                       turnleft();
        }
        
        if (Greenfoot.isKeyDown("W"))
        {
            getWorld().addObject(new Dolphin(getRotation()), getX(), getY());
        }
        }
}
danpost danpost

2014/8/1

#
And the Counter object? where and what is the code that you are using to create and add it into the world?
Amightymuffin Amightymuffin

2014/8/18

#
i thought the scoreboard class created it (at least thats what the rough instructions im following is http://computingteacher.edublogs.org/files/2013/02/GreenfootScores-vvyjld.pdf )
danpost danpost

2014/8/18

#
The scoreboard class will create one, but you still need to tell it when to create one. I would presume it would be coded in your Road class constructor or 'prepare' method.
Amightymuffin Amightymuffin

2014/8/21

#
so what would i type in the spawn code?
public class Road extends World
{
   int delay = 1;
    /**
     * Constructor for objects of class Road.
     * 
     */
    public Road()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1800, 1000, 1); 

        prepare();
        
    }
    
    
        public void act()  
        {  
          spawn();  
        } 
    


    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        BABY1 baby1 = new BABY1();
        addObject(baby1, 49, 630);
        BABY1 baby12 = new BABY1();
        addObject(baby12, 62, 361);
        BABY1 baby13 = new BABY1();
        addObject(baby13, 80, 181);
        baby13.setLocation(60, 319);
        baby1.setLocation(61, 406);
        Wombat wombat = new Wombat();
        addObject(wombat, 736, 329);
        removeObject(wombat);
        baby1.setLocation(80, 554);
        baby12.setLocation(86, 412);
        baby13.setLocation(99, 297);
        Baby2 baby2 = new Baby2();
        addObject(baby2, 440, 731);
        Baby2 baby22 = new Baby2();
        addObject(baby22, 1281, 685);
        Baby2 baby23 = new Baby2();
        addObject(baby23, 1280, 175);
        Baby2 baby24 = new Baby2();
        addObject(baby24, 445, 163);
        Button button = new Button();
        addObject(button, 728, 48);
    }


    public void spawn()  
    {  
       if (Greenfoot.getRandomNumber(1) == 0)   
        {  
         if (getObjects(Wombat.class).size() < 50000) 
         {    
          addObject( new Wombat(),1700+Greenfoot.getRandomNumber(1799),1+Greenfoot.getRandomNumber(1098));    
         }  
        }
    }
}
danpost danpost

2014/8/21

#
Ok. I do not see anywhere an attempt to add a Dolphin object into your world. You would need to create one using the 'new Dolphin(Greenfoot.getRandomNumber(360))'. The value within the parenthesis could be hard-coded or more limited -- your choice -- as long as it is an 'int' value for the new dolphin's rotation). Then it would need to be added to the world. As an alternative, you can right click on the Dolphin class icon and select 'new Dolphin(int)', click on the world where you want it and supply the rotation value in the pop-up. This is similar to how you add a Scoreboard object into your world. Once you are satisfied with how those objects are placed into the world, you need to 'Save the world' to have your code reflect the changes you have made to the world. That way, next time you compile or reset, they will automatically be placed into your world.
davmac davmac

2014/8/21

#
danpost wrote...
Ok. I do not see anywhere an attempt to add a Dolphin object into your world
That's in the Baby2 class posted above, I think. Amightymuffin: you said:
the scoreboard comes up with the error cannot find symbol- variable dolphin when i try to place it before i run the game
Could you point out the exact line of code that you get this error on, please?
danpost danpost

2014/8/21

#
The blog site you are getting your instruction from does not really help much as far as allowing you to become a better programmer. It takes a class that is built specifically for one purpose and re-works it to work in another. I am not saying that this cannot be done -- I am saying that re-usable code should not need re-working; that is the beauty of OOP. For example, a basic Text class could be simply:
import greenfoot.*;

public class Text extends Actor
{
    public Text(String str)
    {
        setImage(new GreenfootImage(str, 24, null, null));
    }
}
With this, you really cannot do anything except create a Text object, add it to the world and remove it. We could add the following method to give the Text object the ability to have its text change:
public void setText(String str)
{
    setImage(new GreenfootImage(str, 24, null, null));
}
This is all that is really necessary to be used as a scoreboard. How about a world class with this:
// instance fields
private int score;
private Text scoreboard = new Text("Score: 0");
// in prepare method
addObject(scoreboard, 80, getHeight()-20);
// method to update score
public int adjustScore(int adjustment)
{
    score += adjustment; // adjust the score
    scoreboard.setText("Score: "+score); // update the scoreboard
    return score; // return the new score
}
That is it! A working scoreboard at your service. Now, anytime an actor decides that the score needs to change:
((WorldName)getWorld()).adjustScore(changeAmount);
will do it all. I made the 'adjustScore' method return the new score value in case it is needed. Like if you want something to happen after a certain score is reached:
if (((WorldName)getWorld()).adjustScore(changeAmount) >= 100)
{
    // make something happen
}
will both adjust the score and pose the question. This was done with close to the minimal amount of code -- nothing extravagant, nothing in-depth, nothing but the basics.
Amightymuffin Amightymuffin

2014/8/21

#
ok so maybe a i scrap the reworked version and write a new scoreboard that record how many wombats are removed by the dolphins ( done with both cancelling when they interact).
dolphin


public void act() 
    {
        setRotation(direction);
        move(7);
        getscore();
        
        
    
    
        
        {

            Actor actor = getOneIntersectingObject (Wombat.class);
            {
            
                int xCoord = getX();
                int yCoord=getY();
                if (xCoord >= 1799 || xCoord <=1 ||yCoord >= 999 || yCoord <=1 )
                {
                    World Road = getWorld();
                    Road.removeObject (this);
                }
                 if (actor != null)
                 {
                Road RoadWorld = (Road)getWorld();
                RoadWorld.removeObject (this);
                RoadWorld.removeObject (actor);
                
                 WombatsStopped++;
                }
            }
           
        }
    
    }
and the baby class that fires the dolphins
if (Greenfoot.isKeyDown("W"))
        {
            getWorld().addObject(new Dolphin(getRotation()), getX(), getY());
        }

and the other baby class
if (Greenfoot.isKeyDown("space"))
        
             if ( delay>=0)
             {
                 delay= delay-1 ;
                 }
                 else
                 {
                     getWorld().addObject(new Dolphin(getRotation()), getX(), getY());
              
                     delay=15;
            }
You need to login to post a reply.