0

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

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

所以这是我的代码:

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




    }
    }

代码中有一些注释..请忽略它,我使用了示例。

4

1 回答 1

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

这是错误 1026,如果构造函数是静态的、私有的或在您的情况下用作标识符,也会引发错误。使用 this.graphics 代替 terrain.graphics 或仅使用 graphics.etc(删除地形),并将“this”作为“CollisionList”的参数传递。
(无关:最好以大写“Terrain”开头的类命名)

于 2013-02-02T22:14:17.273 回答