Question

I'm trying to mix two tutorials in one game. Level 3 is previously used in a action script file but I transferred it into a normal AS3 timeline.

I get this error:

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

This is the code... sorry if its messy.

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

        }
    }
  }
Was it helpful?

Solution

newObjects does not take any arguments, but it's used as an event listener (which requires it to take the event object).

It should probably look something like function newObjects(event:TimerEvent):void.

OTHER TIPS

A function used as event listener should accept one parameter of type Event, depending on what class of events it listens to. You are listening to an event of a TimerEvent class, so yes, declare parameter as TimerEvent. To add a function that does not need parameters passed to it as an event listener, use default value construction like this:

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

The "=null" addition will allow you to pass no parameters to your function, and the declared parameter will allow you to not get exceptions when it will be called as an event handler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top