# Movieclip as button hitTest working intermitent, need advice

**URL:** <https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015>\
**Category:** Uncategorized\
**Created:** [July 9, 2004, 4:12pm UTC](https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015 "2004-07-09T16:12:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![LittleFenris](https://avatars.discourse-cdn.com/v4/letter/l/b5ac83/32.png) [@LittleFenris](https://forum.kirupa.com/u/LittleFenris)\
**Post date:** [July 9, 2004, 4:12pm UTC](https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015/1 "2004-07-09T16:12:53Z")

</div>

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.

Thanks

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

[hitTest SWF](http://www.littlefenris.com/Files/alpha.swf)  
[hitTest FLA](http://www.littlefenris.com/Files/alpha.fla)

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

```auto
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;
	}
}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 9, 2004, 7:43pm UTC](https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015/2 "2004-07-09T19:43:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 9, 2004, 8:31pm UTC](https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015/3 "2004-07-09T20:31:21Z")

</div>

> [@stringy](#):
>
> Yours didn`t stick for me but  
> why not use onRollOver/RollOut  
> 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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 9, 2004, 9:03pm UTC](https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015/4 "2004-07-09T21:03:51Z")

</div>

> [@LittleFenris](#):
>
> 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.

I`m afraid i`m 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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 9, 2004, 11:44pm UTC](https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015/5 "2004-07-09T23:44:02Z")

</div>

> [@stringy](#):
>
> I`m afraid i`m 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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 10, 2004, 7:12am UTC](https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015/6 "2004-07-10T07:12:36Z")

</div>

> [@LittleFenris](#):
>
> 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.

[http://www.gifsrus.com/testfile/alpha.fla](http://www.gifsrus.com/testfile/alpha.fla)

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 10, 2004, 7:55am UTC](https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015/7 "2004-07-10T07:55:58Z")

</div>

@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).

scotty(-:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 10, 2004, 9:59am UTC](https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015/8 "2004-07-10T09:59:12Z")

</div>

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

if (!hitTest(…)) { …

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 10, 2004, 10:04am UTC](https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015/9 "2004-07-10T10:04:05Z")

</div>

…and that’s true…:lol:

scotty(-:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 10, 2004, 10:09am UTC](https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015/10 "2004-07-10T10:09:03Z")

</div>

hehe :beam:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 10, 2004, 3:35pm UTC](https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015/11 "2004-07-10T15:35:04Z")

</div>

> [@stringy](#):
>
> 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.
> 
> [http://www.gifsrus.com/testfile/alpha.fla](http://www.gifsrus.com/testfile/alpha.fla)
> 
> The only reasons which i can think of why my code didn`t work is that you have 1/not put the code on the timeline containing both mc`s 2/not given them instance names.

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 10, 2004, 4:42pm UTC](https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015/12 "2004-07-10T16:42:01Z")

</div>

> [@LittleFenris](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 10, 2004, 5:56pm UTC](https://forum.kirupa.com/t/movieclip-as-button-hittest-working-intermitent-need-advice/57015/13 "2004-07-10T17:56:48Z")

</div>

> [@stringy](#):
>
> 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.
