Flash AS3:构造函数必须是实例方法

时间:2013-02-02 21:24:59

标签: actionscript-3 flash hittest

大家好,我有一个问题,不知道如何解决它:(有人可以告诉我该怎么做吗?

构造函数必须是实例方法。

所以这是我的代码:

package
{
    import com.coreyoneil.collision.CollisionList;
    import flash.events.Event;
    import flash.display.Sprite;    

    public class terrain extends Sprite
    {
        private var wheel:Ball;
        private var collisionList:CollisionList;
        private var speed:Number;

        private const GRAVITY:Number = .75;
        private const FRICTION:Number = .98;
        private const IMMOVABLE:Number = 10000;


        public function terrain():void
        {
            if(stage == null)
            {
                addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
                addEventListener(Event.REMOVED_FROM_STAGE, clean, false, 0, true);
            }
            else
            {
                init();
            }
        }

        private function init(e:Event = null):void
        {
            collisionList = new CollisionList(terrain);

            wheel = new wheel(10);
            wheel.mass = IMMOVABLE * 2;
            addChild(wheel);
            collisionList.addItem(wheel);
            wheel.x = 30;
            wheel.y = 10;

            speed = 0;

            terrain.graphics.lineStyle(15);

            addEventListener(Event.ENTER_FRAME, updateScene);
        }


        private function updateScene(e:Event):void
        {           
            var collisions:Array = collisionList.checkCollisions();

            if(collisions.length)
            {
                var collision:Object = collisions[0];
                var angle:Number = collision.angle;
                var overlap:int = collision.overlapping.length;

                var sin:Number = Math.sin(angle);
                var cos:Number = Math.cos(angle);

                var vx0:Number = wheel.vx * cos + wheel.vy * sin;
                var vy0:Number = wheel.vy * cos - wheel.vx * sin;

                // Unlike the other examples, here I'm choosing to calculate the amount
                // of bounce based on the objects' masses, with a default mass of 10000 (IMMOVABLE)
                // being used for the drawing the wheel is colliding with.  As such, the only
                // real variable in play here is the current vector of the wheel.
                vx0 = ((wheel.mass - IMMOVABLE) * vx0) / (wheel.mass + IMMOVABLE);
                wheel.vx = vx0 * cos - vy0 * sin;
                wheel.vy = vy0 * cos + vx0 * sin;

                wheel.vx -= cos * overlap /wheel.radius;
                wheel.vy -= sin * overlap / wheel.radius;

                wheel.vx += speed;
            }
            trace("down");
            wheel.vy += GRAVITY;
            wheel.vy *= FRICTION;
            wheel.vx *= FRICTION;

            wheel.x += wheel.vx;
            wheel.y += wheel.vy;

            if(wheel.x > stage.stageWidth) wheel.x = stage.stageWidth;  
            if(wheel.x < 0) wheel.x = 0;                                    
            if(wheel.y > stage.stageHeight - (wheel.height >> 1)) 
            {
                wheel.y = 10;   
                wheel.x = 30;
                wheel.vx = wheel.vy = 0;
            }

        }

        private function clean(e:Event):void
        {
            removeEventListener(Event.ENTER_FRAME, updateScene);
        }




    }
    }

代码中有一些注释。请忽略它我用过的例子。

1 个答案:

答案 0 :(得分:3)

collisionList = new CollisionList(terrain);
terrain.graphics.lineStyle(15);

这是错误1026,如果构造函数是静态的,私有的或者在您的情况下用作标识符,也会引发错误1026。要么使用this.graphics而不是terrain.graphics,要么只使用graphics.etc(删除terrain),并将'this'作为'CollisionList'的参数传递。
(不相关:最好用大写字母“地形”命名类别)