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

2013/2/26

Firing Balls In Pulses

forteddyt forteddyt

2013/2/26

#
Hey, I'm trying to create a simple program were I can move around with the WASD keys and shoot balls from me, to whatever direction I'm facing, with "Space". I've got those two parts down but what I realize is that when I shoot the Balls, it keeps shooting them until I let go of space (in a long line). However, I want it to shoot in pulses if I hold the space bar down. To do this I thought I should use the "getObjectInRange()" method (were in an if statement it asks if a ball is within my Characters range, then if it is, dont shoot. While if a ball is not in my Characters range AND the "Space" is down, shoot). Here is my code for the shoot : if(Greenfoot.isKeyDown("space") ) { getWorld().addObject(new Ball(getRotation()), getX(), getY()); } (I've tried adding the "getObjectInRange()" but it doesnt work) The code in the middle works fine for the thing im trying to do. If you need any more info just ask. Any help at all will be appreciated, but please take into consideration that Im new to programming and I need stuff to be dumbed-down. Thanks :)
davmac davmac

2013/2/26

#
What do you mean by this:
I've tried adding the "getObjectInRange()" but it doesnt work
What code did you try exactly?
forteddyt forteddyt

2013/2/26

#
if(Greenfoot.isKeyDown("space") && getObjectInRange(100, Characters.class) != null) Im not sure how the getObjectInRange() method works, I'm just going off google right now. (Im assuming that if something is within my Characters.class 100 radius, it will return null) 'Characters' being the class my Ball shoots out of.
ScratchMan ScratchMan

2013/3/1

#
I found another post that may help - http://www.greenfoot.org/topics/find/13928#post_13928 It has some 'private int' values to slow down firing but i to am relativley new to Greenfoot and don't understand most of it!
davmac davmac

2013/3/1

#
if(Greenfoot.isKeyDown("space") && getObjectInRange(100, Characters.class) != null)
There is no 'getObjectInRange' method. There is a 'getObjectsInRangeMethod' which returns a list. Try this:
if(Greenfoot.isKeyDown("space") && ! getObjectInRanges(100, Characters.class).isEmpty())
danpost danpost

2013/3/1

#
davmac meant:
if (Greenfoot.isKeyDown("space") && !getObjectsInRange(100, Characters.class).isEmpty())
davmac davmac

2013/3/1

#
I did indeed :)
You need to login to post a reply.