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

2014/1/12

I have a problem

sanoy sanoy

2014/1/12

#
I want to make a counter that counts all slimes the player has eliminated
if(space() == true)
        {
            if(direction == 1)
            {
                getWorld().removeObject(getOneObjectAtOffset(0,-(2+getImage().getHeight()/2),slime.class));
                slimeKilled = slimeKilled + 1;
            }
            if(direction == 2)
            {
                getWorld().removeObject(getOneObjectAtOffset(0,(2+getImage().getHeight()/2),slime.class));
                slimeKilled = slimeKilled + 1;
            }
            if(direction == 3)
            {
                getWorld().removeObject(getOneObjectAtOffset(-(2+getImage().getWidth()/2),0,slime.class));
                slimeKilled = slimeKilled + 1;
            }
            if(direction == 4)
            {
                getWorld().removeObject(getOneObjectAtOffset((2+getImage().getWidth()/2),0,slime.class));
                slimeKilled = slimeKilled + 1;
            }
        }
This is my code, but it count all the time space is pressed I just want it to count evry time the player hits a slime
danpost danpost

2014/1/12

#
That is because you have no idea whether the 'removeObject' calls removed anything or not. Set the return of 'getOneObjectAtOffset' to an Actor field, then only if that field is not null would you remove it and add to the counter.
sanoy sanoy

2014/1/12

#
i dont unstand how to do that could you help me
danpost danpost

2014/1/12

#
Instead of (as one example) lines 18 through 22, you would use:
Actor slime = getOneObjectAtOffset((2+getImage().getWidth()/2), 0, slime.class);
if (slime != null)
{
    getWorld().removeObject(slime);
    slimeKilled++;
}
Read the instructions in my last post and compare it to the code here.
You need to login to post a reply.