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

2014/4/11

Starfighter code not working

GamesGrinder1998 GamesGrinder1998

2014/4/11

#
For some reason, my code for my starfighter is not working in the multiplayer version. I want it to shoot iceballs, but when i press the designated key ("j") it doesn't work...can someone please help, here is the code for my starfighter
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Starfighter2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Starfighter2 extends Player
{
    private int x;
    private int y;
    
    public void act() 
    {
        checkKeys();
        atWorldEdge(); 
        stopAtWalls();
    }

    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("a"))
        {
            move(-5);
        }
        if (Greenfoot.isKeyDown("d"))
        {
            move(5);
        }   
        if("j".equals(Greenfoot.getKey()))
        {
            getWorld().addObject(new Iceball(getRotation()), getX(), getY());
        }    
    }

    public void stopAtWalls()
    {  
        if(getOneIntersectingObject(Wall.class)!=null)
        {
            setLocation(x,y);
        }
        else
        {
            x = getX();
            y = getY();
        }    
    }  
}
if it helps, this is my iceball code
import greenfoot.*;  

public class Iceball extends Player
{
    private int direction, speed;

    public Iceball(int dir)
    {
        direction = 270;
        speed = 10;
    }

    public void act() 
    {
        setRotation(direction);
        move(speed);
        disappear();
    }    

    public void disappear()
    {
        Actor BugEnemy;
        BugEnemy = getOneObjectAtOffset(0, 0, BugEnemy.class);
        Actor InsectEnemy;
        InsectEnemy = getOneObjectAtOffset(0, 0, InsectEnemy.class);
        Actor Bee;
        Bee = getOneObjectAtOffset(0, 0, Bee.class);
        Actor DarkPower;
        DarkPower = getOneObjectAtOffset(0, 0, DarkPower.class);
        if (BugEnemy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(BugEnemy);
            MyGame2 mygame2= (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            world.removeObject(this);
            return;
        }    

        if (InsectEnemy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(InsectEnemy);
            MyGame2 mygame2 = (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            world.removeObject(this);
            return;
        } 
       
        if (Bee != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Bee);
            MyGame2 mygame2 = (MyGame2)world;
            Counter2 counter2 = mygame2.getCounter2();
            counter2.addScore();
            world.removeObject(this);
            return;
        } 
        
        if (DarkPower != null)
        {
            World world;
            world = getWorld();
            world.removeObject(DarkPower);
            world.removeObject(this);
            return;
        } else {
            if(atWorldEdge())
            {
                getWorld().removeObject(this);
                return;
            } 
        }
    }   
}    
davmac davmac

2014/4/11

#
Do you have two classes that both call Greenfoot.getKey()? (perhaps the StarFighter1 class also does?) This generally doesn't work. Each time you call the method it "consumes" the key. So if two actors call it, only one of them will see the key that was typed. In this case you need to use Greenfoot.isKeyDown(...) instead, or call Greenfoot.getKey() only once and save the result so you can use it from other actors.
GamesGrinder1998 GamesGrinder1998

2014/4/12

#
davmac wrote...
Do you have two classes that both call Greenfoot.getKey()? (perhaps the StarFighter1 class also does?) This generally doesn't work. Each time you call the method it "consumes" the key. So if two actors call it, only one of them will see the key that was typed. In this case you need to use Greenfoot.isKeyDown(...) instead, or call Greenfoot.getKey() only once and save the result so you can use it from other actors.
Yeah, my other starfighter has greenfoot.getkey....the reason I don't want to use isKeyDown is cos a string of bullets will fire down, I don't get it when you say save it so other actors cans use it, do you mean save under another class???
danpost danpost

2014/4/12

#
You can detect those specific keys from the world act method. The action to take can be done right there in the world when detected or you can call a method on whichever object the key is used for to perform the action. The other option is to use a boolean field to track the state of those specific keys and use a double-condition to trigger and reset. Let us say, with a field called 'shotKeyDown':
if (!shotKeyDown && Greenfoot.isKeyDown("j"))
{ // to fire
    getWorld().addObject(new Iceball(getRotation()), getX(), getY());
    shotKeyDown = true;
}
if (shotKeyDown && !Greenfoot.isKeyDown("j"))
{ // to reset
    shotKeyDown = false;
}
GamesGrinder1998 GamesGrinder1998

2014/4/12

#
danpost wrote...
You can detect those specific keys from the world act method. The action to take can be done right there in the world when detected or you can call a method on whichever object the key is used for to perform the action. The other option is to use a boolean field to track the state of those specific keys and use a double-condition to trigger and reset. Let us say, with a field called 'shotKeyDown':
if (!shotKeyDown && Greenfoot.isKeyDown("j"))
{ // to fire
    getWorld().addObject(new Iceball(getRotation()), getX(), getY());
    shotKeyDown = true;
}
if (shotKeyDown && !Greenfoot.isKeyDown("j"))
{ // to reset
    shotKeyDown = false;
}
i got an error saying cannot find symbol - variable shotKeyDown here is what i wrote down in my starfighter 2 code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Starfighter2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Starfighter2 extends Player
{
    private int x;
    private int y;
    
    public void act() 
    {
        checkKeys();
        atWorldEdge(); 
        stopAtWalls();
        shotKeyDown();
    }

    private boolean shotKeyDown()
    {
        if(!shotKeyDown && Greenfoot.isKeyDown("j"))
        {
            getWorld().addObject(new Iceball(getRotation()), getX(), getY());
            shotKeyDown = true;
        }   
        if (shotKeyDown && !Greenfoot.isKeyDown("j"))  
        { 
            shotKeyDown = false;  
        }    
    }

    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("a"))
        {
            move(-5);
        }
        if (Greenfoot.isKeyDown("d"))
        {
            move(5);
        }      
    }

    public void stopAtWalls()
    {  
        if(getOneIntersectingObject(Wall.class)!=null)
        {
            setLocation(x,y);
        }
        else
        {
            x = getX();
            y = getY();
        }    
    }  
}
danpost danpost

2014/4/12

#
You cannot track anything until you declare the field. In the class, but outside any method, write:
private boolean shotKeyDown;
GamesGrinder1998 GamesGrinder1998

2014/4/13

#
danpost wrote...
You cannot track anything until you declare the field. In the class, but outside any method, write:
private boolean shotKeyDown;
thankyou so much, it works now
You need to login to post a reply.