Gradually change a number in a variable

Get help using Construct 2

Post » Wed Feb 01, 2012 4:24 am

Let's say I have an object that I want to be transparent, then gradually becomes opaque. Usually I create a variable within the object named "transparency", and

if "transparency" = 0
then every "tick" obj.transparency= obj.transparency + 5

and if "transparency" = 1, it does the opposite.

Is there any way of handling this better? I'm not that good with programming, and I feel this isn't the right way of doing it.. I also feel like it should go fast like that. I wanted it to go fast at the beggining, then when it gets close to being completely opaque, it slows down the speed it's doing it.. is there a formula for that?

Sorry, it's kinda hard to explain since english isn't my first language.
B
121
S
41
G
40
Posts: 841
Reputation: 27,859

Post » Wed Feb 01, 2012 5:14 am

I guess you could use the system expression "lerp" (explanation topic of lerp)
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
276
S
103
G
85
Posts: 7,292
Reputation: 83,606

Post » Wed Feb 01, 2012 5:22 am

The lerp function can help. To fade out you could do:
Every tick: set opacity = lerp(self.opacity, 0, 0.5*dT)
to fade in:
Every tick: set opacity = lerp(self.opacity, 100, 0.5*dT)

To alternate between the two, use a sine function:
Every tick: set opacity = 100* sin(time*50)

http://www.scirra.com/forum/lerp-clarification_topic48641.html

B
12
S
3
G
3
Posts: 73
Reputation: 2,647

Post » Wed Feb 01, 2012 9:56 am

B
64
S
22
G
14
Posts: 1,482
Reputation: 16,457


Return to How do I....?

Who is online

Users browsing this forum: Ami Samad, Yahoo [Bot] and 4 guests