ForumsProgramming ForumConfused?

10 2820
alex57834
offline
alex57834
88 posts
445

I have got the error
C:\\Users\\Alex The Best\\Desktop\\Golden Catch\\Classes\\MainGame.as, Line 28 1137: Incorrect number of arguments. Expected no more than 0.

package {

import flash.display.MovieClip;

public class GoldBar extends MovieClip {

public var xSpeed:Number;
public var ySpeed:Number;
public var randomX:Number;


public function GoldBar(startX:Number, startY:Number):void
{

randomX:Number = Math.random()*550;

startX = randomX;
startY = -20;

xSpeed = 0.
ySpeed = 3.
}

public function moveDown():void
{
y = y + ySpeed;
}

}

}

package {

import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class MainGame extends MovieClip {

public var army:Array;
public var goldBar:GoldBar;
public var gameTimer:Timer;

public function MainGame()
{
army = new Array();

gameTimer = new Timer(25);
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer.start();

}

public function onTick(e:TimerEvent):void
{
if( Math.random () < 0.08 )
{
public var newGoldBar = new GoldBar(startX ,startY);
army.push(newGoldBar);
addChild (newGoldBar);

for each (var goldBar:GoldBar in Army)
{
goldBar.moveDown();
}
}
}

}

}

  • 10 Replies
weirdlike
offline
weirdlike
1,309 posts
28,720

well.. I notice a few errors like this line public var newGoldBar = new GoldBar(startX ,startY); in your onTick function shouldn't be public take that out and you declare your public var goldBar:GoldBar; with the name as same but lowercase g in goldBar but the on tick function shows it as being capital G in goldBar. using the same var name but with one difference like a capital versus non capital can be too confusing

and your for each (newGoldBar in army ) you were creating a in army "case sensitive"


a little confusing maybe but try looking at this in your main package

private var gb:GoldBar //gb instead of goldBar

and your onTick
if ( Math.random() < 0.08 )
{
//cut out the randomX function in your gold bar package and just
//added it here because it represents the class and is
//not a function
var randomX:Number = Math.random() * 500;

var gb = new GoldBar(randomX, -20);

army.push(gb);
addChild(gb);

}
for each (gb in army) //lookie change here
{
//Leave the move down function as it is a real function
gb.moveDown();
}

in your gold bar package change this public var ySpeed:Number;
to this public var ySpeed:Number=3;

weirdlike
offline
weirdlike
1,309 posts
28,720

more specifically the error you are getting is from the line

public var newGoldBar = new GoldBar(startX ,startY);

startX and startY

flash doesn't know what it is

alex57834
offline
alex57834
88 posts
445

I changed everything and I still have the same error

package {

import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class MainGame extends MovieClip {

public var army:Array;
public var gb:GoldBar;
public var gameTimer:Timer;

public function MainGame()
{
army = new Array();

gameTimer = new Timer(25);
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer.start();

}

public function onTick(e:TimerEvent):void
{
if( Math.random () < 0.08 )
{

var randomX:Number = Math.random() * 500;
var gb = new GoldBar(randomX , -20);
army.push(newGoldBar);
addChild (newGoldBar);

for each (var gb in Army)
{
gb.moveDown();
}
}
}

}

}

package {

import flash.display.MovieClip;

public class GoldBar extends MovieClip {

public var xSpeed:Number = 0;
public var ySpeed:Number = 3;

public function GoldBar()
{

}

public function moveDown():void
{
y = y + ySpeed;
}

}

}

alex57834
offline
alex57834
88 posts
445

edit: army.push(gb);
addChild (gb);

weirdlike
offline
weirdlike
1,309 posts
28,720

I guess I should have ran the code before replying and was assuming other factors were at play here

use same code as above and change onTick like so

var randomX:Number = Math.random() * 500;
var gb = new GoldBar();
gb.x=randomX;
gb.y=-20;
army.push(gb);
addChild (gb);

for each (gb in army)
{
gb.moveDown();
}

weirdlike
offline
weirdlike
1,309 posts
28,720

after playing around with the code a little I think I am starting to understand what you are trying to achieve

1137: Incorrect number of arguments. Expected no more than 0.

there was actually nothing there telling what the x and y coordinates were so you need to tell it where it is by adding the coordinate in the main GoldBar constructor

//MainGame
var gb = new GoldBar(randomX, -20);

//GoldBar
public function GoldBar(startX:Number, startY:Number)
{
x = startX;
y = startY;
}

alex57834
offline
alex57834
88 posts
445

You have helped loads, I got another error relating to arguments and fixed it(because of your teaching) but this is confusing me

I deleted the public var but that didn't help.

C:\\Users\\Alex The Best\\Desktop\\Golden Catch\\Classes\\MainGame.as, Line 27 Warning: 3596: Duplicate variable definition.

package {

import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class MainGame extends MovieClip {

public var army:Array;
public var gameTimer:Timer;

public function MainGame()
{
army = new Array();

gameTimer = new Timer(25);
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer.start();

}

public function onTick(event:TimerEvent):void
{
if( Math.random () < 0.08 )
{
var randomX:Number = Math.random()*550;
var gb = new GoldBar();
gb.x = randomX;
gb.y = -20;
army.push(gb);
addChild(gb);
}

for each (var gb:GoldBar in army)
{
gb.moveDown();
}
}

}

}

package {

import flash.display.MovieClip;
import flash.events.Event;

public class GoldBar extends MovieClip {

public function GoldBar()
{

}

public function moveDown():void
{
y = y + 2;
}

}

}

weirdlike
offline
weirdlike
1,309 posts
28,720

I am thinking it is because the variable is declared inside the function so other functions actually don't know what it is and that is why it must be declared outside the function

put this back
public var army:Array;
public var gb:GoldBar; //declare it here
public var gameTimer:Timer;

and call to it
public function onTick(event:TimerEvent):void
{
if( Math.random () < 0.08 )
{
var randomX:Number = Math.random()*550;
gb = new GoldBar(); // see the declared variable from above
gb.x = randomX;
gb.y = -20;
army.push(gb);
addChild(gb);
}

for each (gb in army) //it can see it here too
{
gb.moveDown();
}
}

alex57834
offline
alex57834
88 posts
445

Thanks you have really helped and it's quite easy to understand the way you explain it. I'll send a message to show you game when it's done

weirdlike
offline
weirdlike
1,309 posts
28,720

cool I cant wait to see it. another cool piece of information that I use while coding

Instance names, some coding, and other misc lines wont work with spaces so you are forced to combine 2 words, the best thing to do is all lowercase on the first word then one capital starting the second word etc.

for example:
GoldBar- is typed out as goldBar
or
NewGoldBar- is typed out as newGoldBar

the reason is so you can easily distinguish between the words, but as far as coding goes, it really doesn't matter, I try to do this with everything even class names.

I also took the liberty of playing around with your code a little bit and made a game of my own using old projects and slapping them together

link

//code preview
if ( Math.random() < 0.03 )
{
var randomX:Number = Math.random() * 500;
var e = new enemy(randomX, -55);

army.push(e);
addChild(e);

e.addEventListener(navigation.KILL, dead);
e.addEventListener(navigation.POINT, onePoint);
e.addEventListener(Event.ENTER_FRAME, hit);

if (e.x

Showing 1-10 of 10