as3 : Error #1063 MethodInfo-165() Argument Mismatch - actionscript-3

I encountered an error in Flash CS5.5 ( ActionScript 3 ) :
ArgumentError: Error #1063: Argument count mismatch on
MethodInfo-185(). Expected 1, got 0. at MethodInfo-186()
But I have no MethodInfo-185() and MethodInfo-186() . What's wrong with the Flash ?

Somehow Flash CS5.5 / AS3 compiler cannot identify nested functions. The compiler will refer nested functions ( myInnerFunction as example below ) as MethodInfo-123() ( or something similar ).
function myFunction() {
function myInnerFunction() {
}
}

This means yes, you have there an unnamed function. Make sure you have all event listeners enumerated, and check if you have a listener added like this:
addEventListener(Event.ENTER_FRAME,function():void {...});
Any event can be in place of an enter frame event I wrote. If so, this is the line with an error. An event listener function should always accept 1 parameter of corresponding Event type. In this case, the correct line should be:
addEventListener(Event.ENTER_FRAME,function(e:Event):void {...});
Note the parameter type. If you, for example, listen for a "click" mouse event, it should be of MouseEvent type instead.

Related

How to pass object, MouseEvent.CLICK and function to trigger

I want to pass to function object, const of type MouseEvent.CLICK and function to trigger. In my case:
my class Assistant:
public static function addEventListenerTo(obj:Object, MouseEventConst:String, functinToTrigger:Function) {
obj.addEventListener(MouseEventConst, functinToTrigger:Function);
}
and my class Engine which invokes
Assistant.addEventListenerTo(deck,"MouseEvent.CLICK",showObject);
Please give me advice how to make it work. Thanks.
In the code you provide there is one compiler error (the one Tahir Ahmed pointed to in his second comment).
Fixing this by removing the second :Function in the first code block:
public static function addEventListenerTo
(obj:Object, MouseEventConst:String, functinToTrigger:Function)
{
obj.addEventListener(MouseEventConst, functinToTrigger);
}
will let the code compile. (I wrapped the Method signature to avoid the scrollbar, this is not required to make it compile.)
The other major problem is a configuration error (or maybe a typo): the one about MouseEvent.CLICK. (the one Tahir Ahmed pointed to in his first comment)
Looking at the documentation it is defined to have the value "click" (a String literal following the AS3 convention of the lowercase constant name). So to pass it to your method you can either put in a reference to the constant by writing MouseEvent.CLICK (without the "s around it) or reach the same goal with passing its value by writing "click".
As using the reference will prevent mistyping because the compiler checks it, the first approach should be preferred.
So calling the Method should look like this:
Assistant.addEventListenerTo(deck, MouseEvent.CLICK, showObject);
If you want to know why your version didn't work you should read a simple introduction to AS3 Events and EventDispatchers. As a short hint: if deck would dispatch an Event that has its type property set to "MouseEvent.CLICK" your listener would get fired.
While you are at it, you could improve the quality of your code by to major things:
the first one is about avoiding getting runtime Errors and prefering compile time errors: Not every instance of type Object has a method called addEventListener. In your current code, when you pass an instance to Assistant.addEventListenerTo as first parameter, that doesn't have this method (e.g. {} or an instance of type Array), the error will get thrown while your swf is displayed and it might stop displaying anything and might show an error message to the user.
If the type of the parameter is IEventDispatcher instead, the compiler will already tell you that you passed an incompatible instance.
The second one is about names and conventions, which helps other developers to read your code (an having more fun helping you).
what you called MouseEventConst is called an event type in AS3, which provides a better name for a parameter, as it being a String nobody stops anybody from passing contants of other event types like Event
the functionToTrigger is what is called a listener (or event listener)
the first letter of parameter names should be lower case
So if I would have written the static method it would look like this:
import flash.events.*;
public class Assistent{
public static function addEventListenerTo
(dispatcher:IEventDispatcher, eventType:String, listener:Function)
{
dispatcher.addEventListener(eventType, listener);
}
}

AS3 null object error on movie clip 1009 cannot access

Hello I was wandering if someone could help, I keep getting this error spat back at me when I try to launch something in as3. Do I need to import something? Apologies, I originally learned AS2 and I'm now slowly learning AS3.
baby steps.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at draganddropframe1_resetter2_0_fla::MainTimeline/frame3()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
this.window_mc.visible = true;
this.windwo_mc.offwindow.addEventListener(MouseEvent.CLICK, shutwin);
function shutwin(event:MouseEvent):void
{
this.window_mc.visible = false;
}
UPDATES -----------
A method that appears to have worked around it by changing the direct path to 'this'. Clicked inside the movieclip, added a layer called actions and inserted this script that referenced the movieClip I wanted to hide when clicked.
this.addEventListener(MouseEvent.CLICK, fl_ClickToHide);
function fl_ClickToHide(event:MouseEvent):void
{
this.visible = false;
}
The error you are getting means an object, (moveiclip, variable, etc ) doesn't exist.
Go to your publish settings, and check the box that says "permit debugging". Then when you get that error in your output window it will also display the exact line number the error occurred on. That line will probably tell you what object is null.
It looks like you have a typographical error in your second line. This line
this.windwo_mc.offwindow.addEventListener(MouseEvent.CLICK, shutwin);
should be
this.window_mc.offwindow.addEventListener(MouseEvent.CLICK, shutwin);
You've interchanged w and o.
Otherwise, I would suggest doing Ribs's answer.
UPDATES ----------- "from me "
A method that appears to have worked around it by changing the direct path to 'this'. Clicked inside the movieclip, added a layer called actions, and inserted this script that referenced the movieClip I wanted to hide when clicked.
this.addEventListener(MouseEvent.CLICK, fl_ClickToHide);
function fl_ClickToHide(event:MouseEvent):void
{
this.visible = false;
}
Place this script inside the Movie.

Actionscript 3.0: What does the parameter 'e' do in e:KeyboardEvent

Ive been using the 'event' parameter for my KeyboardEvents and MouseEvents in a recent project ive been working on for my course (VERY BASIC).
Im not entirely sure on what the 'e' part of e:KeyboardEvent actually does, and ive been asked to find out what information the parameter 'e' can actually access when using it.
Im sorry if the questions badly written, its been a long night!
EDIT: If A method takes the parameter (e:KeyboardEvent). what information could we access through the use of the parameter e?
e represents an instance of KeyboardEvent (the instance being passed to your listening function).
The most important property of KeyboardEvent (referenced by e in your example) is keyCode.
This determines which key is being pressed/released.
eg:
stage.addEventListener(KeyboardEvent.KEY_DOWN, _keyDown);
function _keyDown(e:KeyboardEvent):void
{
trace(e.keyCode); // Will be 65 if you press 'a'.
}
I'm assuming you have some function like this
function someFunction(e:KeyboardEvent):void
{
// code
}
You can access any information from the KeyboardEvent class, just the same as if the parameter were called "event". The name of the parameter doesn't affect what you can access through it; the type does.
Edit: "e" is just the name of the variable - it could be called fred, banana, or tyrannosaurusRex, and it would make no difference. The thing that determines what sort of information you can access through a variable is its type - in this case, KeyboardEvent. If you follow the KeyboardEvent link above, you will see documentation for the KeyboardEvent class, which will tell you all the things you can do with it. For example, one of the properties of KeyboardEvent is keyCode, which tells you which key was pressed:
if (e.keyCode == 32)
{
// 32 is the keyCode for spacebar, so spacebar was pressed
}
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyboardHandler);
function keyboardHandler(Jack:KeyboardEvent):void{
trace(Jack.keyCode);///----------see output pannel
}
/////////////////--------or
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyboardHandler2);
function keyboardHandler2(Banana:KeyboardEvent):void{
trace(Banana.keyCode);////////----see output pannel
}
You can type anything inside()including KeyboardEvent
You're naming the event that triggers the function, just like any other var it can be named anything. Then, depending on the type of event, you'll have access to a number of vars and function relating to whatever caused the event to trigger.
Edit: Here's what's available to you with a MouseEvent (Public Properties)

Comparing MouseEvent

I have more than one Event Listener that calls a specific function, and I want to add if statements to check what event is being passed. How can I do that?
I tried a simple comparison like:
if(evt == MouseEvent.MOUSE_OVER)
But it will give an error because I comparing a MouseEvent object and a String.
You should use the type property of the event:
if(evt.type == MouseEvent.MOUSE_OVER)

Isn't it redundant to declare the data type of an event object in a listener function's parameters?

When you click on the button something happens. However it seems redundant to me that in the declaration of myListenerFunction, the event object e of class MouseEvent, actually has to have its data type MouseEvent mentioned.
mybutton.addEventListener(MouseEvent.CLICK, myListenerFunction);
function myListenerFunction(e:MouseEvent):void
{
// function body
}
Couldn't I get away with this (the .swf works just the same so far as I know...)?
function myListenerFunction(e):void
Since the data type of e should always match the class of the event MouseEvent.CLICK (which is MouseEvent)?
EDIT:
So let's say we go from a mouse event to a keyboard event. By not declaring the data type of e, we can not be prone to errors in not changing the data type of e. e by default is going to be of type KeyboardEvent
mybutton.addEventListener(KeyboardEvent.KEY_DOWN, myListenerFunction);
function myListenerFunction(e):void
{
// function body
}
You can keep the event type to the base class Event if you like. But you will not have access to any of the MouseEvent / KeyboardEvent-specific members when you do it like that.
Using it without a type will make it Object, which is dynamic, meaning you can try to access any member by name (even if it does not exist) - this is slower (a lot) and fairly error prone. You will not get compile time checking for example.