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

2015/2/3

Help Needed! detecting if object exists and code cleanup

TheGoldenBucket TheGoldenBucket

2015/2/3

#
Hey guys! Needed to know how to detect if there is an arrow, or in this case, "Ranged", in the world. Also could use a little advice on how to clean this code up. Thanks!
public void rangedAttack()
    {
        if (Greenfoot.isKeyDown("o"))
        {
            if (Main_World.playerStamina != (0))
            {
                if (Main_World.playerStamina != (1))
                {
                    if (Main_World.playerStamina != (2))
                    {
                        if (Main_World.playerStamina != (3))
                        {
                            if (Main_World.playerStamina != (4))
                            {
                                Ranged attack = new Ranged();
                                getWorld().addObject(attack, getX(), getY());
                            }
                        }
                    }
                }
            }   
        }
    }
Super_Hippo Super_Hippo

2015/2/3

#
if (Greenfoot.isKeyDown("o") && Main_World.playerStamina > 4
    && getWorld().getObjects(Ranged.class).iEmpty())
    getWorld().addObject(new Ranged(), getX(), getY());
This creates a new object if none are in the world.
TheGoldenBucket TheGoldenBucket

2015/2/3

#
Thanks, Hippo! Managed to get that working, and I will start using the "&&"s to clean up my code. Best of thanks, -TheGoldenBucket
You need to login to post a reply.