PDA

View Full Version : Problems with onRollOver in ActionScript 3.0


xanti
06-19-2007, 03:43 PM
As a Flash/ActionScript noob I find the podcasts at www.learnflash.com really helpful and have been following a 2005 one (http://www.learnflash.com/wp/2005/creating-movie-clip-buttons/) to learn how to make movie-clip buttons. I'm using Flash CS3.

However, when I finish creating the button and come to execute the code

this.button_mc.onRollOver = function() {
button_mc.gotoAndPlay("_over");
}

this.button_mc.onRollOut = function() {
button_mc.gotoAndPlay("_out");
}

I get a compilation error (posted below because this post seems to cut itself off after the statement). I vaguely understand what it means, but don't know how to do it. I tried adding

addEventListener ('mouseOver', callback_handler)

as the first line of the script (and ditto for onRollOut) substituting onRollOver/Out for callback_handler and it executed OK, but just cycles through the animation without reacting to mouse movements.

Can someone please tell me what I need to do here? The error message is

Warning: 1090: Migration issue: The onRollOver event handler is not triggered automatically by Flash Player at run time in ActionScript 3.0. You must first register this handler for the event using addEventListener ( 'mouseOver', callback_handler).

_Aerospace_Eng_
06-19-2007, 08:57 PM
You could either A) select Actionscript 2.0 as your new document to begin with where the tutorial will work or B) if you want to stick to Actionscript 3 then you need to use this
var newbutton:Sprite = new Sprite();
newbutton.buttonMode = true;
newbutton.addChild(button_mc);
addChild(newbutton);
this.button_mc.addEventListener("mouseOver",doOver);
this.button_mc.addEventListener("mouseOut",doOut);
function doOver(evt:MouseEvent)
{
button_mc.gotoAndPlay("_over");
}
function doOut(evt:MouseEvent)
{
button_mc.gotoAndPlay("_out");
}

xanti
06-20-2007, 12:42 AM
Jings! I think I'll stick to ActionScript 2.0 ... it looks a lot easier to learn.

Many thanks for such a quick and comprehensive response.

Pmorgan2
08-19-2009, 07:17 AM
I know I'm dredging up a really old post here, and the original responder might not be around anymore, but I thought I'd give it a shot anyway since I have a follow-up question to this very thread.

First off, I'd like to thank _Aerospace_Eng_ if he is indeed still around, because his post really helped me out. I understood the AS 2.0 way of doing this, but couldn't get it to work with my new version of Flash (CS4). Now I know the error of my ways.

However, my understanding of what the Sprite class is doing is somewhat limited. I can get one button to work, but what I would really like to do is get multiple buttons using the same EventListener functions.

Here is my working code for one button (movie clip):

var picbutton:Sprite = new Sprite();
picbutton.buttonMode = true;
picbutton.addChild(pictures);
this.addChild(picbutton);

this.pictures.addEventListener(MouseEvent.MOUSE_OVER,doOver);
this.pictures.addEventListener(MouseEvent.MOUSE_OUT,doOut);
this.pictures.addEventListener(MouseEvent.CLICK,doClick);

function doOver(evt:MouseEvent) {
pictures.gotoAndPlay(2);
}
function doOut(evt:MouseEvent) {
pictures.gotoAndStop(1);
}
function doClick(evt:MouseEvent) {
pictures.gotoAndPlay(17);
}

So I would like to add instances other than pictures, attach the same EventListeners, and have them run the same action, but on different instances.

Do I need to call a new Sprite() for each new button? And how can I change the code so instead of pictures.gotoAndPlay(2); it works for any instance I send at it (such as this.gotoAndPlay(2), except that doesn't work)?

I hope I didn't kill you with a wall of text, or confuse you with my poor knowledge on ActionScripting. I'm a computer science student, but I'm studying Java, not AS 3.0. Therefore I understand basic code, but not Adobe specific stuff.

Thanks again!

Paul Morgan

Pmorgan2
08-20-2009, 02:09 AM
Okay after reading more I think I know how to solve my own question. I haven't tried it yet, but I believe I should move all my code into classes out of the program. Then I can make reusable methods and better instantiate my objects.

seco
08-20-2009, 02:20 AM
Jings! I think I'll stick to ActionScript 2.0 ... it looks a lot easier to learn.

Many thanks for such a quick and comprehensive response.

AS3 isnt much different. you just define the buttons actions in an actions layer rather than in the button itsself.