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

2025/2/13

how do I program bubbles shooting

yumyumlover69 yumyumlover69

2025/2/13

#
I want to shoot bubbles out of my Bubblegun, but i don't know how I need to program that
danpost danpost

2025/2/13

#
yumyumlover69 wrote...
I want to shoot bubbles out of my Bubblegun, but i don't know how I need to program that
I would start with this:
import greenfoot.*;

public class BubbleGun extends Actor
{
    protected GreenfootImage bubbleImage = new GreenfootImage("bubble.png");
    private boolean spaceDown = false;
    
    public void act() {
        // shooting
        if (spaceDown != Greenfoot.isKeyDown("space")) {
            spaceDown = !spaceDown;
            if (spaceDown) {
                Bubble bubble = new Bubble();
                getWorld().addObject(bubble, getX()+0, getY()+0); // adjust offsets ass needed
            }
        }

       /**  other gun actions here  */
    }
    
    private class Bubble extends Actor
    {
        protected void addedToWorld(World world) {
            setRotation(BubbleGun.this.getRotation());
            setImage(bubbleImage);
        }
        
        public void act() {
            move(5);
        }
    }
}
and ... yes, one class is within the other as shown. Each Bubble object belongs to a specific BubbleGun object.
You need to login to post a reply.