Hello. I have a bubble in my shooter (bubbe shooter game) and i want that bubble to move when the space bar is pressed. My current code is this:
public Bubble(int radius, double xVel, double yVel)
{
mapBubbles();
this.radius = radius;
xSpeed = xVel;
ySpeed = yVel;
}
protected void addedToWorld(World w)
{
xLoc = getX();
yLoc = getY();
}
/**
* removes all bubbles that are touching "this" ball that match
* the specified color. Also removes "this" ball.
*
* Although you may modify this if you wish to, doing so
* is HIGHLY inadvisable.
*
* @param targetColor the color to look for when removing bubbles
**/
public void removeAdjacent(Color targetColor)
{
// make sure we haven't aready been removed ...
if (getWorld()==null)
return;
// get ALL bubbles we are actually touching
List<Bubble> touching = getObjectsInRange(2*Bubble.RADIUS,Bubble.class);
// remove this bubble ...
getWorld().removeObject(this);
// look at each bubble we were touching ...
for(Bubble b: touching)
{
// if the current bubble matches our color ...
if (b.col.equals(targetColor))
// ... repeat this process on the matching colored bubble
b.removeAdjacent(targetColor);
}
}
/**
* Act - do whatever the Bubble wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
String key = Greenfoot.getKey();
if(key!=null && key.equals("space"))
{
getWorld().addObject(new Bubble(0,0,0), 200, 650);
xLoc += xSpeed;
yLoc += ySpeed;
World w = getWorld();
if (xLoc < 0) xLoc = w.getWidth()-1;
else if (xLoc> w.getWidth()-1) xLoc=0;
else if (yLoc> w.getHeight()-1) yLoc=0;
List<Bubble> touchingBlobs = getIntersectingObjects(Bubble.class);
for (Bubble otherBlob : touchingBlobs)
{
radius = Math.sqrt(radius*radius + otherBlob.radius*otherBlob.radius );
mapBubbles();
xLoc = (xLoc + otherBlob.xLoc)/2;
yLoc = (yLoc + otherBlob.yLoc)/2;
}
setLocation((int) xLoc,(int)yLoc);
}
}
}