簡體   English   中英

Flash AS3:構造函數必須是實例方法

[英]Flash AS3:Constructor functions must be instance method

大家好,我有一個問題,不知道如何解決:(有人可以告訴我該怎么做嗎?

構造函數必須是實例方法。

所以這是我的代碼:

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




    }
    }

在代碼中有一些注釋。請忽略它,我已使用示例。

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

這是錯誤1026,如果構造函數是靜態的,私有的,或者在您使用的情況下用作標識符的情況下,也會引發錯誤1026。 要么使用this.graphics代替terrain.graphics,要么僅使用graphics.etc(除去地形),然后將“ this”作為“ CollisionList”的參數傳遞。
(不相關:最好以大寫“ Terrain”開頭的類命名)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM