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

2020/4/26

how do i make an object fire another one

ninjapuffin ninjapuffin

2020/4/26

#
I don't know how to make an object fire another one
danpost danpost

2020/4/26

#
ninjapuffin wrote...
I don't know how to make an object fire another one
Did you do any searches? What have you tried? Show attempted codes.
RcCookie RcCookie

2020/4/26

#
Well, I don't know the exact circumstances you have, but here is my general idea:
public class Player extends Actor{
    public void shoot(){
        Bullet b = new Bullet();//create a new bullet instance
        getWorld().addObject(b, getX(), getY());//add that bullet to the players location
        b.setRotation(getRotation);//set the bullets rotation to the direction the player is facing
    }
}

public class Bullet extends SmoothMover{
    private final static int SPEED = 10;//the movementspeed of all bullets
    public void act(){
        move(SPEED);//moves every frame
        if(isAtEdge())getWorld().removeObject(this);//removes itself if it is at the edge of the world
    }
}
The bullet class should extend the smoothmover class to allow more precise shooting angles, especially on low speeds. For example, at a apeed of 1 the bullet could only move in 45° angles. To use the smooth mover class, simply right-click onto the classes background panel in Greenfoot, select "Import class" and the "Smooth mover".
ninjapuffin ninjapuffin

2020/4/27

#
i combined both your ideas and it worked thanks guys
You need to login to post a reply.