Opacity help

What Wick Editor Version are you using?
1.9

Describe the Problem
I dont know how to make a object start at 0 opacity, then go to 100% when ‘f’ is pressed. It works for the first time but after that it starts out at 100% opacity.

What have you tried so far?
I set in the inspector for my object to be at 0% opacity. Then wrote this in the update script on the object.
if (key === "f" ) { this.opacity === 1; }

1 Like

when assigning opacity, use 1 equal sign, not 3. === is for checking if something is the same as something else. = is for assigning values.

So, i think it would be:

if (isKeyJustPressed(“f”)) {
this.opacity = 1;
}
(PD: I mostly use isKeyJustPressed because key registers the last key pressed, but isKeyJustPressed checks if the key is, well, just pressed.)

Do you want it to fade? If so, you can do this:

default script:

this.targetOpacity = 0

update script:

var opacityChangeRate = 2 / project.framerate; //should go to 100% opacity in 2 seconds.

if (this.opacity !== this.targetOpacity) {
	var opacityChangeDir = Math.sign(this.targetOpacity - this.opacity);
	this.opacity += opacityChangeDir * opacityChangeRate

	//make sure it doesn't go past target opacity
	if (
		(opacityChangeDir > 0 && this.opacity > this.targetOpacity) || //increasing
		(opacityChangeDir < 0 && this.opacity < this.targetOpacity)    //decreasing
	) {
		this.opacity = this.targetOpacity;
	}
}

keydown script:

if (key === "f") this.targetOpacity = 1;
//if your if statement includes only one command, you can omit the curly braces

Or you can use the setTimeout function to do this

keydown script:

var opacityChangeRate = 2 / project.framerate; //should go to 100% opacity in 2 seconds
var timeoutLength = 1000 / project.framerate; //framerate in milliseconds (60 fps turns into one frame per 16.6666666 milliseconds)

if (key === "f") {
	var f = function() { //create a local function for recursion with setTimeout
		this.opacity += opacityChangeRate;

		if (this.opacity >= 1) this.opacity = 1;
		else setTimeout(f, timeoutLength);
	}

	f();
}

I didn’t actually test these myself but hopefully they should work.

1 Like

Wouldn’t this be a lot easier?

if (isKeyJustPressed(“f”)) {
onEvent(‘update’, function () {
if (this.opacity !== 0) {
this.opacity -= project.number;
}
});
}
(Also, project.number is 0.1 (in default put project.number = 0.1))

no you need to make it stop when it reaches the target opacity, and you can’t stop an onEvent

Uhhh…

Doesn’t that stop it when it reaches 0?

oh i thought that was for something different when i read the code sorry about that

but not really it might pass opacity in a situation like this:

0.45 !== 0
0.35 !== 0
0.25 !== 0
0.15 !== 0
0.05 !== 0
-0.05 !== 0
-0.15 !== 0
-0.25 !== 0
e.t.c.

you want to make sure it stops when it passes 0. so you should use a less than

also you can’t stop an onEvent and you’d probably want to stop running that function when it’s done

But it’s still running even if it’s an if?
(Also, opacity can’t pass from 0)

umm… yeah? it will still keep running the function even if it doesn’t execute any instructions. on every update it runs that function.

and yes opacity can pass through zero, if you set it to a non-multiple of project.number. there are infinite (useful) non-multiples of 0.1, while there are only 11 (useful) multiples of 0.1.

But i put a text that sees this.opacity and when it reaches 0 it doesn’t go down…

If you press “f,” then the object will decrease by the project number forever. Therefore, if you press “f” again, it will decrease by the project number for another forever. Also, if the opacity isn’t less than zero, the opacity will go down… but then the opacity won’t be able to go up since it wouldn’t be equal to zero?

What I mean is that the opacity will always be equal to zero. It will keep decreasing

oh wait do you mean that internally it is programmed so opacity has to stay between 0 and 1?

Tried to press f multiple times, it’s still in zero, i’ll share the project.
opacity test12-30-2020_9-27-28PM.wick (1.8 KB)

I think so…
I don’t really know, i’m just saying what i think.

ok. but the problem i have with your solution is that it will still run the function even though it doesn’t do anything anymore. this won’t result in a huge perfomance loss unless you press f a whole lotta times but still…

tried this one and i did not work

Because it’s in 1, change 1 to 0.

(Put 1 because you put 1 in the text, my mistake-)

image
it is still giving me a bunch or red nonsense.

It’s because you aren’t using " i think.
When you use " " it lights the text in orange.
(LOL I JUST NOTICED IT DOES CHANGE IT WHEN TOGETHER!)

1 Like