By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

Button Rollover/rollout problems

P: 7
I am running on a Mac, with FlashCS3, using Actionscript 3.0.

First I'm going to say that I am really new to Actionscript. (As in, only been using for a few days new.) So please excuse me if I don't have the terminology down completely.

I am trying to create an interactive button where upon "rolling over" a certain movie clip is played, and upon "rolling out" another movie clip is played. I know, fairly simple, right?

After trying this by altering the the button's over state and having it not work, I read in a book that you can create movie clips that behave like buttons.

So I have done this. The clip-button is called "btn_red" In the main timeline, I have the layer with this button, and an actions layer.*

Within btn_red, I have this:



The actions layer you see above has this code attached to it:
Expand|Select|Wrap|Line Numbers
  1. stop();
  2. buttonMode = true;
The labels layer is just button states, the last one being "disabled."

The button graphics layer has two movie clips, one for the "over" and one for the "off." Within the movie clips are just basic graphics and text with motion tweens and alpha effects attached. The movie clips are "btn_rd_anim_in" and "btn_rd_anim_out."

Okay, now back to the main timeline. The actions layer mentioned above with the asterick.

My code is this:

Expand|Select|Wrap|Line Numbers
  1. //disabled function
  2. stop();
  3. // code_hint comments
  4. // MovieClip btn_red;
  5.  
  6. // event handlers
  7. function handleClick( pEvent:MouseEvent ):void
  8. {
  9.     if( pEvent == btn_red )
  10.     {
  11.         //handle event
  12.         btn_red.enabled = false;
  13.         btn_red.gotoAndStop( "_Disabled" );
  14.     }
  15. }
  16. // register events
  17. btn_red.addEventListener( MouseEvent.CLICK. handleClick );
  18.  
  19. //over function
  20. stop();
  21. // code_hint comments
  22. // MovieClip btn_red;
  23.  
  24. // event handlers
  25. function handleROLL_OVER( pEvent:MouseEvent ):void
  26. {
  27.     if( pEvent == btn_rd )
  28.     {
  29.         //handle event
  30.         btn_red.ROLL_OVER = true
  31.         gotoAndPlay("_over")
  32.     }
  33. }
  34. // register events
  35. btn_red.addEventListener( MouseEvent.ROLL_OVER, handleRollOver );
  36.  
  37. //off function
  38. stop();
  39. // code_hint comments
  40. // MovieClip btn_red;
  41. function handleROLL_OUT( pEvent:MouseEvent ):void
  42. {
  43.     if( pEvent == btn_rd )
  44.     {
  45.         //handle event
  46.         btn_red.ROLL_OUT == true
  47.         gotoAndPlay("_off")
  48.     }
  49. }
  50. // register events
  51. btn_red.addEventListener( MouseEvent.ROLL_OUT, handleRollOut );
  52. stop();
  53.  
***post continued in comment, for some reason would not let me continue here.**
Oct 20 '07 #1
Share this Question
Share on Google+
12 Replies


P: 7
**Continued from Above**

I am getting these errors upon playing through "Test Movie":

1176: Comparison between a value with static type flash.events:MouseEvent and a possibly unrelated type flash.display:MovieClip. (Line 9)

1120: Access of undefined property btn_rd. (Line 27)

1120: Access of undefined property btn_rd. (Line 43)

1119: Access of possibly undefined property handleClick through a reference with static type String. (Line 17)

1136: Incorrect number of arguments. Expected 2. (Line 52)

1120: Access of undefined property handleRollOver. (Line 35)

1120: Access of undefined property handleRollOut. (Line 51)

Any help would be really appreciated. I have worked over this for hours and still cannot find a problem....

Thank you!
Oct 20 '07 #2

100+
P: 149
**Continued from Above**

I am getting these errors upon playing through "Test Movie":

1176: Comparison between a value with static type flash.events:MouseEvent and a possibly unrelated type flash.display:MovieClip. (Line 9)

1120: Access of undefined property btn_rd. (Line 27)

1120: Access of undefined property btn_rd. (Line 43)

1119: Access of possibly undefined property handleClick through a reference with static type String. (Line 17)

1136: Incorrect number of arguments. Expected 2. (Line 52)

1120: Access of undefined property handleRollOver. (Line 35)

1120: Access of undefined property handleRollOut. (Line 51)

Any help would be really appreciated. I have worked over this for hours and still cannot find a problem....

Thank you!
Hi Flan,

I'm an AS 2.0 developer haven't started looking at 3.0 yet.

In 2.0 you wouldn't use event handler functions like this, behaviours are established in the MovieClip class. Like on(rollover), on(rollout), on(press), on(release) etc. In AS 2.0 you would simple tell the movieclip to gotoAndStop("_over");

Expand|Select|Wrap|Line Numbers
  1. on(rollover)
  2. {
  3. this.gotoAndStop("over");
  4. }
Maybe things are different in AS 3.0.?? Let me know.
Oct 20 '07 #3

P: 7
Hi Flan,

I'm an AS 2.0 developer haven't started looking at 3.0 yet.

In 2.0 you wouldn't use event handler functions like this, behaviours are established in the MovieClip class. Like on(rollover), on(rollout), on(press), on(release) etc. In AS 2.0 you would simple tell the movieclip to gotoAndStop("_over");

Expand|Select|Wrap|Line Numbers
  1. on(rollover)
  2. {
  3. this.gotoAndStop("over");
  4. }
Maybe things are different in AS 3.0.?? Let me know.
I think that they are different. I tried the code with my own info put in, and unfortunately it didn't work..

Thank you, though.
Oct 20 '07 #4

100+
P: 149
Hi Flan,

Looking up some 3.0 references it seems you headed down the right road. These functions should exist in a class that extends the movieclip class.

Line (9) you are checking to see if a mouseevent is == to movieclip instance.

I think you want to check against the CLICK string. CLICK : String = "click"

Expand|Select|Wrap|Line Numbers
  1. if(pEvent == "click")
line (27) typo btn_rd should be btn_red but this needs to be;

Expand|Select|Wrap|Line Numbers
  1. if(pEvent == "rollOver")
Line (17) is a type ',' instead of '.'

Line (35)&&(51) is another typo AS is case sensitive.

Line (52) stop(); is used in either to stop the timeline or stop media playback.

These functions should be inside a class, so you don't need this command.
Oct 21 '07 #5

P: 7
Hi Flan,

Looking up some 3.0 references it seems you headed down the right road. These functions should exist in a class that extends the movieclip class.

Line (9) you are checking to see if a mouseevent is == to movieclip instance.

I think you want to check against the CLICK string. CLICK : String = "click"

Expand|Select|Wrap|Line Numbers
  1. if(pEvent == "click")
line (27) typo btn_rd should be btn_red but this needs to be;

Expand|Select|Wrap|Line Numbers
  1. if(pEvent == "rollOver")
Line (17) is a type ',' instead of '.'

Line (35)&&(51) is another typo AS is case sensitive.

Line (52) stop(); is used in either to stop the timeline or stop media playback.

These functions should be inside a class, so you don't need this command.
I am still getting this error on lines 9, 27, and 43:
"1176:Comparison between a value with static type flash.events:MouseEvent and a possibly unrelated type String."

It seems as though Flash isn't registering my movieclip as a button, and I don't know why...

I got this code in another forum, but I'm not sure if adding the second line would be redundant, as I already put it within the movieclip itself, or if I need it there as well. I am also not sure how to incorporate the addchild function into my script.

Expand|Select|Wrap|Line Numbers
  1. my_btn.addEventListener(MouseEvent.CLICK, myFunction);
  2. my_btn.buttonMode = true;
  3. function myFunction(event:MouseEvent):void {
  4.     //do something
  5. }
  6. addChild(my_btn);
Thank you once again for your help.
Oct 21 '07 #6

xNephilimx
Expert 100+
P: 213
Hi Flan!
I'll get to your errors before this:
Rsdev: In AS3 you MUST use event handlers to register events, behaviours are now deprecated.

Now, Flan
The errors are quite simple. I'll explain them to you one by one:

-1176: Comparison between a value with static type flash.events:MouseEvent and a possibly unrelated type flash.display:MovieClip. (Line 9):
In the line 9 of your code you have:
if( pEvent == btn_red )
pEvent is a MouseEvent, not the button itself, if you need to get the button that recieves the event, you should do:

pEvent.target

The target of the event is the button that, in this case, has been clicked, so pEvent.target will return the actual button.
Anyway, I don't quite understand the purpose of that condition...

1120: Access of undefined property btn_rd. (Line 27)
and
1120: Access of undefined property btn_rd. (Line 43)
:

Your are getting those errors because of a typo! You put btn_rd, instead if btn_red.

1119: Access of possibly undefined property handleClick through a reference with static type String. (Line 17):
This error is because of a typo, also!
You have
btn_red.addEventListener( MouseEvent.CLICK. handleClick );
on line 17, and you should have this:
btn_red.addEventListener( MouseEvent.CLICK, handleClick );
Notice the comma after the MouseEvent.CLICK? You put a dot, and should be a comma.

1136: Incorrect number of arguments. Expected 2. (Line 52):
That error is probably being draged since the line 51 where you put as a callback for the event the function handleRollOut, and that function does not exists, remember that AS is case sensitive, your function is called handleROLL_OUT.
Anyway, that stop(); in line 52 is not nessesary, since you already put one in the beggining of your code for that frame.

1120: Access of undefined property handleRollOver. (Line 35):
Same as above, your function is handleROLL_OVER, not handleRollOver

1120: Access of undefined property handleRollOut. (Line 51)
I already explaind this in the error 1136.

Now that you now what is causing them, you should be able to correct them.

One more thing: declaring the buttonMode for the movieclip that will be acting as one is very important, since it makes it show the hand curson on roll over letting the user know that he can click it. You do it like this:

btn_red.buttonMode = true;

It is not vital, since it will work anyways without that, the thing is you won't get the hand cursor when hovering the button, and that will make users think it is not a button.

Best regards,
The_Nephilim
Oct 22 '07 #7

P: 7
Hi Flan!
I'll get to your errors before this:
Rsdev: In AS3 you MUST use event handlers to register events, behaviours are now deprecated.

Now, Flan
The errors are quite simple. I'll explain them to you one by one:

-1176: Comparison between a value with static type flash.events:MouseEvent and a possibly unrelated type flash.display:MovieClip. (Line 9):
In the line 9 of your code you have:
if( pEvent == btn_red )
pEvent is a MouseEvent, not the button itself, if you need to get the button that recieves the event, you should do:

pEvent.target

The target of the event is the button that, in this case, has been clicked, so pEvent.target will return the actual button.
Anyway, I don't quite understand the purpose of that condition...

1120: Access of undefined property btn_rd. (Line 27)
and
1120: Access of undefined property btn_rd. (Line 43)
:

Your are getting those errors because of a typo! You put btn_rd, instead if btn_red.

1119: Access of possibly undefined property handleClick through a reference with static type String. (Line 17):
This error is because of a typo, also!
You have
btn_red.addEventListener( MouseEvent.CLICK. handleClick );
on line 17, and you should have this:
btn_red.addEventListener( MouseEvent.CLICK, handleClick );
Notice the comma after the MouseEvent.CLICK? You put a dot, and should be a comma.

1136: Incorrect number of arguments. Expected 2. (Line 52):
That error is probably being draged since the line 51 where you put as a callback for the event the function handleRollOut, and that function does not exists, remember that AS is case sensitive, your function is called handleROLL_OUT.
Anyway, that stop(); in line 52 is not nessesary, since you already put one in the beggining of your code for that frame.

1120: Access of undefined property handleRollOver. (Line 35):
Same as above, your function is handleROLL_OVER, not handleRollOver

1120: Access of undefined property handleRollOut. (Line 51)
I already explaind this in the error 1136.

Now that you now what is causing them, you should be able to correct them.

One more thing: declaring the buttonMode for the movieclip that will be acting as one is very important, since it makes it show the hand curson on roll over letting the user know that he can click it. You do it like this:

btn_red.buttonMode = true;

It is not vital, since it will work anyways without that, the thing is you won't get the hand cursor when hovering the button, and that will make users think it is not a button.

Best regards,
The_Nephilim
Thanks so much! You managed to clear me of all my error messages. I am still having one problem, however. I want the button, on rollover, to have a menu bar fade outwards, and on rollout, to have it fade back in. However, instead of this, it keeps looping. This is my code thus far.

Expand|Select|Wrap|Line Numbers
  1. //disabled function
  2. stop();
  3. // code_hint comments
  4. // MovieClip btn_red;
  5.  
  6. // event handlers
  7. function handleClick( pEvent:MouseEvent ):void
  8. {
  9.     if( pEvent.target )
  10.     {
  11.         //handle event
  12.         mc_btn_red.buttonMode = true;
  13.         mc_btn_red.enabled = false;
  14.         mc_btn_red.gotoAndStop( "_Disabled" );
  15.     }
  16. }
  17. // register events
  18. mc_btn_red.addEventListener( MouseEvent.CLICK, handleClick );
  19.  
  20. //over function
  21. stop();
  22. // code_hint comments
  23. // MovieClip mc_btn_red;
  24.  
  25. // event handlers
  26. function handleROLL_OVER( pEvent:MouseEvent ):void
  27. {
  28.     if( pEvent.target )
  29.     {
  30.         //handle event
  31.         mc_btn_red.buttonMode = true;
  32.         mc_btn_red.ROLL_OVER = true;
  33.         mc_btn_red.gotoAndPlay("_over");
  34.     }
  35. }
  36. // register events
  37. mc_btn_red.addEventListener( MouseEvent.ROLL_OVER, handleROLL_OVER );
  38.  
  39. //off function
  40. stop();
  41. // code_hint comments
  42. // MovieClip btn_red;
  43. function handleROLL_OUT( pEvent:MouseEvent ):void
  44. {
  45.     if( pEvent.target)
  46.     {
  47.         //handle event
  48.         mc_btn_red.buttonMode = true;
  49.         mc_btn_red.ROLL_OUT == true;
  50.         mc_btn_red.gotoAndPlay("_off");
  51.     }
  52. }
  53. // register events
  54. mc_btn_red.addEventListener( MouseEvent.ROLL_OUT, handleROLL_OUT );
Thanks once again! You may just have saved my midterm project. I've literally been using actionscript for only a week or so, so I'm sorry if my questions are basic and stupid.
Oct 22 '07 #8

xNephilimx
Expert 100+
P: 213
Hi!
Don't worry about asking, AS3 is a difficult language to start learning, and you got it pretty good.

Looking at your code I think that the menu is inside that mc_btn_red, am I right?
You may be forgot to put a stop in the key frames. I mean, your movielclip button has 3 keyframes that are the three states of a button, on, over, out, for example, so when you roll over you make gotoAndPlay('_over'); but since there are no stops in the frames it will start playing from the _over frame and won't stop.

Suppose you button's timeline is something like this:

(The are the keyframes)
Expand|Select|Wrap|Line Numbers
  1. ------------------------
  2. |on------|over----|out-----|
  3.  
Try putting in each keyframe a stop(); and see what happens.

Also, the "if" you have in each function, in not necessary

Best regards,
The_Nephilim

Thanks so much! You managed to clear me of all my error messages. I am still having one problem, however. I want the button, on rollover, to have a menu bar fade outwards, and on rollout, to have it fade back in. However, instead of this, it keeps looping. This is my code thus far.

Expand|Select|Wrap|Line Numbers
  1. //disabled function
  2. stop();
  3. // code_hint comments
  4. // MovieClip btn_red;
  5.  
  6. // event handlers
  7. function handleClick( pEvent:MouseEvent ):void
  8. {
  9.     if( pEvent.target )
  10.     {
  11.         //handle event
  12.         mc_btn_red.buttonMode = true;
  13.         mc_btn_red.enabled = false;
  14.         mc_btn_red.gotoAndStop( "_Disabled" );
  15.     }
  16. }
  17. // register events
  18. mc_btn_red.addEventListener( MouseEvent.CLICK, handleClick );
  19.  
  20. //over function
  21. stop();
  22. // code_hint comments
  23. // MovieClip mc_btn_red;
  24.  
  25. // event handlers
  26. function handleROLL_OVER( pEvent:MouseEvent ):void
  27. {
  28.     if( pEvent.target )
  29.     {
  30.         //handle event
  31.         mc_btn_red.buttonMode = true;
  32.         mc_btn_red.ROLL_OVER = true;
  33.         mc_btn_red.gotoAndPlay("_over");
  34.     }
  35. }
  36. // register events
  37. mc_btn_red.addEventListener( MouseEvent.ROLL_OVER, handleROLL_OVER );
  38.  
  39. //off function
  40. stop();
  41. // code_hint comments
  42. // MovieClip btn_red;
  43. function handleROLL_OUT( pEvent:MouseEvent ):void
  44. {
  45.     if( pEvent.target)
  46.     {
  47.         //handle event
  48.         mc_btn_red.buttonMode = true;
  49.         mc_btn_red.ROLL_OUT == true;
  50.         mc_btn_red.gotoAndPlay("_off");
  51.     }
  52. }
  53. // register events
  54. mc_btn_red.addEventListener( MouseEvent.ROLL_OUT, handleROLL_OUT );
Thanks once again! You may just have saved my midterm project. I've literally been using actionscript for only a week or so, so I'm sorry if my questions are basic and stupid.
Oct 22 '07 #9

P: 7
Hi!
Don't worry about asking, AS3 is a difficult language to start learning, and you got it pretty good.

Looking at your code I think that the menu is inside that mc_btn_red, am I right?
You may be forgot to put a stop in the key frames. I mean, your movielclip button has 3 keyframes that are the three states of a button, on, over, out, for example, so when you roll over you make gotoAndPlay('_over'); but since there are no stops in the frames it will start playing from the _over frame and won't stop.

Suppose you button's timeline is something like this:

(The are the keyframes)
Expand|Select|Wrap|Line Numbers
  1. ------------------------
  2. |on------|over----|out-----|
  3.  
Try putting in each keyframe a stop(); and see what happens.

Also, the "if" you have in each function, in not necessary

Best regards,
The_Nephilim
Yes, I tried that too. Is there a way to define the hit state of an mc button? The book I've been using says it's uneccessary, but it doesn't say if you can or not. I think that may be the problem, though I could be wrong.
Oct 22 '07 #10

xNephilimx
Expert 100+
P: 213
The book is right, it's not necessary, the hit area for your button will be every part of it's content, so if your button were a circle, all the circle will be the hit area. Anyway, if you need to make a hit area, maybe 'cause your button has an irregular shape with holes or something, you can do a transparent shape, so the user won't see it but it will be there, clickable and all.

Anyway I really don't think that's the problem. First get rid of the if( pEvent.target) in the functions, then see if you could upload your fla file, so I can check all of it and see if I find the problem.

Best regards
The_Nephilim

Yes, I tried that too. Is there a way to define the hit state of an mc button? The book I've been using says it's uneccessary, but it doesn't say if you can or not. I think that may be the problem, though I could be wrong.
Oct 23 '07 #11

P: 7
The book is right, it's not necessary, the hit area for your button will be every part of it's content, so if your button were a circle, all the circle will be the hit area. Anyway, if you need to make a hit area, maybe 'cause your button has an irregular shape with holes or something, you can do a transparent shape, so the user won't see it but it will be there, clickable and all.

Anyway I really don't think that's the problem. First get rid of the if( pEvent.target) in the functions, then see if you could upload your fla file, so I can check all of it and see if I find the problem.

Best regards
The_Nephilim
For some reason my file was lost. I had to go back and start from scratch, but the good thing is that I can use actionscript 2.0. Much much easier. I got everything to work. Thank you for your help. :)
Oct 26 '07 #12

xNephilimx
Expert 100+
P: 213
Oh, that bites!
Well, if you feel mor comfortable with AS2 it's a good thing.
Anyway, if you ever got good practice with AS3 you won't go back using AS2, I tell you, it happened to me LOL

Best regards!
The_Nephilim

For some reason my file was lost. I had to go back and start from scratch, but the good thing is that I can use actionscript 2.0. Much much easier. I got everything to work. Thank you for your help. :)
Oct 27 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.