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

2015/2/28

health help!

Moho Moho

2015/2/28

#
i am making a game and got pretty far with it so far but the health system doesn't seem to work! i have 6 health point so meaning i can be hit 6 times without dying and i have it set when i hit an enemy health int drops 1 and if health == 5 it removes heart number 6 but it doesn't remove the hearts throughout the whole cycle! after 6 hits it displays that i have lost but never removes the hearts! here is my code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MainPlayer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Players
{
    //images for animation
    GreenfootImage WalkBack = new GreenfootImage("WalkBack.png");
    GreenfootImage WalkDown = new GreenfootImage("WalkDown.png");
    GreenfootImage WalkLeft = new GreenfootImage("WalkLeft.png");
    private boolean spaceDown;
    private boolean ZDown;
    public int health = 6;
    
    
     
    
    
    public void act() 
    {
        move();
        attack1();
        //attack2();
        countGems();
        die();
        removeHearts();
        //act method does whatever the Actor wants to do. This method is called whenever the
        //act or run button gets pressed in the enviroment
    }

    public void move()
    {
        int x = getX();
        int y = getY();
        //moving 
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-2);
            setImage(WalkBack);
            if ( isTouching(Walls.class) )
            {
                setLocation(getX(), getY()+2);
                //checks walls
            }
            if(canSee(Enemy1.class))
            {
                setLocation(getX(), getY()+10);
                removeHearts();

                health--;

            }

        }
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+2);
            setImage(WalkDown);
            if ( isTouching(Walls.class) )
            {
                setLocation(getX(), getY()-2);
                //checks walls
            }
            if(canSee(Enemy1.class))
            {
                setLocation(getX(), getY()-10);
                removeHearts();
                health--;

            }

        }
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-2, getY());
            setImage(WalkLeft);
            if ( isTouching(Walls.class) )
            {
                setLocation(getX()+2, getY());
                //checks walls
            }
            if(canSee(Enemy1.class))
            {
                setLocation(getX()+10, getY());
                removeHearts();
                health--;

            }

        }
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+2, getY());
            //setImage("NAME");
            if ( isTouching(Walls.class) )
            {
                setLocation(getX()-2, getY());
                //checks walls
            }
            if(canSee(Enemy1.class))
            {
                setLocation(getX()-10, getY());
                removeHearts();
                health = health - 1;

            }

        }
        //moving 

    }

    public void attack1()

    {
        //attacking the enemy
        if (spaceDown != Greenfoot.isKeyDown("space")) 
        {
            spaceDown = ! spaceDown; 
            if (spaceDown) 
            {  
                Attack1 attack1 = new Attack1();
                getWorld().addObject(attack1, getX()+1, getY());
                attack1.setRotation(getRotation());

            }    
        }

    }

    public void die()
    {

        if(health <= 0)
        {
            Lose lose = new Lose();
            eat(Heart.class);

            getWorld().addObject(lose, 140, 140);
        }
    }
    public  void removeHearts()
    {
        if(health == 5)
        {
            eat(Heart6.class);
        }else if(health == 4) 
        {
            eat(Heart5.class);
        }else if(health == 3)
        {
            eat(Heart4.class);

        }else if(health == 2)
        {
            eat(Heart3.class);

        }else if(health == 1)
        {
            eat(Heart2.class);

        }else if(health <= 0)
        {
            die();

        }

    }
    public void addGems()
    {
        if(canSee(Gems.class))
        {
            eat(Gems.class);
            GemCounter++;
            
        }
        
    }
    
}
danpost danpost

2015/2/28

#
What is the code for your 'eat' method? do you not think that might be important? Maybe you should show the entire code for your 'Players' class since this class extends that one and inherits the methods and fields from it. And if the 'Players' class does not extend the 'Actor' class, the same thing applies.
Moho Moho

2015/2/28

#
my eat method is below
public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
i also have a remove self method and that is also below
 public void removeSelf()
    {
        getWorld().removeObject(this);
    }
danpost danpost

2015/2/28

#
Do you think that a heart will be removed by this 'eat' method (which removes an intersecting object of the class given)? Also, your 'removeHearts' method does not do anything when the value of 'health' is 6 and you are calling the method before deceasing the value of the field. So, even if everything else was fixed, it would still not remove a heart the first time that it is supposed to.
Moho Moho

2015/3/1

#
it doesnt need to do anything when the value is 6 right ? because health-- decreases it to five then removes heart6
danpost danpost

2015/3/1

#
Moho wrote...
it doesnt need to do anything when the value is 6 right ? because health-- decreases it to five then removes heart6
But, that is not the order in which you are executing those actions. You are trying to remove the heart before you decrease the value of the health field. See the code around lines 52, 71, 89 and 107.
Moho Moho

2015/3/1

#
i removed those line and it still doesnt remove the hearts but decreases the health
Moho Moho

2015/3/1

#
do you know an easier way to make health ??? thanks
danpost danpost

2015/3/1

#
Well, the hearts will definitely not get removed if you just remove those lines. Try putting 'health--;' before calling 'removeHearts' in those areas.
Moho Moho

2015/3/2

#
so i removed the lines that you said to remove and it still doesnt seem to work
danpost danpost

2015/3/2

#
Moho wrote...
so i removed the lines that you said to remove and it still doesnt seem to work
I never said to remove any lines. You had removed lines that were not supposed to be removed.
Moho Moho

2015/3/2

#
so i added the lines 52, 71, 89 and 107 back but put "health--;" before the removeHearts(); it still doesnt remove the hearts although it displays that i have lost after i have hit the enemy 6 times
danpost danpost

2015/3/3

#
Moho wrote...
so i added the lines 52, 71, 89 and 107 back but put "health--;" before the removeHearts(); it still doesnt remove the hearts although it displays that i have lost after i have hit the enemy 6 times
It is possible that the issue is due to something in your World subclass. Post its code. Wait. Before you do, are still calling 'eat' to remove the hearts? or, did you alter the code for that?
Moho Moho

2015/3/3

#
i had eat and i also tried getWorld().removeObject(Heart.class); but it that one doesnt work... i also tried getWorld().Heart.removeSelf(); but that doesnt work either and yes it is currently set as eat
danpost danpost

2015/3/3

#
Moho wrote...
i had eat and i also tried getWorld().removeObject(Heart.class); but it that one doesnt work...
Like I said before, the 'eat' method is not going to do what you want because it looks for intersectors , not just objects in the world. And, of course 'getWorld().removeObject(Heart.class)' will not work because the 'removeObject' method requires an Actor object, not a class.
i also tried getWorld().Heart.removeSelf(); but that doesnt work either and yes it is currently set as eat
I do not know why you tried that! Looks to me like you are throwing things together without any rhyme or reason. Since each heart is of a different class, you can just remove all instances of each type from the world to remove the one instance that is in the world of that particular type. This would use the 'removeObjects' method instead of the 'removeObject' method. The following would remove all 'Heart6' type objects from the world:
getWorld().removeObjects(getWorld().getObjects(Heart6.class));
You need to login to post a reply.