The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.

ForumsProgramming ForumProblems with a MouseEvent function

10 4379
Wilon
offline
Wilon
7 posts
Nomad

I'm new to Actionscript 3.0 (and Flash), and am currently having a problem with a MouseEvent function in my first movie.

I'm trying to make the object 'mainVersionInfo' change colour when moused over. Instead of using colorTransform, I'm using labels as shown in the snippets of code below.

function menu():void{
mainVersionInfo.addEventListener(MouseEvent.MOUSE_OVER,glowInfo);
}
function glowInfo(Event:MouseEvent):void{
mainVersionInfo.gotoAndStop("mouseOff"
}

Upon testing the movie, I receive the following compiler error:

1120: Access of undefined property mainVersionInfo.

I'm able to fix this error by moving the 'glowinfo' function inside the 'menu', but I don't think this is the best solution. Can anybody explain to me why this error is occuring, and what the 'roper' way to fix it would be?

  • 10 Replies
beech
offline
beech
107 posts
Nomad

hmmm - well it should be ok if you do have an instance name of mainVersionInfo on the stage. however, there is one issue in the handler declaration, you should try not to use 'reserved names' as your property names, ie. Event:MouseEvent - try it with simply, e:MouseEvent - as 'Event' is a system package

but additionally, what your description leads me to believe is that you have the instance 'mainVersionInfo' declared as a property (var) within the menu() method? this would mean that the instance is 'local' to the scope of the method and doesn't exist outside that scope - which would cause the 1120

additionally another way to target the instance within a handler, is to use: e.currentTarget - provided that 'e' is the property name of the event argument

Wilon
offline
Wilon
7 posts
Nomad

So...are you saying that, if I use the var property to create the instance inside the menu() method, it only exists inside that method?

beech
offline
beech
107 posts
Nomad

yes that is correct

Wilon
offline
Wilon
7 posts
Nomad

So how do I fix that?

beech
offline
beech
107 posts
Nomad

ok - simply move the declaration outside the method so that it looks more like this:

var mainVersionInfo:MovieClip;

function menu():void{
mainVersionInfo = new MainVersionInfo();
mainVersionInfo.addEventListener(MouseEvent.MOUSE_OVER, glowInfo);
addChild( mainVersionInfo );
}

function glowInfo(Event:MouseEvent):void{
e.currentTarget.gotoAndStop("mouseOff"
}

BUT there are several factors here that i am guessing at since i don't know the entire structure of your code and timeline - i would assume that you are propagating the instance within the menu() method when called and then bringing it to the stage within the method - IF this is the case then it explains the 1120

however, if you have an instance that is 'laced' on the stage that has an instance name of 'mainVersionInfo' - then it is a different issue - which probably means that the instance is not 'instantiated' at the time of the menu() method call

but you have said that it does function when placed inside the method, so i would assume that the property is being constructed within that scope - but again, a lot of speculation here not knowing the entire structure of your app - and i suspect, more code in the menu() method not shown above

beech
offline
beech
107 posts
Nomad

sorry i have a copy-paste error in the above code - make sure to change:

glowInfo(Event:MouseEvent)

TO

glowInfo(e:MouseEvent)

Wilon
offline
Wilon
7 posts
Nomad

That's basically the solution I already had...I guess I'll go with it.
Thank you for all that. ^^

beech
offline
beech
107 posts
Nomad

your welcome

but, i'm a bit confused - you've said that it works when placing the glowInfo() method inside the menu() method (this is called nesting) - which is not the solution i have above.

Wilon
offline
Wilon
7 posts
Nomad

The solution I was referring to was using e.currentTarget, which I had also tried previously.

beech
offline
beech
107 posts
Nomad

ahhh very good - then the issue may ultimately have been the use of "Event" as the argument property.

Showing 1-10 of 10