1

我试图在一个游戏中混合两个教程。第 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);

        }
    }
  }
4

2 回答 2

7

newObjects 不接受任何参数,但它用作事件侦听器(这需要它接受事件对象)。

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

于 2012-09-15T16:03:54.513 回答
3

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

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

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

于 2012-09-16T03:30:06.063 回答