Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic image alpha from loop 1

Status
Not open for further replies.

frozenpeas

Technical User
Sep 13, 2001
893
CA
Hi,

I have tried a few methods but I'll lead the post with this one. The value for j is tracing properly (100 through 0) but I cannot apply the value to picHolder's alpha property.

If I set picHolder._alpha to a static value, it sets the alpha. But it refuses to take the j value.

Thanks.

Code:
for(var j:Number=100;j>0;j--){
	picHolder._alpha = j;
	trace(j);
}

frozenpeas
--
Micfo.com Affiliate Program
 
Hmm. As far as the traces are showing, the fade is working:

Code:
j = 21
_alpha = 20.703125
j = 20
_alpha = 19.921875

But the loaded photo does not fade - it just appears as if I am toggling the visibility. Any ideas?

frozenpeas
--
Micfo.com Affiliate Program
 
There's nothing wrong with your code and Flash is doing what you told it to do - but you won't be able to see it.

I have just measured the time it takes to execute your code on my dual 2GHz Mac G5. It took 1 millisecond. I think it actually takes less than 1 millisecond but Flash cannot measure the time shorter than 1 millisecond. In any case, I don't think you'll see the fading effect happens in 1 millisecond (unless you're a ninja!)

The common way is to use onEnterFrame or interval, which you can make it as slow as you want.

Kenneth Kawamoto
 
Thanks, KK.

onEnterFrame was another method I tried but got the same result. One attempt looked like this:

Code:
onEnterFrame = function(){
	if(fadeDirection=="out"){
		if(alphaLevel>0){
			alphaLevel++;
		}
		picLoaded = true;
	}
	if (fadeDirection=="in"){
		if(alphaLevel<100){
			alphaLevel--;
		}
	}
	picHolder._alpha = alphaLevel;
	updateAfterEvent();
}

frozenpeas
--
Micfo.com Affiliate Program
 
This works:

[tt]//
var alphaLevel:Number = 100;
var fadeDirection:String = "out";
onEnterFrame = function () {
if (fadeDirection == "out") {
if (alphaLevel>0) {
alphaLevel--;
}
picLoaded = true;
}
if (fadeDirection == "in") {
if (alphaLevel<100) {
alphaLevel++;
}
}
picHolder._alpha = alphaLevel;
};
stop();
//[/tt]

I just swapped alphaLevel++ and alphaLevel--, also added the initialization of two variables at the top (I needed to do that as I don't know how you're setting those vars.)

Kenneth Kawamoto
 
how about this long winded code

fadeIn = true
picHolder._alpha = 0

this.onEnterFrame = function(){
if(picHolder._alpha<99 && fadeIn){
picHolder._alpha ++
fadeIn = true
else{
picHolder._alpha --
}
if(picHolder._alpha>99){
fadeIn = false
}
if(picHolder._alpha<1){
fadeIn = true
}
}

surely ought to be possible to cut this code in half
 
Still no go.

Here is all of my code at this point:
Code:
//initialise
var picArray:Array = new Array('01.jpg','02.jpg','03.jpg','04.jpg');
var originX:Number = 75;
var originY:Number = 350;
var offsetX:Number = 20;
var picLoaded:Boolean = false;
var alphaLevel:Number = 0;
var fadeDirection:String = "neutral";
for(var i:Number=0;i<picArray.length;i++){
	//create button on stage
	attachMovie("btn","btn"+i,i);
	this["btn"+i]._x = originX;
	this["btn"+i]._y = originY;
	this["btn"+i].txtNum = i+1;
	originX += offsetX;
	//on button press, load external image
	this["btn"+i].onRelease = function(){
		loadMovie(picArray[this.txtNum-1],"picHolder");
		if(picLoaded==true){
			hideImage();
		}else{
			revealImage();
		}
	}
}
//fade image out
var hideImage:Function = function(){
	var fadeDirection:String = "out";
}
//fade image in
var revealImage:Function = function(){
	var fadeDirection:String = "in";
}
//fade action
onEnterFrame = function(){
	if((alphaLevel<100)&&(fadeDirection=="in")){
		alphaLevel++;
	}
	if((alphaLevel>0)&&(fadeDirection=="out")){
		alphaLevel--;
	}
	if(alphaLevel==100){
		picLoaded = true;
	}
	if((alphaLevel==0)&&(picLoaded==true)){
		revealImage();
	}
	picHolder._alpha = alphaLevel;
	updateAfterEvent();
}
At the moment, no images are visible until alphaLevel is initialised with a value greater than 0.

Thanks again.

frozenpeas
--
Micfo.com Affiliate Program
 
May be it's a good idea to code it step by step. I modified your code to do the reveal function.

[tt]//initialise
var picArray:Array = new Array('01.jpg', '02.jpg', '03.jpg', '04.jpg');
var originX:Number = 75;
var originY:Number = 350;
var offsetX:Number = 20;
var fadeDirection:String;
for (var i:Number = 0; i<picArray.length; i++) {
//create button on stage
attachMovie("btn", "btn"+i, i);
this["btn"+i]._x = originX;
this["btn"+i]._y = originY;
this["btn"+i].txtNum = i+1;
originX += offsetX;
//on button press, load external image
this["btn"+i].onRelease = function() {
loadMovie(picArray[this.txtNum-1], "picHolder");
picHolder._alpha = 0;
revealImage();
};
}
//fade image in
var revealImage:Function = function () {
fadeDirection = "in";
};
//fade action
onEnterFrame = function () {
if (fadeDirection == "in") {
if (picHolder._alpha<100) {
picHolder._alpha++;
} else {
fadeDirection = "out";
}
}
};
[/tt]

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top