# Gradually change a number in a variable

Get help using Construct 2

### » 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
106
S
35
G
37
Posts: 798
Reputation: 25,215

### » 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

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 7,000
Reputation: 57,795

### » 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

### » Wed Feb 01, 2012 9:56 am

B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Return to How do I....?

### Who is online

Users browsing this forum: flemmig, Kagubro, MisterAqua and 23 guests