AS3 - CS6 - 参数数量不正确

时间:2014-02-04 17:07:43

标签: actionscript-3 arguments flash-cs6

我正在尝试编译我的2D游戏来测试它,我收到了这个错误:\Main.as, Line 32 1136: Incorrect number of arguments. Expected 2.
第32行包含此代码var enemy:Enemy = new Enemy(null);

感谢大家,感谢任何帮助。

与错误相关的代码(如果需要,我可以发布其余代码):

Main.as

package
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

public class Main extends MovieClip
{
    public var player:Player;
    public var enemy:Enemy;

    public var bulletList:Array = [];

    public var mousePressed:Boolean = false; //keeps track of whether the mouse is currently pressed down
    public var delayCounter:int = 0; //this adds delay between the shots
    public var delayMax:int = 7; //change this number to shoot more or less rapidly

    public var enemies:Array =  [];

    public function Main():void
    {
        player = new Player(stage, 320, 240);
        stage.addChild(player);

        //stage.addEventListener(MouseEvent.CLICK, shootBullet, false, 0, true); //remove this
        stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, false, 0, true);
        stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false, 0, true);

        stage.addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
        for(var numBaddies=0; numBaddies<6;numBaddies++){
            var enemy:Enemy = new Enemy(null);
            enemy.x = numBaddies*50;
            enemy.y = numBaddies*50
            stage.addChild(enemy);
            enemies.push(enemy);
        }
    }

    public function loop(e:Event):void
    {
        if(mousePressed) // as long as the mouse is pressed...
        {
            delayCounter++; //increase the delayCounter by 1
            if(delayCounter == delayMax) //if it reaches the max...
            {
                shootBullet(); //shoot a bullet
                delayCounter = 0; //reset the delay counter so there is a pause between bullets

           }
        }

        if(bulletList.length > 0)
        {
            for(var i:int = bulletList.length-1; i >= 0; i--)
            {
                bulletList[i].loop();
            }
        }

        /*for(var h = 0; h<bulletList.length; ++h)
        {
            if(bulletList[h].hitTestObject(this)){
              trace("player hit by baddie " + h);
               }
        }*/

        for(var u:int=0; u<enemies.length; u++) {
        Enemy(enemies[u]).moveTowards(player.x, player.y);
        }
    }


    public function mouseDownHandler(e:MouseEvent):void //add this function
    {
        mousePressed = true; //set mousePressed to true
    }

    public function mouseUpHandler(e:MouseEvent):void //add this function
    {
        mousePressed = false; //reset this to false
    }

    public function shootBullet():void //delete the "e:MouseEvent" parameter
    {
        var bullet:Bullet = new Bullet(stage, player.x, player.y, player.rotation, enemies);
        bullet.addEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved, false, 0, true);
        bulletList.push(bullet);
        stage.addChild(bullet);
    }

    public function bulletRemoved(e:Event):void
    {
        e.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, bulletRemoved);
        bulletList.splice(bulletList.indexOf(e.currentTarget),1);
    }
}
}

Enemy.as

package  {

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

public class Enemy extends MovieClip {

    public var bullets:Array;

    public var stageRef:Stage;

    private var enemyPositionX, enemyPositionY,xDistance,yDistance,myRotation:int;
    public function Enemy(stageRef:Stage, bulletList:Array) {
        // constructor code
        bullets = bulletList;
        this.stageRef = stageRef;
    }

    public function moveTowards(playerX:int, playerY:int){
        xDistance = this.x - playerX;
        yDistance = this.y - playerY;

        myRotation = Math.atan2(yDistance, xDistance);

        this.x -= 3 * Math.cos(myRotation);
        this.y -= 3 * Math.sin(myRotation);


    }

    private function removeSelf():void
    {
        //removeEventListener(Event.ENTER_FRAME, loop);
        if (stageRef.contains(this))
        stageRef.removeChild(this);
    }
}

}

1 个答案:

答案 0 :(得分:2)

如你所说,错误指向:

var enemy:Enemy = new Enemy(null);

Enemy构造函数是:

public function Enemy(stageRef:Stage, bulletList:Array){

所以你缺少第二个参数 - bulletList。

相关问题