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

2020/3/4

scoreboard problem

Hinokami Hinokami

2020/3/4

#
hello, so i tried making a scoreboard which works when i kill the first enemy, after that each time i kill an enemy the score keeps at 5 i know why, but i dont know the solution, it is caused because its repeating making the scoreboard. Anyone please help? heres the code
public void killEnemy()
    {
        try {
            if(canSee(Enemy1.class))
            {
                Enemy1 enemy1 = (Enemy1) getOneObjectAtOffset(0, 0, Enemy1.class);
                if (isTouching(Enemy1.class))
                {
                    enemyCounter= enemyCounter + 5;
                } 
                
                Level3 level3 = (Level3)getWorld();
                World world = getWorld();

                world.removeObjects(world.getObjects(EnemyCounter.class));
                world.addObject (new EnemyCounter(enemyCounter), 100, 50); 
                removeObject(Enemy1.class);
                getWorld().removeObject(this);
            }
        } catch (IllegalStateException ex)
        {
            //do nothing
        }
    }
danpost danpost

2020/3/4

#
Instead of adding and removing counters, just change the image of the counter that is in the world.
Hinokami Hinokami

2020/3/5

#
Sorry i still dont understand your solution could you come up with the code?
danpost danpost

2020/3/5

#
Hinokami wrote...
Sorry i still dont understand your solution could you come up with the code?
Show EnemyCounter class codes.
Hinokami Hinokami

2020/3/5

#
code for enemycounter
import greenfoot.*;
/**
 * Write a description of class EnemyCounter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class EnemyCounter  extends Actor
{
    public static final float FONT_SIZE = 24.0f;
    public static final int WIDTH = 150;
    public static final int HEIGHT = 50;
    public static final int TRANSPARENCY = 128;
    public static final int RED = 255;
    public static final int GREEN = 255;
    public static final int BLUE = 255;
    private int enemyCounter;
    /**
     * Constructor for objects of class EnemyCounter
     */
    public EnemyCounter(int newCount)
    {
        this.enemyCounter=newCount;
        createCounter();
    }

    public void act()
    {
       
        
    }


    public void createCounter()
    {
        GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
        Color color = new Color (RED,GREEN,BLUE,TRANSPARENCY);
        image.setColor(color); //set the color of the image with the object color
        image.fillRect(0, 0, WIDTH, HEIGHT); //fill the rectangle with the setted color
        image.setColor(new Color(RED,0,0, TRANSPARENCY));
        image.fillRect(5, 5, WIDTH-10, HEIGHT-10);
        image.setColor(new Color(0, GREEN, BLUE, TRANSPARENCY));
        image.fillRect(10, 10, WIDTH-20, HEIGHT-20);
        Font font = image.getFont();
        font = font.deriveFont(FONT_SIZE);
        image.setFont(font);
        image.setColor(new Color(0,0,0,TRANSPARENCY));
        image.drawString("score: " + enemyCounter, 30, 32);
        setImage(image); //create the image 
    }
    public void destroyEnemies()  
    {   
        Actor enemy = getOneIntersectingObject(Enemy1.class);  
        if(enemy != null) {  
            World myWorld = getWorld();
            getWorld().removeObject(enemy);  
            Level3 level3 = (Level3)myWorld;
    
           

           
        }
    }
}
code for projectile that adds up the counter per enemy kill
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import greenfoot.*;
/**
 * Write a description of class Projectile here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Projectile extends BaseClass
{
    private int enemyCounter;

    /**
     * Constructor for objects of class Projectile
     */
    public Projectile(int rotation)
    {
        setImage("projectile.png");
        setRotation(rotation);

    }

    public void act()
    {
        move(4);
        killEnemy();
        disappearAtEdge();
    }

    public void countEnemies()
    {
        if (canSee(Enemy1.class))
        {

        }
    }

    public void killEnemy()
    {
        try {
            if(canSee(Enemy1.class))
            {
                Enemy1 enemy1 = (Enemy1) getOneObjectAtOffset(0, 0, Enemy1.class);
                if (isTouching(Enemy1.class))
                {
                    enemyCounter= enemyCounter + 5;
                } 
                
                Level3 level3 = (Level3)getWorld();
                World world = getWorld();

                world.removeObjects(world.getObjects(EnemyCounter.class));
                world.addObject (new EnemyCounter(enemyCounter), 100, 50); 
                removeObject(Enemy1.class);
                getWorld().removeObject(this);
            }
        } catch (IllegalStateException ex)
        {
            //do nothing
        }
    }

    public void disappearAtEdge()
    {
        try {
            if(atWorldEdge())
            {

                if (getX()>570)
                {
                    getWorld().removeObject(this); 
                }
                //north
                if (getY()>570)
                {
                    getWorld().removeObject(this);  
                }
                //south
                if (getY()<30)
                {
                    getWorld().removeObject(this); 
                }
                //west
                if (getX()<30)
                {
                    getWorld().removeObject(this);
                }

            }
        } catch (IllegalStateException ex)
        {
            //do nothing
        }
    }

}
Hinokami Hinokami

2020/3/5

#
oh by the way ignore public void destroyEnemies
danpost danpost

2020/3/5

#
Replace lines 21 thru 31 in EnemyCounter class with the following:
public EnemyCounter()
{
    createCounter();
}

public void add(int amount)
{
    enemyCounter += amount;
    createCounter();
}
This will allow you to adjust the value and update the image of the (one) counter in lieu of removing and adding (multiple) counters.
danpost danpost

2020/3/5

#
Oh, and do not use an enemyCounter field in your character's class. Anytime you replace a character, its fields are reset.
Hinokami Hinokami

2020/3/5

#
Oh, and do not use an enemyCounter field in your character's class. Anytime you replace a character, its fields are reset.
what do you mean by this?
Hinokami Hinokami

2020/3/5

#
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import greenfoot.*;
/**
 * Write a description of class Projectile here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Projectile extends BaseClass
{
    private int enemyCounter;

    /**
     * Constructor for objects of class Projectile
     */
    public Projectile(int rotation)
    {
        setImage("projectile.png");
        setRotation(rotation);

    }

    public void act()
    {
        move(4);
        killEnemy();
        disappearAtEdge();
    }

    public void countEnemies()
    {
        if (canSee(Enemy1.class))
        {

        }
    }

    public void killEnemy()
    {
        try {
            if(canSee(Enemy1.class))
            {
                Enemy1 enemy1 = (Enemy1) getOneObjectAtOffset(0, 0, Enemy1.class);
                if (isTouching(Enemy1.class))
                {
                    enemyCounter= enemyCounter + 5;
                } 

                Level3 level3 = (Level3)getWorld();
                World world = getWorld();

                world.removeObjects(world.getObjects(EnemyCounter.class));
                world.addObject (new EnemyCounter(), 100, 50); 
                removeObject(Enemy1.class);
                getWorld().removeObject(this);
            }
        } catch (IllegalStateException ex)
        {
            //do nothing
        }
    }

    public void disappearAtEdge()
    {
        try {
            if(atWorldEdge())
            {

                if (getX()>570)
                {
                    getWorld().removeObject(this); 
                }
                //north
                if (getY()>570)
                {
                    getWorld().removeObject(this);  
                }
                //south
                if (getY()<30)
                {
                    getWorld().removeObject(this); 
                }
                //west
                if (getX()<30)
                {
                    getWorld().removeObject(this);
                }

            }
        } catch (IllegalStateException ex)
        {
            //do nothing
        }
    }

}
i've changed the code to this now the score keeps add 0 and does not add up
danpost danpost

2020/3/5

#
Hinokami wrote...
<< Quote Omitted >> what do you mean by this?
I mean that each object created from the class gets its own independent int enemyCounter field and that is why your score never goes above 5.
danpost danpost

2020/3/5

#
In your Projectile class, remove lines 13, 54 and 55 and change line 48 to the following:
((EnemyCounter)getWorld().getObjectsAt(100, 50, EnemyCounter.class).get(0)).add(5);
Hinokami Hinokami

2020/3/5

#
i've asked my teacher about it, it is fixed now thank you
You need to login to post a reply.