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

2015/1/10

im trying to make a score counter for when the actor kills enemies but doesn't work for some reason

1
2
3
khalid11 khalid11

2015/1/10

#
this is the code for the score counter itself
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
{
    int score = 0;
    /**
     * Act - do whatever the Scorecounter 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.GREEN, Color.BLACK));
    }    

    public void addscore()
    {
        score++;
    }

    public void destroyEnemies()  
    {   
        Actor enemy = getOneIntersectingObject(enemy.class);  
        if(enemy != null) {  
            World myWorld = getWorld();
            getWorld().removeObject(enemy);  
            Level1 level1 = (Level1)myWorld;
            Counter counter = Level1.getCounter();
            counter.addScore();
              
        
this is the code for the bullet when it hits the enemy 
[code] public void destroyEnemies()
    {
        //"Enemy" can be any class that you want the bullet to destroy. 
        Actor enemy = getOneIntersectingObject(enemy.class);
        if(enemy != null) 
        {
            World myWorld=getWorld();
            Level1 level1=(Level1)myWorld;
            Counter counter = level1.getCounter();
            getWorld().removeObject(enemy);
          
        }
    }
any help would be good thanks
danpost danpost

2015/1/10

#
Try this -- right click on the world and select 'Inspect'. Then find the Counter object in the list of fields and click on it and then click the 'Inspect' button (you can also just double-click on the field). Now, on that panel, find where the World is listed. if it says 'null', then that is the problem -- the counter in the world is not the counter returned by the 'getCounter' method.
khalid11 khalid11

2015/1/10

#
there are no visible fields
danpost danpost

2015/1/10

#
Please post the 'getCounter' method of your Level1 world class. Also, post any part of the class that has anything to do with the Counter class or a Counter object. State where each part is located within the class.
khalid11 khalid11

2015/1/10

#
my Level1 counter codes
public class Level1 extends World
{
    Counter counter = new Counter();
    /**
     * Constructor for objects of class Level1.
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(700, 700, 1);
        prepare();
    }
        public Counter getCounter()
    {
        return counter;    
    }
khalid11 khalid11

2015/1/10

#
the first message was the counter class itself
khalid11 khalid11

2015/1/10

#
inside my bullet class
public void destroyEnemies()
    {
        
        Actor enemy = getOneIntersectingObject(enemy.class);
        if(enemy != null) 
        {
            World myWorld=getWorld();
            Level1 level1=(Level1)myWorld;
            Counter counter = level1.getCounter();
            getWorld().removeObject(enemy);
           
        }
khalid11 khalid11

2015/1/10

#
thats everything to do with my counter
danpost danpost

2015/1/10

#
What about the 'prepare' method in your Level1 class? do it not have something dealing with counters in it?
khalid11 khalid11

2015/1/10

#
could i just post a link to my game
danpost danpost

2015/1/10

#
If you put it up on this site, I am sure I could find it easily enough. Make sure you check the 'Publish source code' checkbox.
khalid11 khalid11

2015/1/10

#
i uploaded it its called greenfoot game
danpost danpost

2015/1/10

#
Got it. Will look into it shortly.
khalid11 khalid11

2015/1/10

#
thank you its yet to be completed so yh
danpost danpost

2015/1/10

#
In the 'destroyEnemies' method of the bullet class, you are getting the counter. But, you are not calling the 'addScore' method on it.
There are more replies on the next page.
1
2
3