0
votes

as3 code I took from tut. I get the error

Call to a possibly undefined method icon the code:

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

}

Im sorry for the messy code. the error is refers to

icon.addChild(new icon());

and the line

Bubble.addEventListener(MouseEvent.CLICK, popBubble);

other errors:

Access of possibly undefined property y through a reference with static type Class

Implicit coercion of a value of type Class to an unrelated type flash.display:DisplayObject

Call to a possibly undefined method addEventListener through a reference with static type Class

Eternal gratitude to those who help me.Thanks!

1

1 Answers

0
votes

You used the name of the class Bubble instead of the name of the variable 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] About the second issue:

new icon()

You the keyword new should precede a class name. Class names start with an uppercase letter, like FacebookIcon. Are you sure you need to add an icon as a child of the icon object, which already is an icon? I guess you should just remove this line.