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
115
S
39
G
40
Posts: 828
Reputation: 27,383

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
263
S
93
G
56
Posts: 7,181
Reputation: 67,656

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
62
S
22
G
14
Posts: 1,481
Reputation: 16,405


Return to How do I....?

Who is online

Users browsing this forum: Eren, frilous, kccarmea, mrtumbles and 6 guests