I have been trying to develop a CustomButton class that extends MovieClip, however I am having problems. I have got this error:
ArgumentError: Error #1063: Argument count mismatch on
mkh.custombutton::CustomButton(). Expected 2, got 0. at
flash.display::Sprite/constructChildren() at flash.display::Sprite()
at flash.display::MovieClip()
I've tried to debug my code, but it says
"Cannot display source code at this location."
I am not sure where is the problem, but I suppose it's in the constructor:
public function CustomButton( buttonlabel:String, animationAR:Array, active:Boolean=true, animated:Boolean = false, type:String = "free", group:int = 0 )
I would be very grateful if anyone helped me. Thank you.
EDIT2: I think I know why it's not appearing, so nevermind.
Seems like you must be instantiating CustomButton without passing it any arguments.
Like so:
var cBtn = new CustomButton();
However, you constructor has 2 arguments that must be passed - buttonLabel and animationAR (the rest are OK because they are assigned a default value).
So you should be doing something like this:
var cBtn = new CustomButton('Test', someArray);
I think I know what the problem is, now I hope I can explain to you clearly enough (English is not my first language). Did you by any chance make a graphic MovieClip in the Flash program and linked it to your CustomButton class? If so, be careful with the instances you might have on the stage, because when Flash creates the Sprites/Movieclips objects that are on the stage it calls their constructor without any parameters.
To avoid this, either:
Set default values for all parameters in your CustomButtonClass (EDIT: which would solve your problem, but is not very good practice)
Use addChild to put instances of your Button onto the stage (I recommend this one)
Hope this helps!
public function CustomButton(
buttonlabel:String,
animationAR:Array,
active:Boolean=true,
animated:Boolean = false,
type:String = "free",
group:int = 0
);
That's how you defined your constructor. This means that the first 2 arguments (buttonlabel abd animationAR) are required arguments. The rest are optional.
Now if you try to instantiate this like
var cb:CustomButton=new CustomButton();
You are not passing any arguments to the constructot, which will throw that error.
Note that this is what happens when you create the object directly in the UI.
A way to fix this would be to redefine the constructor as:
public function CustomButton(
buttonlabel:String="CustomButton",
animationAR:Array=[],
active:Boolean=true,
animated:Boolean = false,
type:String = "free",
group:int = 0
);
This makes all arguments optional and should work. Of course, you'll be best off putting the default value of the arguments as something you know will work. For example, in my example, the empty array default for animationAR could break your code, in which case you need to add this to the constructor body:
if(animationAR.length==0) {
animationAR.push(new Animation());
//YOU WILL HAVE TO CHANGE THIS LINE TO CORRESPOND TO YOUR CODE
}
OR ELSE, you could instantiate the object as
var cb:CustomButton=new CustomButton("My Crazy-ass CustomButton", animArray);
Related
I have a class of a movieclip symbol that is called third_scene_border, I create 12 instances of this class like so:
public var border_1:third_scene_border = new third_scene_border();
public var border_2:third_scene_border = new third_scene_border();
public var border_3:third_scene_border = new third_scene_border();
and so on, I also set the name of the first instance to be "first_border" like so:
border_1.name = "first_border";
Then when I trace its name I get "instance(some numbers)".
Why isn't the name property being set correctly? Ive done it for a lot of other instances and its working just fine. I am trying to see on which border a draggable object is being dropped.
Edit: When I write trace(border_1.name) I get "first_border", but when I add an event listener that listens for clicks and put
trace(event.target.name);
in its function, I get instance(some numbers).
Edit: trace(event.target.parent.name); returns first_border which is correct but when I try to trace the dropTarget in the function of a MouseEvent.MOUSE_UP like so: trace(event.target.dropTarget.parent.name), I get the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at mainClass/up()
It's hard to find a 100% solution for your case, because you didn't attach the part of your code with adding the event listener. But, I'd hazard a guess, that you should try to use the event.currentTarget parameter instead of event.target.
You may read more about differences about theese 2 properites here:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html#target
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html#currentTarget
Inside my classes, I invoke this function
MovieClip(root).increaseScore();
which handles the score in the main .as file.
It all works fine during the execution of the level. However when the level is finished and the screen goes to another frame, the game crashes and gives me this error
TypeError: Error #1009: Cannot access a property or method of a null
object reference.
on the line above.
How do I fix this?
Thanks
edit:
This is were I tell it to addScore, this is in the GameController.as file
private function removeBubble(bubble, addScore:Boolean)
{
var delay:Timer = new Timer(200, 1);
delay.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent)
{
if(bubble.parent==mcGameStage)
{
var j:int = bubbleList.indexOf(bubble);
bubbleList.splice(j,1);
if(addScore) bubble.addScore();
mcGameUI.txtScorePlayer.text = String(playerScore);
mcGameStage.removeChild(bubble);
}
e.currentTarget.removeEventListener(e.type, arguments.callee);
checkWin();
});
delay.start();
}
here is the checkWin function:
private function checkWin()
{
if (playerBlue + playerRed + playerYellow + playerOrange + playerPurple + playerGreen == 0)
{
gameWin();
}
}
private function gameWin()
{
while (bubbleList.numChildren > 0)
{
bubbleList.removeChildAt(0);
}
mcGameUI.btnMixBlue.removeEventListener(MouseEvent.CLICK, mixBlue);
mcGameUI.btnMixRed.removeEventListener(MouseEvent.CLICK, mixRed);
mcGameUI.btnMixYellow.removeEventListener(MouseEvent.CLICK, mixYellow);
mcGameUI.btnNeedle.removeEventListener(MouseEvent.CLICK, activateNeedle);
mcGameStage.removeEventListener(Event.ENTER_FRAME,update);
mcGameStage.removeEventListener(MouseEvent.CLICK, checkToHit);
removeEventListener(Event.ADDED_TO_STAGE, gameAddedToStage );
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
if (mouseCursor != null)
{
mouseCursor.removeEventListener(Event.ENTER_FRAME,followMouse);
mouseCursor = null;
}
gotoAndPlay("level1win");
}
And inside my classes,
public function addScore()
{
root["increaseScore"]();
}
This is what increaseScore does
public function increaseScore()
{
playerScore += 1000;
}
So where is the null object? D:
Also I am very inexperienced using the debugger so I apologize if this could be easily solved with that. I tried it and couldn't figure it out before coming here.
What is OPP method?
What is FrameScript?
Also, the class is MovieClip
thanks :)
Fixing this is done with the help of the debugger!
The error is telling you that something is null and you're trying to access it. That's not good, so let's think what could be null?
1) Your movie clip instance might be null.
2) increaseScore() method of your movie clip instance might try to access something that is null
You didn't post any code that I could analyse at the time I'm writing this answer, so I can't say for sure.
Some possible problems:
Your class is not called MovieClip, but you're just trying to cast your root object to a MovieClip. Thing is, MovieClip's don't have a increaseScore() method. You should instead call the increaseScore() method with
root["increaseScore"]();
This will call the method of your root timeline, but since we are using weak-typing, you might have problems with debugging it later. But I guess that's the price you pay when writing all the code in a frame instead of using OPP approach.
The true reason here is said in the error: Cannot access a property or method of a null object reference.
What this means is that MovieClip(root) is null (it doesn't even bother to check if the function is present). The reasons for this might be three:
Your document class is not MovieClip (could be Sprite).
The class you use root in, is not DisplayObject. The property is part of the DislpayObject class and so if you use it on other kind of classes that does not extend it, it won't work.
You are using this piece of code before you have added the instance to the stage (most probable cause). The root property represents the top-most display object in the tree. If you haven't added the child to the tree, it has no roots :) Check out parent - the error should be the same.
I have a weird problem, and I don't know why this is happening.
I have a movieClip with the name of wellcomeMenu. It is exported for AS with the name of WellcomeMenu, and in the document class I do this:
public var _welcome:WellcomeMenu = new WellcomeMenu();
public function MainTest()
{
_welcome.x = stage.stageWidth * 0.5
_welcome.y = stage.y
addChild(_welcome);
}
All simple stuff. Then I go into the WellcomeMenu movieClip and make a shape with the name Box, then I make it a movieClip too, and give its Instance Name the name specialItem.
To sum up: I dynamically call a wellcomeMenu movieclip, which contains another movieClip with an instance name of specialItem. Then I compile and get this error:
ReferenceError: Error #1056: Cannot create property specialItem on WellcomeMenu.
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at WellcomeMenu()
at MainTest()
what am I doing wrong?
When I remove its instance name, it shows just fine, but I can't manipulate the movieclip within the WellcomeMenu.
I think the problem is in specialItem attribute in the WellcomeMenu class (by the way, you may have an extra l in there if it's English). If the environment is failing to create the attribute, it is probably already there, but with the wrong permission.
If you declared specialItem manually, make sure it's public and not private (public Sprite specialItem) or else the environment won't have permission to set its value.
Another possible issue is that you declared specialItem manually and still enabled the "automatically declare instance", the environment may try to redeclare the attribute and fail. So either remove the manual declaration or disable that option.
The error can happen if you assign an object of one type to another.
var square:Square = new Square();
square.row = 9; //OK, There is a row property in the Square class
var block:Block = new Block();
square = block; //this is not a compiler error, but probably a mistake
square.row = 0; //error if there is no row property on Block
So I got a very basic class
package {
import flash.display.MovieClip;
public class XmlLang extends MovieClip {
public function XmlLang(num:int) {
trace(num);
}
}
}
and an object at frame one:
var teste:XmlLang = new XmlLang(1);
I'm getting this error:
ArgumentError: Error #1063: Argument count mismatch on XmlLang(). Expected 1, got 0
What am I doing wrong?
Thank you very much for you help.
Something is up with your setup. I took your code and implemented it and it worked.
Here's what I did. I created a new test.fla file in AS3 and put the following code on frame 1 - no object on the stage, just code in frame 1.
import XmlLang;
var teste:XmlLang = new XmlLang(1);
stop();
Created a XmlLang.as file, copying your code exactly and saved it in the same folder as the test.fla. Compiled and got a trace of 1
So I'm not exactly sure what's going on. What version of Flash are you running?
Not sure if this was your case, but for future googlers: you get this error message when you're trying to initialize a vector but then forget the new keyword.
So this:
var something:Vector.<Something> = Vector.<Something>();
Will give you an error saying that Something had an argument count mismatch. The correct line is:
var something:Vector.<Something> = new Vector.<Something>();
Difficult error to get at a glance. Took me a few minutes to find it in my code, especially because it doesn't really give you the error line.
I expect you have an instance of XmlLang located on stage, that will be constructed using a constructor with 0 parameters, like an ordinary MovieClip. To check for this, change the constructor header to this:
public function XmlLang(num:int = 0) {
This way, if something will instantiate an XmlLang without a parameter supplied, the new instance will receive a 0 (the default value) as parameter. And then you check your trace output, I am expecting one or more zeroes appear, followed by an 1.
I'm having quite some trouble to try and get an app I wrote in AS2 to AS3. The reason I need to go to AS3 is something icky, so I won't go into detail about it.
I've got 90% of the application running with the new code.
Now I've come to the point where I have to convert this code from AS2,
function setAnimation(theObject,id)
{
theObject.vensterid=id;
theObject.onEnterFrame = function()
{
var myHoriTween:Tween = new Tween (this,"_x",Strong.easeOut,this._x,(130+((theObject.vensterid-frameno)*260)),1,true);
}
}
setAnimation(venster0,0);
, to AS3. My attempt of doing this ended up like
function setAnimation(anObject,id) {
var theObject = this[anObject];
theObject.vensterid=id;
function slideHorizontal(event:Event)
{
var myTween:Tween = new Tween (theObject,"x",Strong.easeOut,this.x,(130+((theObject.vensterid-frameno)*260)),1,true);
}
theObject.addEventListener(Event.ENTER_FRAME,slideHorizontal);
}
setAnimation(venster0,0);
and gives me the following non-error (it doesn't show as a compiler error, but as output):
TypeError: Error #1010: A term is undefined and has no properties.
at sliding_windows_as3_fla::SlideMenu_1/setAnimation()
at sliding_windows_as3_fla::SlideMenu_1/frame1()
I think this is very strange since it doesn't say anything about which term (and there are quite a lot) and googling didn't find me an explanation either.
I didn't get the chance to test your code, because it's difficult to set up a context for it, but my thoughts would be:
You should declare the parameter types: function setAnimation(anObject:Object,id:uint):void. It's at least good practice.
var theObject = this[anObject]; is completely unnecessary if your variable anObject is an object. I think var theObject = this[anObject]; doesn't work, theObject ends up being null and that's why you get your error. If you have declared a variable called venster0, that is the instance of a class that extends Object, then you can pass the reference to it without any other trouble.
Depending on the object you work with, theObject.vensterid=id; might not work. The class that theObject instances must have the 'vensterid' property, or you will get `1119: Access of possibly undefined property vensterid through a reference with static type ...
I think your problem here is following string:
var theObject = this[anObject];
Just replace it with
var theObject = anObject;
I hope that's what you need.
Alternatively instead of
setAnimation(venster0,0);
you could pass an instance name (i.e. String):
setAnimation("venster0",0);
That will work because by this['propertyname'] you are actually accessing Object's property by name.
Just going to throw out that using the built-in Tween classes in Flash/Flex is a pain. Look into using Tweening libraries instead: Tweener, TweenLite, etc. They are much easier to work with, and you don't have to worry about maintaining references until the Tween completes.