Confused

i cannot figure out why the following bit of code will not work. Maybe someone here can shed some light:

currentBtn_mc.onRollOver = function() {
if (this._alpha<100) {
this._alpha += 10;
}
};
currentBtn_mc.onRollOut = function() {
if (this._alpha>0) {
this._alpha -= 10;
}
};
currentBtn_mc.onRelease = function() {
activebtn_mc._alpha = 0;
this._alpha = 100;
activebtn_mc = this;
};

This code is to control buttons as part of a slide show. The onRelease function works like a charm. The onRollOver and onRollOut do not. What i am not understanding is why i can control the _alpha with onRelease, but not with onRollOver or onRollOut. Am i not seeing something obvious? Or do i need something more complex?

Where are you actually changing the _alpha value of currentBtn_mc? Your onRollover function increases the _alpha value if it’s less than 100, but the default value will already be 100 so you won’t actually see any changes the first time you roll over the mc.

The onRollOut function decreases the _alpha value if it’s greater than 0 so it will drop to 90 or thereabouts. It’s probable that you can’t visually differentiate between a value of 90 and a value of 100.
When you next roll over the button, the _alpha value increases to 100. Again, you’d find it difficult to tell the difference between 100 and the previous value of 90.

If you want currentBtn_mc to fade out on a roll out, and fade in on a roll over, then you need to combine it with an onEnterFrame event. I’m not entirely sure what you’re trying to achieve but I guess you want currentBtn_mc to fade in when rolled over, fade out when rolled out, and set the _alpha permanently to 100 when the mouse button is released? If so, something like this should do the trick:


// set initial variables:
// amount to fade in or out by
fadeAmount = 10;
// set default _alpha of currentBtn_mc
defaultAlpha = 10;
currentBtn_mc._alpha = defaultAlpha;
// function to fade the movieclip in
function fadeIn() {
	// increase by the fade amount
	this._alpha += fadeAmount;
	// check if _alpha reaches 100
	if (this._alpha >= 100) {
		// if so, set _alpha to 100
		this._alpha = 100;
		// and delete the onEnterFrame
		this.onEnterFrame = null;
	}
}
function fadeOut() {
	// decrease by the fade amount
	this._alpha -= fadeAmount;
	// check if _alpha reaches default value  
	if (this._alpha <= defaultAlpha) {
		// if so, set _alpha to default value
		this._alpha = defaultAlpha;
		// and delete the onEnterFrame event
		this.onEnterFrame = null;
	}
}
currentBtn_mc.onRollOver = function() {
	// on roll over, associate fadeIn function with an onEnterFrame event 
	this.onEnterFrame = fadeIn;
};
currentBtn_mc.onRollOut = function() {
	// on roll out, associate fadeOut function with an onEnterFrame event
	this.onEnterFrame = fadeOut;
};
currentBtn_mc.onRelease = function() {
	// on release, set default _alpha value to 100
	defaultAlpha = 100;
	// although it's not necessary, 
	// delete the fadeIn and fadeOut functions
	// to prevent them from triggering
	fadeOut = null;
	fadeIn = null;
};

thank you for the reply. This clears things up for me. I need the onEnterFrame to loop through the alpha change, correct?

Just as a note. I set the alpha to 0 for currentBtn_mc so that when it loads it is not visible. There is an outline around the button to designate where it is. An example from the website i am working on can be found at:

http://www.mzarchitect.com/mz_projects/0313/salvation.html

I will let you know how i get on with the onEnterFrame once i get a chance to implement it.

Also, i have looked, but i cannot seem to figure out how to add code in a seperate scrollable window rather than just copy pasting it into the main body of my post.

thanks

Yes, the onEnterFrame event will repeat your code at the same frame rate as your movie.

You can alter the code I’ve supplied to set the initial _alpha value to 0, e.g. defaultAlpha = 0;

To highlight actionscript code in the forum, enclose it within **[noparse]

 

[/noparse]** tags