as3 - Constructor Error - actionscript-3

I've got (to me) a strange error. As far as I can tell I'm doing everything correct, however I keep getting the error bellow.
ArgumentError: Error #1063: Argument count mismatch on Enemy().
Expected 2, got 0.
As you can see, when initializing my class Enemy it expects two arguments and gets one. However, as far as I can tell it's not the case.
Where it is called.
function startHandle(evt:MouseEvent):void{
enemy = new Enemy(1090, 189);
gotoAndStop(2);
Player.stop();
currentLevel = 1;
}
Then the Enemy class
public function Enemy(xLocation:int, yLocation:int){
trace(xLocation);
trace(yLocation);
// constructor code
x = xLocation;
y = yLocation;
trace(x);
trace(y);
}
The output I get from this is as follows.
1090
189
1090
189
ArgumentError: Error #1063: Argument count mismatch on
Enemy(). Expected 2, got 0.
at flash.display::MovieClip/gotoAndStop()
at
Project_fla::MainTimeline/startHandle()
As far as I can tell it gets the two values, knows it has them, sets them. But still gives an error. Anyone got an idea?

Most likely you have a pre-placed enemy on some frame. Since the default constructor for any DisplayObject descendant wants 0 arguments, anything created in Flash GUI makes Flash compiler to make a constructor call with 0 arguments. To circumvent this (and find that pesky enemy that throws you up) give default values for the constructor like this:
public function Enemy(xLocation:int=0, yLocation:int=0){
And watch when an enemy will appear at (0,0), debug that point and eliminate creation of any unneeded enemy instances.

Related

How to refer to a single frame in a an object class?

So, I have my character movieclip on frame1 of my timeline. When it hits an enemy the movie goes on frame2 where I have a game-over screen. However I get this error at runtime when the character hits the enemy:
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at Coinsy/update()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Untitled_2_fla::MainTimeline/CharMovement()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Untitled_2_fla::MainTimeline/ResetBox()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Untitled_2_fla::MainTimeline/EnemyBall()
I debugged and it seems that the problem is in my coins class:
package
{
import flash.display.*;
import flash.events.*;
public class Coinsy extends MovieClip
{
var char:MovieClip;
var MTL = MovieClip(root);
public function Coinsy()
{
this.addEventListener(Event.ENTER_FRAME, update);
}
function update(event:Event):void
{
char = MTL.char1;
// CC = int(root).CC;
if(hitTestObject(char))
{
this.removeEventListener(Event.ENTER_FRAME, update);
parent.removeChild(this);
MTL.CC++;
}
}
}
}
at char = MTL.char1;, apparently I can't use MovieClip(root) when I have more then one frame.
char1 is my character's instance name andCC is the coin count var.
I'm pretty new at ActionScript and I'd like to know where i did wrong. THANKS!
The error is actually happening on this line
if(hitTestObject(char))
What's happening is the game is still trying to check for collisions on the game-over screen. The char1 object couldn't be found, so it became null. Null essentially means something's empty, so you can't do anything with it. If you ever try null.something you'll get an error, and someFunction(null) will quite likely cause an error too.
A quick fix is changing the line to
if(char != null && hitTestObject(char))
as Karma mentioned.
However, the following three TypeErrors are unrelated to the code you posted. They're happening because some line of code is trying to do null.something. The CharMovement, ResetBox and EnemyBall functions are probably being called when they shouldn't be. You might need to remove event listeners when game-over happens, or add extra logic to check for null values.

Strange issues when using addChild and hitTest with AS3

I am having a couple of problems when adding a child in action script 3. I am currently building a Space Invaders game and I am writing the function that adds the asteroids to the stage.
My first problem is that the all previous asteroids are being added each time I try to add a new asteroid.
My second issue is when I add the hitTestOject function. It throws up an error and it doesn't do anything when the space ship hits the asteroid object.
Here is the error I receive with the hitTestObject:
TypeError: Error #1034: Type Coercion failed: cannot convert "ast_0"
to flash.display.DisplayObject. at
spaceranger_fla::MainTimeline/addAstroid() at
flash.utils::Timer/_timerDispatch() at flash.utils::Timer/tick()
And here is my code. I use a timer so each asteroid is added every 5000ms:
// Add astoid
var astTimer:Timer = new Timer(5000);
astTimer.addEventListener(TimerEvent.TIMER, addAstroid);
var i:Number = 0;
function addAstroid (e:TimerEvent):void{
var ast = new astroid();
ast.name = "ast_"+i;
ast.y = Math.random()*stage.stageHeight;
ast.x = 565;
addChild(ast);
trace(i);
if(ship.hitTestObject(ast.name)){
gotoAndStop("2");
}
i = i+1;
}
astTimer.start();
Some advice, recommendations and answers will be greatly appreciated :)
UPDATE
I sorted the looping error. Old asteroids no longer appear again! :D
Many Thanks,
Peter Stuart
Per your first problem, it does not appear i increments - it's always 0.
When you assign name, increment i:
ast.name = "ast_" + (i++).toString();
Basically, saying i = i + 1;
Next up, hit test against the instance itself, not an identity:
ship.hitTestObject(ast)
Not sure how your game play works, but it would seem what you really want are two handlers:
one to occasionally add a new asteroid
one that tests for collisions
Currently your addAsteroid() function adds a new asteroid and immediately tests if it collides with the ship upon creation. That asteroid will never be tested for collision again. If this is similar to a classic asteroids game, you may want to push each asteroid to an array, and add an event listener for ENTER_FRAME to test each asteroid for collision against the ship.
ship.hitTestObject(ast.name) is not going to work because ast.name is a String, not a DisplayObject.
Try this :
if(ship.hitTestObject(ast)){
gotoAndStop("2");
}

AS3 tweenlight loop stops

I have a loop animation that stops and gives me an argument error. I've redone the coding a couple of different ways but to no avail. Here is my code:
contactbox.addEventListener(MouseEvent.MOUSE_OVER, Scroll);
function Scroll(evt:MouseEvent){
TweenLite.to(
btnwave, 2, {
x:-115.5, ease:Linear.easeNone, overwrite:true, onComplete:Switch});
}
function Switch(){
TweenLite.to(
btnwave, 0, {
x:184.6, ease:Linear.easeNone, overwrite:true, onComplete:Scroll});
}
And here is the error it gives me:
ArgumentError: Error #1063: Argument count mismatch on Main/Scroll(). Expected 1, got 0.
at Function/http://adobe.com/AS3/2006/builtin::apply()
at com.greensock.core::TweenCore/complete()
at com.greensock::TweenLite/renderTime()
at com.greensock::TweenLite()
at com.greensock::TweenLite$/to()
at Main/Switch()
at Function/http://adobe.com/AS3/2006/builtin::apply()
at com.greensock.core::TweenCore/complete()
at com.greensock::TweenLite/renderTime()
at com.greensock.core::SimpleTimeline/renderTime()
at com.greensock::TweenLite$/updateAll()
I'm trying to brush up on my tweenlite skills for some upcoming work. Any help would be appreciated.
You're getting an error because TweenLite isn't passing a MouseEvent instance to Scroll(). Scroll() currently requires that a MouseEvent Object be passed to it since it's an event handler. You can fix this by making Scrolls first argument optional like this:
function Scroll(evt:MouseEvent=null){
This way, when TweenLite calls Scroll() the MouseEvent will just default to null.

AS3 Argument Error #1063 ... expected 1 got 0

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.

MovieClip extension

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);