[SOLVED] How do I play sound at limits of sine cycle?

Get help using Construct 2

Post » Fri Jun 23, 2017 9:41 pm

This is probably an easy fix, but I'm tired and my brain hurts:

I'm trying to play a sound whenever any instance of an object reaches either the maximum or minimum of its sine behaviour.

Currently I'm saving the cycle position to an instance variable "cycle" each tick and then testing every tick if cycle is less than 0.02; any less than 0.02 and the sample rarely plays, any more and it will trigger over successive frames, and even at this value it is temperamental, sometimes missing a play other times doubling up. I understand why that happens, but can't see a way around it.

I also tried using "if object.cycle<0.02 trigger once | play sound" but this resulted in the sound playing once for each instance at the start (because they all start their cycles at 0) and then not playing again. I also swapped sine.cyclePosition for sine.Value but the problem is still the same.

Thanks in advance for any help :)
Last edited by mekonbekon on Sat Jun 24, 2017 11:04 am, edited 1 time in total.
B
20
S
9
G
4
Posts: 473
Reputation: 4,260

Post » Fri Jun 23, 2017 11:18 pm

The Sine extremes actually seem to occur at 0.25 and 0.75, so switch to two conditions:

CyclePosition>0.25 & Trigger once -> sound
Else CyclePosition>0.75 & Trigger once -> sound

http://www.blackhornettechnologies.com/Construct2Stuff/mekonbekon_SineExtremes.capx
ImageImageImage
B
71
S
22
G
248
Posts: 3,756
Reputation: 138,058

Post » Sat Jun 24, 2017 10:12 am

@blackhornet Thanks for the example, I hadn't considered that the extremes were at 0.25 and 0.75.

This works great for a single instance, but how would you handle it with multiple instances at different cycle offsets? I can't get "trigger once" to repeat on objects, and using "compare two values" means that it fires for all instances every time one of them triggers the condition. If I had a fixed number of instances in the level then I could tie each of their cycle positions to a unique local variable, but if I'm spawning on the fly then using a local variable wouldn't work.
B
20
S
9
G
4
Posts: 473
Reputation: 4,260

Post » Sat Jun 24, 2017 11:04 am

Managed to get it working for multiple instances with an extra instance variable:

Image

Cheers again for your help @blackhornet - you pointed me in the right direction.
B
20
S
9
G
4
Posts: 473
Reputation: 4,260


Return to How do I....?

Who is online

Users browsing this forum: Christmas and 56 guests