# Charged attacks: Problem with "every X seconds"

Get help using Construct 2

### » Mon Oct 28, 2013 9:48 pm

Hi, I've been using Construct for a while, but this is my first time posting on the forums. I guess you'll be seeing more of me in the future as I'll surely get stuck again at some point, but I hope to eventually get good enough to help others.

Ok, this is the situation:
I've been working on a game, I have a character (the player) always carrying a melee weapon (separate sprites) pointing upwards (0 degrees).

When pressing "c" on the keyboard the weapon is tilted back -30 degrees and then it rotates 210 degrees (180 + the 30 it tilted in the begin).

If the c key is hold for a certain amount of time and then released, the weapon will rotate 390 degrees (360 + the 30 it tilted in the begin)and will be a little faster. Something similar to the "spin attack" in Zelda games.

The longer you hold c the stronger the attack will be, there are 3 levels of charge (these levels are not relevant for the current problem).

To do all this, the player's sprite has 2 variables:
1) charge_lvl:It goes from 0 to 3, the default value is 0 representing a regular 210 degrees attack, from 1 to 3 is a charged 390 degree attack. For the issue at hand there is no difference between 1 to 3 values, it only matters if it is 0 or greater than 0.
2) lock: It goes from 0 to 1. Default is 0. When the attack "animation" starts I set it to 1, when it ends I set it back to 0. An attack can be performed only when the value is 0, this is to prevent the player to interrupt the attack's animation by pressing c repeatedly.

I do the attack's "animation" by rotating the weapon 30 degrees, waiting a fraction of a second(0.03 or 0.02), rotating another 30 degrees and so on until the 210 or 390 degrees are completed. At the end of each attack there is a little extra waiting time (0.5 seconds).

There are 3 events:
1) c is pressed: This tilts the weapon -30 degrees.
2) c is down: every X seconds the value of charge_lvl is increased.
2) c is released: this is divided in 2 sub events: When charge_lvl is 0 and when it is greater than 0. In both cases the attack's "animation" is activated, the only difference is that when charge_lvl is greater than 0 it has additional rotation steps in order to complete the total 390 degree rotation, also, each step in the "animation" is faster (the waits are shorter).

Ok, that is the way I'm working, now, the actual problem:
Sometimes when pressing c without holding it does the charged attack(390 degrees) instead of the regular attack (210 degrees). This happens occasionally, try it a few times to see.

I don't really know where is my error, but I have the feeling it has something to do with the "wait X seconds" condition.

In my actual game there are enemies, sound effects, visual effects etc, to make sure none of these things had anything to do with my problem I recreated the basics elements associated with the problem into another very basic project. The results were the same so I guess there is a problem with my logic or my understanding of some event.
(Also, I've tried with Firefox, Chrome and IE, same results)

So, I'm asking for help to find out a solution to the problem.

And these are some images of the project.

Notes:
* The number on screen is the current value of the charge_lvl variable I added it for debugging purposes.
* The wait every X seconds sentence is set to high value (9 seconds) in order to make sure that said amount of time is NOT passing end therefore it shouldn't be activating the charged attack.

B
28
S
3
G
1
Posts: 29
Reputation: 1,698

### » Mon Oct 28, 2013 10:49 pm

Move to sub event and problem solved :

B
48
S
19
G
87
Posts: 2,317
Reputation: 51,518

### » Mon Oct 28, 2013 11:11 pm

use the timer behavior instead of every 9 seconds
start a regular timer when you set the angle -30
then on the timer you add to the charge as long as charge <3 and c is down

Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

### » Mon Oct 28, 2013 11:44 pm

@mindfaQ
Yep. Because he's already made logic to reset after melee has completed and on release of C, putting it into the sub event works. However it will instantly increase to 1 so he will need to adjust melee to charge_lvl > 1 probs.
B
48
S
19
G
87
Posts: 2,317
Reputation: 51,518

### » Tue Oct 29, 2013 12:54 am

Thank you very much for the answers

I moved the "every X seconds" to a sub event and tested it. The results were the same, usually it worked well, but every now and then it would activate a charged attack instead of a regular attack.

Then, I used a timer, that did the trick.

This was the resulting code:

Once again, thank you ^_^
B
28
S
3
G
1
Posts: 29
Reputation: 1,698

### » Tue Oct 29, 2013 12:03 pm

@plinkie: nope, because "every x seconds" uses the global time. every x seconds will trigger on (multiples of x) seconds game time (it doesn't start counting from 0 when you press c so to say)
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

### » Tue Oct 29, 2013 12:05 pm

it does, because the logic set up resets charge_lvl to 0 when C is released.
B
48
S
19
G
87
Posts: 2,317
Reputation: 51,518