Fading an object onto the screen?


(Nate ) #1

Hello guys! I would like to add items to my game every X amount of time to my game. I want the items to fade in, rather than just popping up.

Any ideas how I could do this without sucking up a lot of resources?

So for example, I have a rock entity. I am looking to have it fade in upon spawn.

Thank you

This is how I am adding some rocks randomly:

var r:int = 0;
		for (var i:int = 0; i < rockCount; i ++)
		{
			r = FP.rand(512);
			FP.world.add(new theRock(r, 0));
		}

(Nate ) #2

I think I have something working. Not sure if it is correct.

But this is what I did in the rock class in the update method:

(graphic as Image).alpha = (graphic as Image).alpha + .01;
		
		if ((graphic as Image).alpha == 1)
		{
			(graphic as Image).alpha = 1;
		}

(graphic as Image).alpha starts as 0.4


(Ultima2876) #3

That’s a perfectly fine way of doing it. You may also want to consider incorporating FP.elapsed if you’re using a variable timestep (can’t hurt to do it with a fixed timestep too);

(graphic as Image).alpha = (graphic as Image).alpha + .01 * 60 * FP.elapsed;

Another option is to use a NumTween.


(azrafe7) #4

…Or using FP.tween():

if (justSpawned) {
    var image:Image = Image(graphic);
    FP.tween(image, {alpha:1.0}, durationInSecs);
    justSpawned = false;
}

(Nate ) #5

Thanks for the replies guys!

Which method do you think will be less work for the program?


(Ultima2876) #6

I wouldn’t worry. That kind of micro-optimisation won’t give you any benefit and will just waste time better spent on other things, unless you have thousands and thousands of entities doing it.


(Nate ) #7

Nope just 1-8 every 15 seconds depending on how many are on the screen already! :smiley: Thanks Ultima