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

2021/11/15

score wont wont work (class interface or enum expected)

Fangstar Fangstar

2021/11/15

#
So im making a kind of very simple space invaders but im trying to add a score board thing I used the: Edit, import class then counter. And when i have put it into my code to add a score every time my enemy dies it would work however when i put
public static int actCounter = 0;
ontop of my code it doesnt work: Here is my full code for my bullet
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public static int actCounter = 0;

/**
 * Write a description of class bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */


public class bullet extends Actor
{
    /**
     * Act - do whatever the bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static int actCounter = 0;
    public void act()
    {
        move(3);
        isTouching();
        if (isAtEdge()) getWorld().removeObject(this);
    }
    
        public void isTouching()
    {
    if (isTouching (alien2.class))
    {
    removeTouching(alien2.class); 
    actCounter.setValue(actCounter.getValue() + 1);
    }
    {
    if (isTouching (alien.class))
    {
    removeTouching(alien.class);
    actCounter.setValue(actCounter.getValue() + 1);
    }
    if (isTouching (laseralien.class))
    {
        removeTouching(laseralien.class);
        actCounter.setValue(actCounter.getValue() + 1);
    }
    if (isTouching (laseralien2.class))
    {
        removeTouching(laseralien2.class);
        actCounter.setValue(actCounter.getValue() + 1);
    }
    }
    }
    
    
}
Pls help
danpost danpost

2021/11/15

#
You have line 19 duplicated at line 3. Line 19 is fine. Line 3 needs deleted to fix the error (the line is not allowed outside the class brackets). It looks like you have non-destructible bullets. They will kill everything and anything (pretty much) within its path, only to be removed when the edge of the world is reached. Not sure that is what you wanted. As far as the Counter class, use of it is mainly to display a value on the screen. I doubt that you want any bullet to have its act count displayed to the user -- so the Counter class is not needed here (if it truely is for counting act steps). So, actCounter does not seem to be the appropriate name for the field declared at line 19. The name kills (or enemiesKilled) would seem to fit what it is being used for. Make sure that you zero the field from the world constructor or else subsequent plays will have the value carried over into the new games. Instead of using getValue and setValue, just use (using old name):
actCounter++;.
If you are to display the number of enemies killed, then you would use:
public static Counter killCounter;
with:
killCounter.setValue(killCounter.getValue()+1);
and with the following in your world constructor:
Bullet.killCounter = new Counter("Kills: ");
(so it does not keep the same Counter object on subsequent games)
You need to login to post a reply.