Movieclip as button hitTest working intermitent, need advice

I was playing around last night trying to learn simple AS to make a movie clip act like a button. I wanted the button movie clip to reduce the alpha value of the other movie clip when you rollover the button and when you roll off the button the alpha of the movie clip goes back up. It works, but if you keep rolling over and out of the button movieclip sometimes the alpha gets stuck at a certain value for a bit, then does what its supposed to. Any ideas why? I’m using onEnterFrame () to do the hitTest constantly.


Here’s the SWF and FLA files for you to look through:

hitTest SWF
hitTest FLA

Here’s the AS code I used in the frame to do this:

onEnterFrame = function() {
	if ( background._alpha > 100 ) {
		background._alpha = 100;
	if ( background._alpha < 0 ) {
		background._alpha = 0;
	if (hitTest( _root._xmouse, _root._ymouse, true)) {
	background._alpha -=25;
	else if (hitTest( _root._xmouse, _root._ymouse, false)) {
	background._alpha +=25;

Yours didn`t stick for me but
why not use onRollOver/RollOut
give the mc an instance name mc and something like this
function fadein() {
background.onEnterFrame = function() {
if (this._alpha<=100) {
this._alpha += 25;
} else {
this._alpha = 100;
delete onEnterFrame;
function fadeOut() {
background.onEnterFrame = function() {
if (this._alpha>=0) {
this._alpha -= 25;
} else {
this._alpha = 0;
delete onEnterFrame;
mc.onRollOver = fadein;
mc.onRollOut = fadeout;

25 is quite a big step so i think it will look a bit rough.

onrollover and onrollout didn’t seem to be working as well as the hittest. and yeah, i changed the 25 to 10 so it was smoother…but this was really just me experimenting trying to learn ASing.

Your code didn’t work for me. I did a check and it said there were 4 errors in the code.

Im afraid im a careless mx user.
if you are using mx2004, you`ll have to change mc.onRollOut = fadeout;to fadeOut but there are no errors. Are you putting the code on the main timeline?

Ah, yeah I’m using 2004. Is fadeOut a built-in command? I did a search and it didn’t find it. I played around with your code enough to get it to change the alpha when I rolled over or out, but I wanted it to continuosly reduce or increase the alpha value, as far as I can tell it only reduces or increases ONCE, when you either roll over or out. I was trying to get the effect that when you rolled over the button the movieclip would fade out and if you rolled off it, it would come back into view. thats why i used onenterframe. also in my code, i added a trace command so i could see the alpha value and its going above 100 and below 0…how do I stop that from happening? (if i have the value change by 25, it will go to -25 and 125, if i change that to 10 it goes to -10 and 110). i thought my first 2 if statements would prevent it from going past 0 and 100.

ps. actually I tried it in the alpha_mc and in the main timeline, neither working…but part of that could be the fadeout thing.

fadein and fadeOut are custom made and you can call them anything you like.

You are right to use onEnterFrame, but mine does too.

You need to use delete onEnterFrame to stop the _alpha values changing but (and i`ve not tested this) i think it would stop all your code from working, so you would have to restructure it somehow.

The only reasons which i can think of why my code didnt work is that you have 1/not put the code on the timeline containing both mcs 2/not given them instance names.

@LittleFenris, the true and false in the hitTest don’t refer to if the hittest is true or false, but [size=1](from the AS-reference)[/size]:

shapeFlag A Boolean value specifying whether to evaluate the entire shape of the specified instance (true), or just the bounding box (false).


…so if you want to know if hitTest is false, you do like this…

if (!hitTest(…)) { …

…and that’s true…:lol:


hehe :beam:

First, thanks for the fla file! Works perfect. This was all just to experiment to figure out syntax and some simple commands and whatnot. I guess the delete onEnterFrame in your code is to stop it from changing the alpha value below 0 and above 100? I’ll have to play with if else statements…I kept getting errors trying those that the else didn’t have the if to go with it, even though it was there. I’m sure it was how I was structuring my code but I couldn’t figure it out. Thanks for all your help. I’m picking up this ASing slowly but surely. I’m an artist not a programmer, so its a bit different thinking for me.

YOu should always remove onEnterFrame when not needed, just a processor thing-eventually your movies will run really slowly.
The code i gave you was actually wrong and it should have read
delete this.onEnterFrame;in both functions. As it stands it is not doing anything.

Thanks for the info. I’ll add the this. into both…although as it is your code works perfect for what I was trying to accomplish.