ActionScript 3.0 addChild抛出错误

时间:2013-11-26 00:25:43

标签: actionscript-3 actionscript addchild

基本上我的问题是我得到了我的主类......它有一个变量,它是持有我的水平的对象。但是当我尝试将孩子添加到舞台上时我得到一个错误...我之前从未遇到过这个错误,我很困惑。我得到的错误就是这个

ArgumentError:错误#1063:Levels / spawn()上的参数计数不匹配。预计0,得1。     在flash.display :: DisplayObjectContainer / addChild()     在Main()

这是我的主要课程:

package  {

    import flash.display.MovieClip;


    public class Main extends MovieClip {
        public static var game:Levels = new Levels();

        public function Main() {
            game = new Levels();
            addChild(game); //this triggers my error
            game.y = 352;
        }

    }

    }

这里我是我的等级

package {

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


    public class Levels extends MovieClip
    {

        var platforms:Array = new Array();

        var gravity:Number = 0;
        var rPress:Boolean = false;
        var lPress:Boolean = false;

        var falling:Boolean = false;
        var jumping:Boolean = false;

        var velX:Number = 0;
        var velY:Number = 0;

        var a:int = 0;

        public function Levels()
        {
            addEventListener(Event.ADDED_TO_STAGE, spawn);
        }
        function spawn()
        {
            removeEventListener(Event.ADDED_TO_STAGE, spawn);
            getMap();
            this.stage.addEventListener(KeyboardEvent.KEY_DOWN, kPress);
            this.stage.addEventListener(KeyboardEvent.KEY_UP, kReleased);
            this.addEventListener(Event.ENTER_FRAME, loop);
        }
        function getMap()
        {
            platforms.pop();
            for (var a:int = 0; a < numChildren; a ++)
            {
                if (getChildAt(a) is Terrain)
                {
                    platforms.push(getChildAt(a).getRect(this));
                }
            }
        }
        function kPress(event:KeyboardEvent)
        {
            if (event.keyCode == 65)
            {
                lPress = true;
            }
            if (event.keyCode == 68)
            {
                rPress = true;
            }
            if (event.keyCode == 32)
            {
                if (jumping == false)
                {
                    gravity = -25;
                    jumping = true;
                }

            }
        }
        function kReleased(event:KeyboardEvent)
        {
            if (event.keyCode == 65)
            {
                lPress = false;
            }
            if (event.keyCode == 68)
            {
                rPress = false;
            }
        }
        function loop(event:Event)
        {
            velX = 0;

            if (rPress == true)
            {
                velX = 7;
            }
            if (lPress == true)
            {
                velX = -7;
            }
            velY = gravity;
            _player.y +=  velY;
            gravity +=  2;

            for (a = 0; a < platforms.length; a ++)
            {
                if (_player.getRect(this).intersects(platforms[a]))
                {
                    if (velY > 0)
                    {
                        jumping = false;
                        _player.y = platforms[a].top - _player.height;
                        gravity = 0;
                    }
                    else if (velY < 0)
                    {

                        _player.y = platforms[a].bottom;
                        gravity = 0;

                    }
                }
            }
            _player.x +=  velX;
            if (rPress == true)
            {

                if (_player.x + _player.width / 2 > 336 + x * -1 && x + width > 640)
                {

                    x -=  velX;
                }
            }
            else
            {
                if (_player.x + _player.width / 2 > 304 + x * -1 && x < 0)
                {

                    x -=  velX;
                }
            }
            for (a = 0; a < platforms.length; a ++)
            {
                if (_player.getRect(this).intersects(platforms[a]))
                {
                    if (velX == 7)
                    {
                        _player.x = platforms[a].x - _player.width;
                        x +=  velX;
                    }
                    if (velX == -7)
                    {
                           _player.x=platforms[a].x+platforms[a].width;
                        x +=  velX;
                    }
                }

            }
        }
    }
      }

1 个答案:

答案 0 :(得分:1)

您的spawn方法是addedToStage事件的处理程序,因此它希望将事件对象作为参数:

function spawn(event:Event)
{
    removeEventListener(Event.ADDED_TO_STAGE, spawn);
    getMap();
    this.stage.addEventListener(KeyboardEvent.KEY_DOWN, kPress);
    this.stage.addEventListener(KeyboardEvent.KEY_UP, kReleased);
    this.addEventListener(Event.ENTER_FRAME, loop);
}