How do I fire weapons with multipul barrels

Get help using Construct 2

Post » Mon Jul 18, 2016 6:14 pm

Hi everyone,

So I am looking for a bit of advice on firing weapons with more than one barrel - specifically spawning projectiles and animating the weapons.

For example, I have a weapon with two barrels. I would like to alternate spawning the projectile at each barrel and to know which barrel is firing so I can animate it.

My current projectile system attempts to emulate swapping barrels but it is visual only and I cant control the order in which the barrels fire - therefore I can't animate them accurately.

http://i.imgur.com/dq0lMWK.png

Any advice is greatly appreciated.
B
13
S
6
Posts: 130
Reputation: 1,553

Post » Mon Jul 18, 2016 6:21 pm

I guess you could go the other way around - create an animation of the ship shooting with alternating barrels, then check the current frame. If frame is 5 (for example) spawn a bullet from the left barrel. If it's on frame 11 - spawn from the right barrel.
B
18
S
7
G
1
Posts: 160
Reputation: 1,934

Post » Mon Jul 18, 2016 7:14 pm

https://www.dropbox.com/s/hz5ai8rr2cl5o ... .capx?dl=0

Use image points and toggle states, or an incrementing counter.
Mistakes were made.
B
54
S
28
G
115
Posts: 1,705
Reputation: 65,083

Post » Mon Jul 18, 2016 7:56 pm

Thanks for the input guys!

oosyrag wrote:https://www.dropbox.com/s/hz5ai8rr2cl5oa0/alternate.capx?dl=0


Wow that is an incredibly elegant solution to my example, thank you very much oosyrag. Sorry to ask more of you, but I've clearly not had enough coffee today - how would you expand that system to include more barrels, 3 or 4 or 5 say.

trueicecold wrote:I guess you could go the other way around - create an animation of the ship shooting with alternating barrels, then check the current frame. If frame is 5 (for example) spawn a bullet from the left barrel. If it's on frame 11 - spawn from the right barrel.


Thats a good suggestion too, thank you :) but I'm always hesitant to trigger things on frame because I've found it to be notoriously unreliable for some reason!
B
13
S
6
Posts: 130
Reputation: 1,553

Post » Mon Jul 18, 2016 8:15 pm

TMAJA wrote:Wow that is an incredibly elegant solution to my example, thank you very much oosyrag. Sorry to ask more of you, but I've clearly not had enough coffee today - how would you expand that system to include more barrels, 3 or 4 or 5 say.


Use an incrementing variable. So after each shot, add one to that variable, and use that to chose imagepoint, animations, ect... After it reaches the number of turrets, reset it back to 1. Remember imagepoint 0 is reserved for the origin). Or, you can do variable+1 when spawning from imagepoints.

A fancy way of doing this is with a conditional operator:
Assuming you have one imagepoint per barrel, and are not using imagepoints for anything else on that object...
Upon firing, set ActiveBarrel to "ActiveBarrel<Object.Imagepointcount?Object.ActiveBarrel+1:1"
This says "If the ActiveBarrel variable is less than the total number of imagepoints, then add one to ActiveBarrel. Otherwise, set ActiveBarrel to 1."

This will cover any number of imagepoints you decide to add, and cycle through them one at a time.
Mistakes were made.
B
54
S
28
G
115
Posts: 1,705
Reputation: 65,083

Post » Tue Jul 19, 2016 12:02 pm

oosyrag wrote:
TMAJA wrote:Wow that is an incredibly elegant solution to my example, thank you very much oosyrag. Sorry to ask more of you, but I've clearly not had enough coffee today - how would you expand that system to include more barrels, 3 or 4 or 5 say.


Use an incrementing variable. So after each shot, add one to that variable, and use that to chose imagepoint, animations, ect... After it reaches the number of turrets, reset it back to 1. Remember imagepoint 0 is reserved for the origin). Or, you can do variable+1 when spawning from imagepoints.

A fancy way of doing this is with a conditional operator:
Assuming you have one imagepoint per barrel, and are not using imagepoints for anything else on that object...
Upon firing, set ActiveBarrel to "ActiveBarrel<Object.Imagepointcount?Object.ActiveBarrel+1:1"
This says "If the ActiveBarrel variable is less than the total number of imagepoints, then add one to ActiveBarrel. Otherwise, set ActiveBarrel to 1."

This will cover any number of imagepoints you decide to add, and cycle through them one at a time.


Hi Oosyrag,

Wow what an excellent solution - before I saw your reply I had tried an incrementing variable system and while it worked it was no where near as simple and clear as your conditional operator suggestion. Thank you for not only providing a superb solution - but also taking the time to explain what the condition operator was doing, I appreciate it very much!
B
13
S
6
Posts: 130
Reputation: 1,553


Return to How do I....?

Who is online

Users browsing this forum: Felipesz and 38 guests