调用可能未定义的方法

时间:2014-07-12 07:14:00

标签: actionscript-3 flash flash-builder

我从tut那里得到了as3代码。 我收到了错误

调用可能未定义的方法图标 代码:

package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;

[SWF(width='800',height='600',backgroundColor='#FFFFFF',frameRate='25')]

public class Test1 extends MovieClip
{

    var icon:FacebookIcon = new FacebookIcon();
    var background:BG = new BG();

     private var timer:Timer = new Timer(5000,-1);
     private var bubbles:Array = [];
     private var score:int;
     private var textBox:TextField = new TextField;
     private var textFormat:TextFormat = new TextFormat(null, 30);

    public function Test1():void
    {   

        if(stage) init();
    else { addEventListener(Event.ADDED_TO_STAGE, init); }

    }
    private function init(Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point

        //add start button
        icon.addChild(new icon());
        icon.addEventListener(MouseEvent.CLICK, startGame);
        icon.buttonMode = true;
        icon.x = (stage.stageWidth / 2) - (icon.width / 2);
        icon.y = (stage.stageHeight / 2) - (icon.height / 2);
        addChild(icon);


        textBox.defaultTextFormat = textFormat; 

    }

    private function balloonCheck(e:Event):void {
        if (bubbles.length)
        {
            for (var i:int = 0; i < bubbles.length; i++)
            {
                if (bubbles[i].y == 0 - bubbles[i].height)
                {
                    removeEventListener(Event.ENTER_FRAME, balloonCheck);
                    for (var j:int = 0; j < bubbles.length; j++)
                    {
                        bubbles[j].die();
                        removeChild(bubbles[j]);
                    }
                    timer.stop();
                    textBox.text = "You popped " + score + " balloons!\nWell Done!";
                    textBox.width = textBox.textWidth;
                    textBox.x = (stage.stageWidth / 2) - (textBox.width / 2);
                    textBox.y = (stage.stageHeight / 4) - (textBox.height / 2);
                    addChild(textBox);

                    icon.addEventListener(MouseEvent.CLICK, restartGame);
                    addChild(icon);
                    return;
                }
            }
        }
    }
        private function startGame(e:MouseEvent):void {
                 icon.removeEventListener(MouseEvent.CLICK, startGame);
                 removeChild(icon);
                 removeChild(background);
                 timer.addEventListener(TimerEvent.TIMER_COMPLETE, createBubble);
                 timer.start();
                 createBubble();
                 score = 0;
        }
         private function createBubble(e:TimerEvent = null):void {
                          var bubble:Bubble = new Bubble();
                          Bubble.addEventListener(MouseEvent.CLICK, popBubble);
                          Bubble.y = stage.stageHeight;
                          Bubble.x = Math.floor(Math.random() * (stage.stageWidth - Bubble.width));
                          Bubbles.push(Bubble);
                          addChild(Bubble);
                          timer.reset();
                          timer.start();

                 }

       private function popBubble(e:MouseEvent):void {
                        e.target.x = Math.floor(Math.random() * (stage.stageWidth - e.target.width));
                        e.target.reset();
                 }
        }

}

我很抱歉凌乱的代码。错误是指

icon.addChild(new icon());

和行

Bubble.addEventListener(MouseEvent.CLICK, popBubble);

其他错误:

通过静态类型Class

的引用访问可能未定义的属性y

将类型Class的值隐式强制转换为不相关的类型flash.display:DisplayObject

通过静态类型Class

的引用调用可能未定义的方法addEventListener

永远感谢那些帮助我的人。谢谢!

1 个答案:

答案 0 :(得分:0)

您使用了类Bubble的名称而不是变量bubble的名称。

    var bubble:Bubble = new Bubble();
    bubble.addEventListener(MouseEvent.CLICK, popbubble);
    bubble.y = stage.stageHeight;
    bubble.x = Math.floor(Math.random() * (stage.stageWidth - bubble.width));
    bubbles.push(bubble);
    addChild(bubble);

[UPDATE] 关于第二个问题:

new icon()

关键字new应该在类名之前。类名以大写字母开头,如FacebookIcon。 您确定需要添加图标作为图标对象的子图标,该图标对象已经是图标吗?我想你应该删除这一行。