Help i have no idea how the gun in my game shoot bullets

#1

i am making a game that you a human is holding a gun that you can aim that you shoot to destroy meteors

i have already prepered things like rotation and menus but have no idea how to to make spirtes so my gun can shoot and make it so that

i know it can be done since someone made a game non newgrounds called puking duck
where the duck pukes(shoots) out circles
so if anyone knows how to do it can you teach me pls :slight_smile:

1 Like

#2

Hey @mlgcoolguys_1, welcome to the forum!

The interaction you’re looking for here is the object.clone() method.

If you have a character/object on screen, you can name it, then us the clone method like this to get a copy.

// Cloning
var myClone = object.clone();

You can move that clone around, or give it more information to use, by saying cloneName.property, like this:

// Move the clone to 50, 100    
myClone.x = 50;
myClone.y = 100; 

// Give the clone a "speed" value to use later.
myClone.speed = 5;

I’m attaching an example that shows you how to then allow objects to interact with clones. The easiest way is by using hitTest on a clone and another object. You can do this, by looking through all of the clones of an object like this:

// Go through all clones and see if they hit me.
for (var c of myClone.clones) {
    if (this.hitTest(c)) {
        this.remove(); // Delete this object
        return; // Stop checking.
    }
}

In the example, I delete rain drops, and allow other types of clones to play an animation, but you can really do anything! (I need to add this example to the website, it’s super handy!)

We’re happy to help, just reach out if you need any more assistance!

CloningDemo.wick (58.1 KB)

0 Likes

#5

so like this?
if (key === “space”){
var bullet = object.clone(1);
bullet.x = 50
bullet.y = 100
bullet.speed = 5
for (var c of bullet.clones) {
if (this.hitTest©) {
this.remove(1);
return;
}
}

}

0 Likes

#6

Hello! I am the creator of the game you mentioned called “Puking Bird”.
There is a simpler way to shoot bullets without using the clone function.
I used a tween to make the bullet go 200 units up.
Here’s how I did it.
First, I’ll show the timeline.
image
This is the timeline for the mode in Puking Bird where you can shoot.
I’ll go layer by layer explaining what these frames do.
First, there’s the top layer which covers all of the frames below it.
image
The highlighted frame is the gameplay frame.
This is where the bird and meteors are.
The frame is as long as all of the frames below it combined so no matter what frame it’s on, the gameplay is still showing.
The code for this frame is as follows:
stop()
That’s all it is.
When the game ever goes into that specific spot, it stops.
Now, it’s time to move on to the second layer.
image
The first frame is simple enough.
In this frame, the bullet is following the bird, hiding behind it.
The bullet has this code for it:
Every tick on this frame, this code is ran:
this.x = bird.x
this.y - bird.y

When a key is pressed, this code is ran:
if (key === “space”) {
project.pukego = 1
}

This makes project.pukego = 1, and to explain what that does, I’ll show you what the code for the frame does.
This code is ran on this frame:
Every tick, this code is ran on this frame:
if (project.pukego === 1) {
project.birdlast = bird.x
gotoAndPlay(7)
}

By default, project.pukego = 0.
When project.pukego = 1, it saves the bird’s x coordinate and then it goes to frame 7 and plays.
Now, we move on to this frame.
image
The bullet only has one line of code.
Here is the code:
Every tick on this tween, this code is ran:
this.x = project.birdlast
On the last frame, when space was pressed, it set project.birdlast to the bird’s x coordinate when space was pressed.
This means that the bullet will travel in a straight line without moving to the left or the right because of the tween.
image
This frame has these lines of code:
project.pukego = 0
gotoAndStop(6)

So, it resets project.pukego and goes to frame 6, or the start.
Now it’s time to discuss the last layer and the last frame (with code).
image
Every time that it enters that frame, this code is ran:
project.birdlast = bird.x
The gameplay is still running through this whole thing, so when it goes back to the frame where it started, the bullet starts behind the bird.
If this is too confusing for you, then I linked the .wick file if you want to inspect it.
Puking Bird (2).wick (20.6 KB)

2 Likes

#7

Great suggestion @DylanExists! This is also a totally valid way of making projectiles / moving objects in Wick Editor!

1 Like

#8

does this work with rotation as well
and i also need help for making the metetors move to the left

0 Likes

#9

yes, it does work with rotation

0 Likes

#10

sorry for the wait but my i know where you hid the animation on the cloning demo
i also want to put a thing in my code so that the player cant scam click and shoot many times and glitch my game (according to my tests)

0 Likes

#11

For the first one, when you select a clip, there’s a button called “Edit Timeline”. That’s where I hid the animation.
For the second one, do you mean that you want to have a period between when the bullet ends and when you can fire again, sort of like a reloading time? You can do that. I’m going to refer to the explanation I used on the first comment I posted on this thread. You can add an empty frame between the 2nd and 3rd frame on the 2nd layer and elongate it to the amount of time you need. Just make sure that all of the other frames on the other layers line up.

1 Like