【问题标题】:Flash AS3:Constructor functions must be instance methodFlash AS3:构造函数必须是实例方法
【发布时间】:2013-02-02 21:24:58
【问题描述】:

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

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

这是我的代码:

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




    }
    }

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

【问题讨论】:

  • 您的问题是什么?你有什么问题?
  • 没有必要在构造函数中检查if (stage == null),因为此时将创建一个新对象,该对象在构建完成之前无法添加到阶段。因此,stage 属性在构造函数中将始终为 null。只需添加听众就足够了。 else-part 永远不会被评估。

标签: actionscript-3 flash hittest


【解决方案1】:
collisionList = new CollisionList(terrain);
terrain.graphics.lineStyle(15);

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

【讨论】:

  • 感谢您的建议,但如果我使用它,我仍然会在第 3 行(我开始导入内容)中遇到相同的错误
  • 哦,是的,还有一个:“collisionList = new CollisionList(terrain);”,也改成 new CollisionList(this)。基本上你只需要使用一个类名作为标识符,如果它有一个静态属性或方法并且来自另一个类,否则它的'this'或类中的任何内容。
猜你喜欢
  • 1970-01-01
  • 2011-07-26
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 2015-12-31
  • 2012-11-26
相关资源
最近更新 更多