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

2018/1/8

shooting in 4 directions

Lunatale Lunatale

2018/1/8

#
For my school project I need my main character that moves right,left, down and up with shooting keys K,M,O,L to shoot in four directions. my actor is a car, and i need to shoot from the front and back from the left right sides. this my existing code: /** * keyboard control */ public void keyMove() { if (Greenfoot.isKeyDown("up")) { setLocation(getX(), getY()-12); } if (Greenfoot.isKeyDown("down")) { setLocation(getX(), getY()+12); } if (Greenfoot.isKeyDown("left")) { setLocation(getX()-30, getY()); } if (Greenfoot.isKeyDown("right")) { setLocation(getX()+12, getY()); } if(Greenfoot.isKeyDown("k")) { getWorld().addObject(new Fla (), getX(), getY()); (Front left ammo) } if(Greenfoot.isKeyDown("l")) { getWorld().addObject(new Rba (), getX(), getY()); (Right back ammo) } if(Greenfoot.isKeyDown("m")) { getWorld().addObject(new La (), getX(), getY()); (Left ammo) } if(Greenfoot.isKeyDown("o")) { getWorld().addObject(new Fra (), getX(), getY()); (Front right ammo) } // kpoliceFire(Greenfoot.isKeyDown("K")); // if(Greenfoot.isKeyDown("K")) // { // fire(); // } } This is my existing bullet codes: Rba public void act() { setLocation(getX()-2, getY()); if (isAtEdge()) getWorld().removeObject(this); } La { setLocation(getX()-40, getY()); // Touching(); if (isAtEdge()) getWorld().removeObject(this); } Fla { setLocation(getX()+20, getY()); if (isAtEdge()) getWorld().removeObject(this); } { setLocation(getX()+16, getY()); if (isAtEdge()) getWorld().removeObject(this); } O dont know the right code and its really bothering me. Thanks!
danpost danpost

2018/1/9

#
Shoot all four directions at the same time?
Lunatale Lunatale

2018/1/10

#
It would happen if all the firing keys were pressed at the same time.
danpost danpost

2018/1/10

#
Code each key separately. Use a timer for each to regulate the shots; or, use a boolean to track the state of each key to fire when a key is going from an up state to a down state.
You need to login to post a reply.