Making a gun that shoots
-
@AtomAlexander are we talking 2D or 3D here? Since you mentioned sprites I'm assuming this is a 2D game using the sprite engine?
If the game is 2D, it is also worth mentioning if your idea is top-down or side-scrolling - this will affect how the direction for the bullet needs to be applied.
-
Side scrolling and 2 d I'm a sprite based guy not good enough at coding to make 3d stuff lol 😂
-
So you just want to add a gun into the animation right?
-
Yes but I only want the gun to appear when sprite is aiming. (like only when holding be and then only fire when shoulder button is pressed for example)
-
Part 2 of the video tutorial on sprites gets to this near the end.
State variable within an array of animation data? Or something -
OK I'll re-watch that video
-
@AtomAlexander youl have different states for your sprite(character)
like ;
idle = 0
walk= 1ect
and then your have your animation for those statesso add a state
draw gun = 2
then for your animation choose the tiles with that state in
for shooting check out a sample or users project and see how they did it -
Everyone's correct here :) You'll need a a few different states and that should manage everything you need.
Are you planning on drawing the sprites yourself with the image editor or using the built-in assets? I'm also wondering what you mean by "aiming"..
If you want to simply shoot straight across the screen, you'll need only to increase the bullet's
x
position.If you want to "aim" and shoot in a direction, it's a little different. Still simple, but it involves using something called
normalize()
. I'm super happy to explain it here, but I won't overload you with info that isn't relevant to what your goal is!Let me know and I'll put together a post.
-
I'm making my own sprites and yes the bullets will only be traveling along the x axis I call it among because I don't want h to have the gun out and showing unless you press the button to upholster it. Also the gun it's a different separete sprite
-
Alright - well it sounds like you'd make us of
setSpriteVisibility()
on the gun itself, so you might have a button press which sets the visibility of the gun sprite. Let's say you had (in this example code I'm assuming your gun sprite is create and stored in "gunSprite":In your game loop:
if c.zl then playerSprite.aim = true // set a flag in the sprite properties so the game knows the sprite is aiming setSpriteVisibility(gunSprite, true) else playerSprite.aim = false setSpriteVisibility(gunSprite, false) endif
This would be alright, but the problem would be that for every frame the ZL button is held, you are setting visibility and the aiming flag. An improvement could make use of the "rising and falling edge" concept in the sprite tutorial part 2 video:
You'll need a global controls and "old" controls variable:
c = controls(0) old_c = c
You will also want to make sure your gunSprite visibility and player aiming property are set to false before you enter the main loop:
setSpriteVisibility(gunSprite, false) playerSprite.aim = false
Then, in your main loop
// before you do anything with controls, read them: c = controls(0) // check if ZL is pressed and wasn't pressed on the previous frame (when you initially press the button): if c.zl and !old_c.zl then playerSprite.aim = true setSpriteVisibility(gunSprite, true) endif // check to see if ZL is not held and was held on the previous frame (when you let go of the button) if !c.zl and old_c.zl playerSprite.aim = false setSpriteVisibility(gunSprite, false) endif // after you've done everything you want, before you draw things, store the old state of the controls: old_c = c
This way, you only set the visibility and aim state to the correct values once, when the button is pressed or let go of.
There will be much more in terms of the shooting and processing bullets, but this is a core concept which will help!
-
You guys are the best thanks for all the help