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

2018/5/13

Hi I recently created a health and a score counter in my game but one of them keeps increasing by 4 and the other by 1. I only want them to increase by 1, please help

1
2
an123 an123

2018/5/13

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

public class Score  extends Actor
{
    public int life = 0;

    
    public Score()
    {
        setImage(new GreenfootImage("Score: 0", 50, Color.BLACK, Color.WHITE));
    }

    /**
     * Increase the total amount displayed on the counter, by a given amount.
     */
    public void bumpCount(int amount)
    {
        life = life + amount;
        setImage(new GreenfootImage("Score: " + life, 50, Color.RED, Color.WHITE));
    }
    
}

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

public class Health  extends Actor
{
    private int health = 5;

    
    public Health()
    {
        setImage(new GreenfootImage("Health: 5", 50, Color.BLACK, Color.WHITE));
    }

    /**
     * Increase the total amount displayed on the counter, by a given amount.
     */
    public void bumpCount(int amount)
    {
        health -= amount;
        setImage(new GreenfootImage("Health: " + health, 50, Color.RED, Color.WHITE));
    }
    
}
enemy lifecheck public void lifeCheck()//checks if the player has been hit { Actor bullet = getOneIntersectingObject(Bullet.class); if (bullet != null) { hitABullet(); this.setImage("pplwhite.png"); score = score + 1; getWorld().removeObject(bullet); } if (score == 5) { //Greenfoot.delay(60); //Greenfoot.setWorld(new YouLost()); } playerlifecheck public void lifeCheck()//checks if the player has been hit { Actor bullet = getOneIntersectingObject(Psuedocode.class); if (bullet != null) { hitAnAsteroid(); this.setImage("hitstudent.png"); health--; getWorld().removeObject(bullet); } if (health == 0) { //Greenfoot.delay(60); //Greenfoot.setWorld(new YouLost()); health = 5; } } private void hitAnAsteroid() { level1 level1 = (level1) getWorld(); Health health = level1.getHealth(); health.bumpCount(1); } } } private void hitABullet() { level1 level1 = (level1) getWorld(); Score score = level1.getScore(); score.bumpCount(1); } }
danpost danpost

2018/5/13

#
What do the Bullet and Psuedocode classes look like??
an123 an123

2018/5/13

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bullet here. * * @author (your name) * @version (a version number or a date) */ public class Bullet extends Student { /** * Act - do whatever the Bullet wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int direction; private int speed; public void act() { setRotation(direction-90); move(speed); if(atWorldEdge()){ getWorld().removeObject(this); return; } } public Bullet(int dir) { direction = dir; speed = 7; } private boolean atWorldEdge() { return (getX()==0 || getY()==0 || getY()==getWorld().getHeight()/2 || getX()==getWorld().getWidth()-1); } }
an123 an123

2018/5/13

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Psuedocode here. * * @author (your name) * @version (a version number or a date) */ public class Psuedocode extends Actor { /** * Act - do whatever the Psuedocode wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int speed = 6; /** * Act - do whatever the Projectile wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void Psuedocode() { setLocation(getX(), getY()); act(); } public void act() { move(speed); leaveCheck(); } public void orient(int x) { setRotation(x); } public void leaveCheck() { if(getY() >= getWorld().getHeight() - 1 || getX() < 1 || getX() >= getWorld().getWidth() - 1) { getWorld().removeObject(this); } } // Add your action code here. }
danpost danpost

2018/5/13

#
an123 wrote...
<< Code Omitted >>
Okay -- no conflicting code there. Is there any code elsewhere that deals with the score or health?
an123 an123

2018/5/13

#
the level1 world? import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.*; /** * Write a description of class LevelOne here. * * @author (your name) * @version (a version number or a date) */ public class level1 extends World { public static ArrayList<Cancer> cancers; private Health theHealth; private Score theScore; /** * Constructor for objects of class LevelOne. * */ public level1() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 600, 1); addObject(Student, 300, 509); addObject(Chavez, 300, 100); theHealth = new Health(); addObject(theHealth, 650, 42); theScore = new Score(); addObject(theScore, 100, 42); } public Score getScore() { return theScore; } public Health getHealth() { return theHealth; } Chavez Chavez = new Chavez(); Student Student = new Student(); }
danpost danpost

2018/5/13

#
an123 wrote...
the level1 world? << Code Omitted >>
No -- nothing there that would cause the behavior you suggest.
an123 an123

2018/5/13

#
i just don't know why its doing this. Should i post my scenario so u can take a look at it?
danpost danpost

2018/5/14

#
an123 wrote...
i just don't know why its doing this. Should i post my scenario so u can take a look at it?
That would allow a more complete inspection. Make sure you check the 'Publish source code" checkbox in the dialog box.
Heet_Patel Heet_Patel

2018/5/15

#
Try this
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 */
public class Counter  extends be
{
    private int totalCount = 0;

    public Counter()
    {
        setImage(new GreenfootImage("Score shown here", 30, Color.ORANGE, Color.BLACK));
    }

    /**
     * Increase the total amount displayed on the counter, by a given amount.
     */
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("" + totalCount, 30, Color.ORANGE, Color.BLACK));
    }
}
Heet_Patel Heet_Patel

2018/5/15

#
For the health bar:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Level_4 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HealthBar extends chaar
{
    int health = 4;
    int healthBarWidth = 300;
    int healthBarHeight = 30;
    int pixelsPerHealthPoint = (int)healthBarWidth/health;
    /**
     * Act - do whatever the Level_4 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public HealthBar()
    {
        update();
    }    

    public void act() 
    {
        update();
    }

    public void update()
    {
        setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2));
        GreenfootImage myImage = getImage();
        myImage.setColor(Color.WHITE);
        myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
        myImage.setColor(Color.BLUE);
        myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight);
    }

    public void loseHealth()
    {
        health--;
    }    
}
Heet_Patel Heet_Patel

2018/5/15

#
If you don't want to have a health bar but thats what I have in my game. It does the same thing
Heet_Patel Heet_Patel

2018/5/15

#
For the losing of the health:
Actor (Actorname) = getOneIntersectingObject((actorname).class);
        if((Actorname) != null)
        {
            World myWorld = getWorld();
            (Worldname) (worldname) = (Worldname)myWorld;
            HealthBar healthbar = (worldname).getHealthBar();
            if(touching(actorname) == false)
            {
                healthbar.loseHealth();
                touching(actorname) = true;
                if(healthbar.health <=0)
                {
                    myWorld.removeObject(this);
                }    
            }    

        } else { 
            touching(actorname) = false;
        }
Change all the (actornames) in to the actor which will deal damage. And change (worldname) into your worldname and (worldname) into a short form for the world name instead of writing the entire world name again. For example. If your world name was Water and your actor which was dealing the damage was called Jeremy then this would be the code:
Actor Jeremy = getOneIntersectingObject(Jeremy.class);
        if(Jeremy != null)
        {
            World myWorld = getWorld();
            Water water = (Water)myWorld;
            HealthBar healthbar = water.getHealthBar();
            if(touchingJeremy == false)
            {
                healthbar.loseHealth();
                touchingJeremy = true;
                if(healthbar.health <=0)
                {
                    myWorld.removeObject(this);
                }    
            }    

        } else { 
            touchingJeremy = false;
        }
Heet_Patel Heet_Patel

2018/5/15

#
put this in the void act for the actor which is losing health.
Heet_Patel Heet_Patel

2018/5/15

#
Also add this code to the world class.
HealthBar healthbar = new HealthBar();
and
    public HealthBar getHealthBar()
    {
        return healthbar;
    }
There are more replies on the next page.
1
2