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

2012/4/20

how do I create a standard variable?

h123n h123n

2012/4/20

#
I am sort of new and I have a game where you play as a bee who fires nectar.However the nectar can only go right,but I want it to go to whatever direction the bee is facing.Here is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bee here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bee extends Actor
{
    /**
     * Act - do whatever the bee wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(4);
        if (Greenfoot.isKeyDown ("left"))
        {
           turn(-4);
    }    
    if (Greenfoot.isKeyDown ("right"))
    {
        turn(4);

}
if (Greenfoot.isKeyDown ("up"))
{
World world;
world = getWorld();
world.addObject (new nectar(), getX(), getY());
}
}
}
And the code for nectar is;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
I need some sort of variable or boolean to find the direction the bees facing
 * Write a description of class nectar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class nectar extends Actor
{
    /**
     * Act - do whatever the nectar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       move (6);
    }    
}
Denzien Denzien

2012/4/20

#
Yeah I'm not sure myself, but it includes setting the rotation of the nectar the same as the rotation of the bee Try download a scenario that includes the thing you want "Mic's Asteroids" is a good one to look at to get an idea.
danpost danpost

2012/4/20

#
The easiest way to do this, is to create a constructor for 'nectar' that recieves the 'bee' object.
public nectar(bee b)
{
    setRotation(b.getRotation());
}
and the 'bee object can create it with
getWorld().addObject(new nectar(this), getX(), getY());
h123n h123n

2012/4/21

#
Thanks danpost and Denzein! now my bee can actually fire nectar anywhere!
You need to login to post a reply.