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

2013/2/16

Boolean help

1
2
Gingervitis Gingervitis

2013/2/16

#
I am trying to make a boolean statement that will make it so when my SpaceTurtle2 class eats the TurtleBlaster class, it will make the turtle shoot a different blaster bullet. I don't even know where to start....
danpost danpost

2013/2/16

#
You should already have the boolean statement (if you are using Animal class with 'canSee' and 'eat' methods):
if(canSee(TurtleBlaster.class))
{
    eat(TurtleBlaster.class);
    getWorld().addObject(new BlasterBullet(), x, y); // add a line like this here
}
Gingervitis Gingervitis

2013/2/16

#
I think you misunderstood me. I should have been more clear. I can create new BlasterBullets fine. What is already happening is that when you press the spacebar, the turtle will shoot the BlasterBullet class. In level 10, the turtle picks up a new turtle blaster. When that happens, I want a different bullet class that has a completely different function. The first BlasterBullet class kills zombies. I want the second BlasterBullet class to turn the zombies back to human. Is this confusing? I want a way to switch between the different BlasterBullets. That is why I asked about a boolean statement......
danpost danpost

2013/2/17

#
Ok, I see now. You need a boolean field to track the kind of BulletBlaster the turtle has.
// add this instance boolean field
private boolean isBlasterType2;
// then to shoot
if (isBlasterType2)
{
    getWorld().addObject(new BlasterBullet2(), getX(), getY());
}
else
{
    getWorld().addObject(new BlasterBullet(), getX(), getY());
}
// and when eating a TurtleBlaster object
if(canSee(TurtleBlaster.class))
{
    eat(TurtleBlaster.class);
    isBlasterType2=true;
}
Gingervitis Gingervitis

2013/2/17

#
Is there a way to make it to switch between the two BlasterBullet classes when the "z" key is pressed?
Gingervitis Gingervitis

2013/2/17

#
Regarding your previous post, will I need to change my method that makes the spacebar shoot the first BlasterBullet?
danpost danpost

2013/2/17

#
To toggle between the two types of bullets:
// in the act method (or a method it calls)
if("z".equals(Greenfoot.getKey()) isBlasterType2= !isBlasterType2;
You can use the same code to shoot by putting lines 4 through 11 in place of your shooting code.
Gingervitis Gingervitis

2013/2/17

#
I put in that code but it didn't work.
danpost danpost

2013/2/17

#
I will have to see what your class looks like at this point.
Gingervitis Gingervitis

2013/2/17

#
I had to put the // marks in front of it so it wouldn't have a function because it wouldn't even shoot anything with it there. Here is the code. I put it in the 'checkKeys' method.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SpaceTurtle2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SpaceTurtle2 extends Animal
{
    private int tbEaten;
    private boolean isBlasterType2;
    
    public SpaceTurtle2()
    {

    }

    public void Boss2()
    {

    }

    public void addedToWorld(World world)
    {
        if (world instanceof Boss2)
        {
            //if (getY()<370) setLocation(getX(), getY() + 1);
            if (getX()<370) setLocation(getX()+1, getY());
        }
    }

    /**
     * Act - do whatever the SpaceTurtle2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {

        move(3);
        checkKeys();
        ifCanSeeArrow();
        ifCanSeeFactory();

        shoot1();
        
    }    

    /**
     * Check if keys are being pressed. Press left turn left. 
     * Press right turn right.
     */
    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-5);
        }

        if (Greenfoot.isKeyDown("right"))
        {
            turn(5);
        }
        
        //if("z".equals(Greenfoot.getKey())) isBlasterType2= !isBlasterType2;

    }

    public void shoot1()
    {
        if (isBlasterType2)  
        {  
            if ("space".equals(Greenfoot.getKey()))
            {
                shoot2();
            }
            //getWorld().addObject(new BlasterBullet2(), getX(), getY());  
        }  
        else  
        {  
            if ("space".equals(Greenfoot.getKey()))
            {
                shoot();
            }
            //getWorld().addObject(new BlasterBullet(), getX(), getY());  
        }  
        // and when eating a TurtleBlaster object  
        if(canSee(TurtleBlaster.class))  
        {  
            eat(TurtleBlaster.class);  
            tbEaten++;
            isBlasterType2=true;  
        } 
        
    }

    
    public void ifCanSeeFactory()
    {
        if (canSee(Factory.class))
        {
            Greenfoot.setWorld(new Zombies3());
        }
    }

    public void ifCanSeeArrow()
    {
        if (canSee(Arrow.class))
        {
            Greenfoot.setWorld(new Zombies1());
        }

        if (canSee(Arrow2.class))
        {
            Greenfoot.setWorld(new Zombies2());
        }

        if (canSee(Arrow3.class))
        {
            Greenfoot.setWorld(new Zombies4());
        }

        if(canSee(Arrow4.class))
        {
            Greenfoot.setWorld(new Boss2());
        }

        if (canSee(Arrow5.class))
        {
            Greenfoot.setWorld(new Moon2());
        }

        if (canSee(Arrow6.class))
        {
            Greenfoot.setWorld(new Moon3());
        }

        if (canSee(Arrow7.class))
        {
            Greenfoot.setWorld(new Moon4());
        }

    }

    public void shoot()
    {
        BlasterBullet blasterBullet = new BlasterBullet();
        getWorld().addObject(blasterBullet, getX(), getY());
        blasterBullet.setRotation(getRotation());
    }

    public void shoot2()
    {
        BlasterBullet4 blasterBullet4 = new BlasterBullet4();
        getWorld().addObject(blasterBullet4, getX(), getY());
        blasterBullet4.setRotation(getRotation());
        blasterBullet4.setImage("BBPurge.png"); 
    }

}
danpost danpost

2013/2/17

#
Ok, you were calling Greenfoot.getKey() in two different places in the class; both being executed during the same 'act' cycle. The second call will always return null, so when calling it the first time, there is a need to save what is returned in a local variable. That way you can refer to its value more than one time. I created another method (ifCanSeeTurtleBlaster) because it really had nothing to do with 'checkKeys'. Also, I removed two of your shoot methods, combining the code in one 'shoot' method. Finally, maybe I am not aware of something, but moving the actor (SpaceTurtle2 object) one pixel to the right if anywhere to the left of 370 along the x-axis does not seem to accomplish much (if anything). I am thinking that you may not need that method; but, I could be wrong.
import greenfoot.*; 

public class SpaceTurtle2 extends Animal
{
    private int tbEaten;
    private boolean isBlasterType2;
    
    public SpaceTurtle2()
    {
    }

    public void addedToWorld(World world)
    { // I cannot think of a reason for this method (for what it does)
        if (world instanceof Boss2)
        {
            //if (getY()<370) setLocation(getX(), getY() + 1);
            if (getX()<370) setLocation(getX()+1, getY());
        }
    }

    public void act() 
    {
        move(3);
        checkKeys();
        ifCanSeeTurtleBlaster();
        ifCanSeeArrow();
        ifCanSeeFactory();
    }    

    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("left")) turn(-5);
        if (Greenfoot.isKeyDown("right")) turn(5);
        String key=Greenfoot.getKey();
        if (key == null) return;
        if ("z".equals(key)) isBlasterType2= !isBlasterType2;
        if ("space".equals(key)) shoot();
    }

    public void shoot()
    {
        Actor blaster = null;
        if (isBlasterType2) 
        {
            blaster = new BlasterBullet4();
            blaster.setImage("BBPurge.png"); 
        }
        else
        {
            blaster = new BlaserBullet();
        }
        blaster.setRotation(getRotation());
        getWorld().addObject(blaster, getX(), getY());  
    }

    private void ifCanSeeTurtleBlaster()
    {
        if(canSee(TurtleBlaster.class))  
        {  
            eat(TurtleBlaster.class);  
            tbEaten++;
            isBlasterType2=true;  
        }
    }

    public void ifCanSeeFactory()
    {
        if (canSee(Factory.class)) Greenfoot.setWorld(new Zombies3());
    }

    public void ifCanSeeArrow()
    {
        if (canSee(Arrow.class)) Greenfoot.setWorld(new Zombies1());
        if (canSee(Arrow2.class)) Greenfoot.setWorld(new Zombies2());
        if (canSee(Arrow3.class)) Greenfoot.setWorld(new Zombies4());
        if (canSee(Arrow4.class)) Greenfoot.setWorld(new Boss2());
        if (canSee(Arrow5.class)) Greenfoot.setWorld(new Moon2());
        if (canSee(Arrow6.class)) Greenfoot.setWorld(new Moon3());
        if (canSee(Arrow7.class)) Greenfoot.setWorld(new Moon4());
    }
}
Also, I did not include the Boss2 method as it was not being called and does not do anything, anyway (at least, as yet).
Gingervitis Gingervitis

2013/2/17

#
Thank you!!! It worked. Is there a way to make it so that pressing the "z" key will only make it switch if it has eaten the TurtleBlaster class in Boss2 world? Here is my revised code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SpaceTurtle2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SpaceTurtle2 extends Animal
{
    private int tbEaten;
    private boolean isBlasterType2;
    
    public SpaceTurtle2()
    {

    }

    /**
     * Act - do whatever the SpaceTurtle2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(3);
        checkKeys();
        ifCanSeeTurtleBlaster();
        ifCanSeeArrow();
        ifCanSeeFactory();        
    }    

    /**
     * Check if keys are being pressed. Press left turn left. 
     * Press right turn right.
     */
    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("left")) turn(-5);  
        if (Greenfoot.isKeyDown("right")) turn(5);  
        String key=Greenfoot.getKey();  
        if (key == null) return;  
        if ("z".equals(key)) isBlasterType2= !isBlasterType2;  
        if ("space".equals(key)) shoot();
    }

    public void shoot()
    {
        Actor blaster = null;  
        if (isBlasterType2)   
        {  
            blaster = new BlasterBullet4();  
            blaster.setImage("BBPurge.png");   
        }  
        else  
        {  
            blaster = new BlasterBullet();  
        }  
        blaster.setRotation(getRotation());  
        getWorld().addObject(blaster, getX(), getY());  
    }
    
    private void ifCanSeeTurtleBlaster()  
    {  
        if(canSee(TurtleBlaster.class))    
        {    
            eat(TurtleBlaster.class);    
            tbEaten++;  
            isBlasterType2=true;  
            
        } 
        
    }

    
    public void ifCanSeeFactory()
    {
        if (canSee(Factory.class))
        {
            Greenfoot.setWorld(new Zombies3());
        }
    }

    public void ifCanSeeArrow()
    {
        if (canSee(Arrow.class))
        {
            Greenfoot.setWorld(new Zombies1());
        }

        if (canSee(Arrow2.class))
        {
            Greenfoot.setWorld(new Zombies2());
        }

        if (canSee(Arrow3.class))
        {
            Greenfoot.setWorld(new Zombies4());
        }

        if(canSee(Arrow4.class))
        {
            Greenfoot.setWorld(new Boss2());
        }

        if (canSee(Arrow5.class))
        {
            Greenfoot.setWorld(new Moon2());
        }

        if (canSee(Arrow6.class))
        {
            Greenfoot.setWorld(new Moon3());
        }

        if (canSee(Arrow7.class))
        {
            Greenfoot.setWorld(new Moon4());
        }

    }

    

}
danpost danpost

2013/2/17

#
if ("z".equals(key) && tbEaten>0)
Gingervitis Gingervitis

2013/2/17

#
Where do I place that?
danpost danpost

2013/2/17

#
Part of line 42.
There are more replies on the next page.
1
2