How do I set up a timed power-up?

Get help using Construct 2

Post » Tue Dec 30, 2014 11:17 pm

Hello,
I recently got into Construct 2 and am enjoying it so far. I've gone through the shooter tutorial and am now continuing with the tutorial code, adding in various things to learn more about Construct 2.

One of the things I've added is a gun power-up which will enable the player to shoot at full auto for 5 seconds. Currently, I am generating it a specific location every 6 seconds (I'll randomize the location later...makes it easier to debug at the moment). The power-up sprite will only generate if there is not one already on the map. I've gotten the full auto feature to work as well when the player collides with the power-up sprite.

However, I cannot figure out how to limit the full-auto feature to work for only 5 seconds. I've tried a few different things, but, the primary bug is that, while it works great the first time, when the full-auto ends and the player attempts to immediately pick up another full-auto power-up, it will not trigger...I have the code attached. The power-up events are all at the bottom of Event Sheet 1.
You do not have the required permissions to view the files attached to this post.
B
3
Posts: 4
Reputation: 183

Post » Tue Dec 30, 2014 11:36 pm

Here's one option. Could also have the timer set/reset your variable if you prefer.
You do not have the required permissions to view the files attached to this post.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Wed Dec 31, 2014 5:56 am

Wow, cool, thanks, it works perfectly. So that I can understand the coding better, what was the issue with the previous code? I know it had something to do with the Wait action, but I am not sure what. Thanks again!
B
3
Posts: 4
Reputation: 183

Post » Wed Dec 31, 2014 6:55 am

You're setting hasPowerup to "false" every 5 seconds, possibly even after they just got the powerup. I don't like global Wait actions for this kind of thing. Some people get good mileage with System: Every x seconds for cooldown periods like this (which it kind of is). I just prefer setting a Timer as it keeps the behaviour with the object. There's also a 3rd party Cooldown add-on which I've used.

edit: this one is better. There was no need for the complicated expression checking the timer values.
testgame.capx


edit 2: BTW I wouldn't use "true" and "false", it's very error prone. Use 0 and 1 or Boolean variables.
You do not have the required permissions to view the files attached to this post.
Last edited by codah on Wed Dec 31, 2014 8:51 am, edited 1 time in total.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Wed Dec 31, 2014 7:23 am

Ah, thank you for your help!!! The timer is quite useful. :)
B
3
Posts: 4
Reputation: 183

Post » Tue Jan 03, 2017 11:24 pm

Thank you, @codah.
I gave a try to using Timer behavior instead of Wait function and it works like a charm. Your .capx example is also very useful.

I was aware of the theory behind Timer behaviors, but I didn't really give it a try.
I thought using Wait functions was lot easier, and Timer behavior functions lot more complex.
But well, I was wrong. Now I can say you definetely got a more stable code by using Timer functions instead of Wait ones. At least in that kind of situations where you want to play around with variables value for just a few seconds before any other events happen.

I also figured out that using System variables instead of object variables (on the player character) fixed a few issues on my code.

Again, thanks a lot.

Kr,
Antoine
Image
Version 1.1.2 - Available for: Web - iOS - Android
B
16
S
3
Posts: 50
Reputation: 1,046


Return to How do I....?

Who is online

Users browsing this forum: AllanR, BackendFreak, brunopalermo, farsen, ST314 and 32 guests