Let The Bullets Fly


The Bullet behavior: Right Click> Add> Behavior> MOVEMENTS> Bullet
Gives almost any object movement with a specific angle and speed. Its great for moving things in a general direction that don't necessarily need a specific stopping point. So yeah a bullet, rain, snow, leaves, ETC. One feature of the behavior is that the angle of motion can use the objects angle, or you can set the angle of the behavior itself, which is usually what you need for objects that you don't want changing display angles. For that you would untick "Set angle" over in OBJECT  TYPE  PROPERTIES.
The events for the project are as follows:

+ System: On start of layout
-> crosshairsprite: Destroy
    | Local number ang‎ = 0
    // Create 25 sprites
----+ System: For "s" from 0 to 24
-----> System: Set ang to random(360)
-----> System: Create object crosshairsprite on layer 0 at (random(LayoutWidth), random(LayoutHeight)), create hierarchy: False, template: ""
-----> crosshairsprite: Set animation frame to int(random(crosshairsprite.AnimationFrameCount))
-----> crosshairsprite: Set Bullet angle of motion to ang degrees
-----> crosshairsprite: Set Bullet speed to 10+random(-5,10)

// keep the sprites on the layout
+ crosshairsprite: Is outside layout
-> crosshairsprite: Set Bullet angle of motion to angle(crosshairsprite.X,crosshairsprite.Y,LayoutWidth÷2,LayoutHeight÷2) degrees

So we have quite a bit going on at the start. First destroy the sprite that's on the layout. Its not that big of a deal here, but unless you do an layout object repository(I'll cover that in yet another tut), you may want to let an object stay in the layout to edit its preferences, since the default setting don't include custom things like opacity, angle ETC.
With that out of the way... We create 25 more of them, at random positions, and change the frame to a random cross hair sprite(thanks Kenney.nl).
Then set random bullet angles thanks to randomizing the Local Variable each step of the loop.
Ok maybe not so much stuff when you nibble it down like that.😬

Then even though we don't have to worry about where they go, we do want to keep them on the screen.
To deal with that we have the crosshairs check that if they are outside of the layout we point them back at the middle of the layout.

The source file is located: Here

Comments