# 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
131
S
45
G
41
Posts: 860
Reputation: 28,641

### » 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
311
S
128
G
101
Posts: 7,413
Reputation: 75,222

### » 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
71
S
22
G
14
Posts: 1,494
Reputation: 16,660

Return to How do I....?

### Who is online

Users browsing this forum: No registered users and 5 guests