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

2012/5/7

How do I make a boolean?

h123n h123n

2012/5/7

#
I want to make a boolean called humansDestroyed how do I do this? here is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class sausageNinja1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class sausageNinja1 extends Actor
{
    /**
     * Act - do whatever the sausageNinja1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       Greenfoot.delay(100);
       Actor beginning;
       beginning = getOneObjectAtOffset(0, 0, beginning.class);
       if (beginning != null)
       {
           World world;
           world = getWorld();
           world.removeObject(beginning);
        }
        if(Greenfoot.isKeyDown("up"))
        {
            somersault();
        }
    }    
    public void somersault()
    {
        Actor Human;
       Human = getOneObjectAtOffset(0, 0, Human.class);
       if (Human != null)
       {
           World world;
           world = getWorld();
           world.removeObject(Human);
    }
        turn(-20);
        Greenfoot.delay(1);
        turn(-20);
        Greenfoot.delay(1);
        turn(-20);
        Greenfoot.delay(1);
        turn(-20);
        Greenfoot.delay(1);
        turn(-280);
        
}
}
erdelf erdelf

2012/5/8

#
Look in line 05 and 09
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class sausageNinja1 extends Actor
{
    private boolean humansDestroyed;

    public sausageNinja1()
    {
        humansDestroyed = false;
     }

    public void act() 
    {
       Greenfoot.delay(100);
       Actor beginning;
       beginning = getOneObjectAtOffset(0, 0, beginning.class);
       if (beginning != null)
       {
           World world;
           world = getWorld();
           world.removeObject(beginning);
        }
        if(Greenfoot.isKeyDown("up"))
        {
            somersault();
        }
    }    
    public void somersault()
    {
        Actor Human;
       Human = getOneObjectAtOffset(0, 0, Human.class);
       if (Human != null)
       {
           World world;
           world = getWorld();
           world.removeObject(Human);
    }
        turn(-20);
        Greenfoot.delay(1);
        turn(-20);
        Greenfoot.delay(1);
        turn(-20);
        Greenfoot.delay(1);
        turn(-20);
        Greenfoot.delay(1);
        turn(-280);
        
}
}
h123n h123n

2012/5/11

#
Thanks, but could I make it so that humans destroyed is a number? please can someone answer. ;)
Julian Julian

2012/5/11

#
A boolean by definition can only ever be True or False. If you wish a number then use "int" . private int humansDestroyed = 10; // Create and set total human population. then . humansDestroyed--; // Reduce humans by 1. and finally . if (humansDestroyed <=0) {do stuff} // When no humans are left.
danpost danpost

2012/5/12

#
If you use the method described by Julian, you may want to change the name of the variable to something more appropriate, like 'humansRemaining' or 'humansAlive' or 'humanPopulation'.
h123n h123n

2012/5/12

#
thanks that helps
You need to login to post a reply.