闪存错误#1063参数计数不匹配AS3

时间:2012-09-15 15:25:55

标签: actionscript-3 flash adobe-flash-cs3

我正在尝试在一个游戏中混合两个教程。级别3以前在动作脚本文件中使用,但我将其转移到正常的AS3时间轴。

我收到此错误:

ArgumentError: Error #1063: Argument count mismatch on adventure_fla::MainTimeline/newObjects(). Expected 0, got 1.
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.utils::Timer/flash.utils:Timer::tick()

这是代码...对不起,如果它凌乱。

    const speed:Number = 5.0;
    var nextObject:Timer;
 // array of objects
    var objects:Array = new Array();

function initGame():void{
player.x=200;
player.y=400;
stage.addEventListener(MouseEvent.MOUSE_MOVE,movePlayer);
Mouse.hide();
player.gotoAndStop("arrow");
setGameTimer();
newObjects();
addEventListener(Event.ENTER_FRAME, moveObject);
}

    function movePlayer(e:MouseEvent):void{

    player.x=mouseX;
    e.updateAfterEvent();}

    function setGameTimer():void {
trace("GAME TIMER STARTED");
var gameTimer:Timer = new Timer(40000, 1);
gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, endGame);
gameTimer.start()
    }

    function endGame(e:Event):void {
    trace("Game Over");
    // remove all objects
    for (var i:int=objects.length-1; i>=0; i--) {
        removeChild(objects[i]);
        objects.splice(i, 1);
    }  }

    function setNextObject():void {
nextObject = new Timer(1000, 1);
nextObject.addEventListener(TimerEvent.TIMER_COMPLETE, newObjects);
nextObject.start();

    function newObjects():void{
//create next object
        // array of good and bad objects
        var badObjects:Array = ["Bad_1", "Bad_2"]
        var goodObjects:Array = ["Good_1", "Good_2"]
        // create random number
        if (Math.random() < .5 ) {
            //based of treat object length
            var r:int = Math.floor(Math.random()*goodObjects.length);
            // get the treat object by name and cast it as its own class in a temporary variable
            var classRef:Class = getDefinitionByName(goodObjects[r]) as Class;
            // now we can make a new version of the class
            var newObjects:MovieClip = new classRef();
            // dynamic var (because mc is an object) typestr it as good
            newObjects.typestr = "good";
        } else {
            //  for bad same as above
            r = Math.floor(Math.random()*badObjects.length);
            var classRef:Class = getDefinitionByName(badObjects[r]) as Class;
            var newObjects:MovieClip = new classRef();
            // typestr it bad
            newObjects.typestr = "bad";
        }
        // random pos
        newObjects.x = Math.random()* 500;
        newObjects.scaleX = newObjects.scaleY = .4;
        addChild(newObjects);
        // push it to array
        objects.push(newObjects);
        // create another one
        setNextObject();

    }

     function moveObject(e:Event):void {
    // cycle thru objects using a for loop
    for (var i:int=objects.length-1; i>=0; i--) {
        //move objects down based on speed
        objects[i].y += speed;
        objects[i].rotation += speed;
        // if objects leaves the stage
        if (objects[i].y > 400) {

            removeChild(objects[i]);

            objects.splice(i, 1);

        }
    }
  }

2 个答案:

答案 0 :(得分:5)

newObjects不接受任何参数,但它被用作事件监听器(需要它来获取事件对象)。

它应该看起来像function newObjects(event:TimerEvent):void

答案 1 :(得分:3)

用作事件侦听器的函数应该接受一个Event类型的参数,具体取决于它侦听的事件类。您正在侦听TimerEvent类的事件,所以是的,将参数声明为TimerEvent。要添加不需要作为事件侦听器传递给它的参数的函数,请使用默认值构造,如下所示:

function newObjects(event:TimerEvent=null):void {...}

“= null”添加将允许您不向您的函数传递任何参数,并且声明的参数将允许您在将其作为事件处理程序调用时不会获得异常。