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

2012/12/6

How to have more than one key being used at once?

bbwf bbwf

2012/12/6

#
I have a game where an Aligator runs around trying to kill the frogs by shooting lemons. I am unable to move and shoot at the same time, is there any way I can fix this?
bbwf bbwf

2012/12/6

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Aligator here. * * @author (your name) * @version (a version number or a date) */ public class Aligator extends Mover { int shootdelay=0; int mindelay=5; public void act() { //Sets the rocket as a smaller image. GreenfootImage img = getImage(); img.scale(120,32); setImage(img); int w=getWorld().getWidth(); int h=getWorld().getHeight(); setLocation((getX()+w)%w, ((getY()+h)%h)); shootdelay++; if(Greenfoot.isKeyDown("up")) { move(5); } if (Greenfoot.isKeyDown("left")) { turn(-4); } if(Greenfoot.isKeyDown("right")) { turn(4); } if(Greenfoot.isKeyDown("space")) { shoot(); } } //Shoots the bullet public void shoot() { if(shootdelay >= mindelay) { int rot = getRotation()-10; int xOffset = (int)(40 *Math.cos(Math.toRadians(rot))); int yOffset = (int)(40 *Math.sin(Math.toRadians(rot))); Bullet b = new Bullet(getRotation()); getWorld().addObject(b, getX()+xOffset, getY()+yOffset); shootdelay = 0; } } } Here is the code, it would not let me use the code tab.
danpost danpost

2012/12/6

#
First, move the image scaling code to a constructor method. It is something you only need to do once; not every act cycle.
// add the constructor
public Aligator()
{
    // Sets the rocket as a smaller image.
    getImage().scale(120,32);
}
Next, move the world-wrapping code within the 'up' key check code. The only time you need to check for world wrapping is when the rocket moves.
if(Greenfoot.isKeyDown("up"))
{
    move(5);
    int w=getWorld().getWidth();
    int h=getWorld().getHeight();
    setLocation((getX()+w)%w, ((getY()+h)%h));
}
As far as not being able to move and shoot at the same time, I do not see any problem with the code given above. If your code tab does not work, you can put before your code and after it (without the spaces between each character).
nccb nccb

2012/12/7

#
Could we see the collision code for the bullet? Is it possible that when you move and shoot, the bullet hits the alligator? Otherwise, I can't see a problem with the code as you've given it.
bbwf bbwf

2012/12/7

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bullet here. * * @author (your name) * @version (a version number or a date) */ public class Bullet extends Mover { public Bullet(int rotation) { setRotation(rotation); GreenfootImage img = getImage(); img.scale(20,20); setImage(img); } public void act() { //Sets our image as a smaller size. GreenfootImage img = getImage(); img.scale(20,20); setImage(img); move(20.0); if(atSide()) {getWorld().removeObject(this);} } //Checks to see if the bullet as at the edge of the screen. If it is, remove the bullet. public boolean atSide() { if(getX() == 0 || getX()==849) return true; if(getY()==0 || getY()==549) return true; else return false; } }
vonmeth vonmeth

2012/12/7

#
Like danpost posted above, you can remove the following from the act method in your bullet code as well (it is already done in the constructor).
        //Sets our image as a smaller size.
        GreenfootImage img = getImage();
        img.scale(20,20);
        setImage(img);
So the bullet fires off fine when you are not moving? I don't see why it wouldn't work while moving.
You need to login to post a reply.