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

2016/7/26

How to make lives and invincibility frames

Flootershai Flootershai

2016/7/26

#
I am having an existential crisis over an issue I'm having in which my lives variable doesn't decrease when I tell it to and I can't figure out how to successfully make my character invincible for a period of time. During this invincibility, I want BulletSlow.class to pass through the character (so I want to stop getObjectsInRange). Here's the relevant code:
    public int PippiLives = 3;
    private boolean HitboxState = true;
    private int contactTimer;
Getting hit in the below method should remove 1 PippiLives but doesn't:
    public void circularCollision()
    {
        if (HitboxState && getWorld().getObjects(Pippi.class).size() != 0)
        {
            List<BulletSlow> bulletList=getObjectsInRange(25, BulletSlow.class); /** Adjust range for sprite */
            for (BulletSlow bulletSlow:bulletList)
            {
                if (bulletList.size() > 0)
                    {
                        PippiLives--;
                        getWorld().removeObjects(bulletList); 
                        /** doesn't exist if Pippi removed, so nullpointerexception
                        * In future stop this method when lives = 0 */
                    }
            }
        }  
This is the invincibility timer, which I also tried to make remove 1 PippiLives but it didn't work either:
    public void invincibility()
    {
        if (HitboxState = false)
        {
            contactTimer = (contactTimer + 1) % 20;
            if (contactTimer == 0)
            {
                HitboxState = true;
                PippiLives--;
                if (PippiLives == 0)
                {
                    Greenfoot.stop();
                    return;
                }
    }
    }
Could someone show me how to make the character's hitbox actually turn off for a bit and then back on and have the character being hit actually remove 1 PippiLives? Thanks <3
davmac davmac

2016/7/26

#
I think most of your problem is in this line:
    if (HitboxState = false)
This is setting HitboxState to false, and always evaluating to false. You need to use double-equals (==) to compare, rather than single equals:
    if (HitboxState == false)
Or, since you are dealing with a boolean value:
    if (! HitboxState)
(Read above as "if not HitboxState").
You need to login to post a reply.