【问题标题】:Bubble pop game Actionscript: defining ToString() method through a reference with a static type class泡泡游戏Actionscript:通过静态类型类的引用定义ToString()方法
【发布时间】:2012-04-17 15:35:08
【问题描述】:

您好,我这里有一个泡泡爆破游戏的动作脚本代码。游戏开始时,气泡会从游戏顶部落到底部,并在 30 秒内点击或弹出气泡最多的分数保留。

气泡的大小由代码中的initialize方法定义,使用ToString()计算分数。 ToString 给了我一个 1061: Call to a possible undefined method Random through a reference with static type uint。这个错误我无法弄清楚它来自哪里。

如果任何有 AS3 经验的人可以指出我的错误,我将不胜感激。谢谢。

package  {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.media.Sound;

    public class Ball extends MovieClip{

        static public var burstCounter: uint;
        private var vx: Number;
        private var vy: Number;
        private var gravity: Number;
        private var stageWidth;
        private var stageHeight;
        private var bubble:Ball = new Ball();
        private var score: uint=0;

        public function Ball() {
            bubble.addEventListener(Event.ADDED_TO_STAGE, initialize)
            bubble.addEventListener(MouseEvent.CLICK, burst)
            bubble.addEventListener(Event.ENTER_FRAME, dropping)
        }

        public function initialize (e:Event):void
        {
            bubble.x = Math.random() * stageWidth;
            bubble.y = 0;

            stageWidth = stage.stageWidth;
            stageHeight = stage.stageHeight;

            bubble.vx = Math.random() * 2 - 1;
            bubble.vy = Math.random() * 2 + 1;
            gravity = 0.1;

            var sizeScale = Math.random() * 1.2 + .6;
            bubble.scaleX = bubble.scaleY = sizeScale;
            score = (10 / sizeScale);
            scoreValue.text = score.ToString();

            var colorTran = new ColorTransform();
            colorTran.color = Math.random() * 0xFFFFFF;
            transform.colorTransform = colorTran;
            addChild(bubble);
        }
        function dropping(e: Event) :void
        {
            x += vx;
            y += vy;
            vy += gravity;

            if((x<0) || (x>stageWidth) || (y<0) || (y>stageHeight))
            {
                if(parent != null)
                {
                    parent.removeChild(this);
                }
                removeEventListener(Event.ENTER_FRAME, dropping)
            }
        }
        function burst (e:Event):void
        {
            var ballonPopping: Sound = new BalloonPopping();
            bubble.removeEventListener(Event.ADDED_TO_STAGE, initialize);
            bubble.removeEventListener(Event.ENTER_FRAME, dropping);
            removeChild(bubble);

            ballonPopping.play();

            burstCounter += score;
        }
    }

}

【问题讨论】:

    标签: actionscript-3 flash math random


    【解决方案1】:

    请注意,在命名和调用属性或方法时,大小写确实很重要

    ActionScript 对其成员使用pascalCasing,包括static 成员。常量使用ALL_CAPS

    我想不出 ActionScript 中有任何类成员像您一直在做的那样使用 CamelCasing - 据我所知,没有。

    【讨论】:

      【解决方案2】:

      你需要改变每次出现的

      Math.Random()
      

      Math.random()
      

      注意小写的r

      或者...因为您似乎刚刚为 ToString() 编辑了这个问题

      试试

      toString()
      

      请注意错误的相似程度。你要调用一个在这两个上都不存在的方法。大小写很重要 VAR 与 var 不同

      【讨论】:

      • 总是更喜欢toString而不是ToString,因为当你写trace(myObj)时,toString()会被自动调用。您可以使用它来获取有关您的对象的有用信息,而不是在“[Object object]”处出现或不得不在任何地方调用trace(myObj.ToString())
      【解决方案3】:

      uint 也没有 ToString() 方法,但它有一个 toString()。

      大小写在方法名称中很重要。 :)

      【讨论】:

        【解决方案4】:

        改用Math.random()...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多