user1546057
user1546057

Reputation: 5

What to import in AS3 to allow "addEventListener"?

This is an extremely basic question (I'm sort of new to AS3) but what do you import in AS3 to allow "addEventListener"? I made the mistake of learning how to program in the timeline and now this jump over is kind of tricky.

EDIT: Here is my code (there are countless errors, but I just want to fix the addEventListener ones):

package 
{
    import flash.display.*;
    import flash.events.*;
import flash.utils.*;

public class TheGameItself extends MovieClip
{

    public static function startit(stage)
    {
        addEventListener(Event.ENTER_FRAME, movecat);
        addEventListener(Event.ENTER_FRAME, spawnanimal);
        var myDelay:Timer = new Timer(100);
        myDelay.addEventListener(TimerEvent.TIMER, showMessage);
        myDelay.start();
        var myMouse = 0;
        var speed = 1;
        var wave = 1;
        var score = 0;
        var rnum = 0;
        var moveornot = false;
    }
    function showMessage(event:TimerEvent):void
    {
        moveornot = true;
    }

    function movecat(event)
    {
        if ((mouseY > 40) && (mouseY < 330))
        {
            cat.y = mouseY;
        }
    }
    function spawnanimal(event)
    {
        var rnum = Math.floor(Math.random() * ((100 / wave) - 1 + 1)) + 1;
        if (rnum == 5)
        {
            myMouse = new mouse();
            myMouse.x = 0;
            myMouse.y = Math.floor(Math.random() * (350 - 45 + 1)) + 45;
            addChild(myMouse);
            for (var i:int = 0; i < 10000; i++)
            {
                if (moveornot == true)
                {
                    myMouse[i].y +=  speed * 100;
                    moveornot == false;
                    if ((myMouse.x == 500) || (myMouse.hitTestObject(cat)))
                    {
                        myMouse.visible = false;
                    }
                }
            }
        }
    }

}

}

Upvotes: 0

Views: 4000

Answers (5)

The_asMan
The_asMan

Reputation: 6402

You can not access an instance of an object through a static function.
in the following code:

public static function startit(stage)
{
  addEventListener(Event.ENTER_FRAME, movecat);
  addEventListener(Event.ENTER_FRAME, spawnanimal);
  var myDelay:Timer = new Timer(100);
}

by doing:

addEventListener(Event.ENTER_FRAME, movecat);

you are essentially doing:

this.addEventListener(Event.ENTER_FRAME, movecat);

which is saying add the listener to the current instantiation, but you declared the function as static, which won't allow you to do it.

Do away with the static reference to solve your problem.

Upvotes: 0

divillysausages
divillysausages

Reputation: 8033

As TheGameItself extends MovieClip, you don't need to include anything - MovieClip already extends EventDispatcher. Your problem here is that startit() is a static function, while addEventListener() is an object function.

Remove the static from startIt(), create your instance of TheGameItself, and it should be fine.

E.g. (in your main class):

var game:TheGameItself = new TheGameItself; // hold a reference to this somewhere, otherwise it'll be gc'd
game.startIt();

On a somewhat related note, myMouse, score etc will all be local variables to the startIt() function, so they won't exist when the function exits

Upvotes: 0

Moorthy
Moorthy

Reputation: 754

You need to import the API of the event about to use. If you are going to use an EnterFrame event then you should import flash.Events.Event. Because EnterFrame event coded in the Event API. For more details visit ADOBE Documentation about Events.

If you want to initialize once it is added to stage, you can avail this

 this.addEventListener(Event.ADDED_TO_STAGE, onAdded);

Also in you code you were declared some variables inside the static function. You will get error when you tried to access these variables outside that function. If you want to have it common for all functions then declare it as global variable.

I am not sure why you used static function. If possible, try to avail the above said listener to avoid a static function call in this case.

Upvotes: 0

Peter Hall
Peter Hall

Reputation: 58725

To create a class that can dispatch events, then extend flash.events.EventDispatcher. That will provide the addEventListener and dispatchEvent methods on objects that are instances of that class. For objects that listen to the events, you don't need to import anything.

in com/foo/MyClass.as:

package com.foo {
   import flash.events.EventDispatcher;

   public class MyClass extends EventDispatcher
   {

   }

   public function doSomething():void
   {
       dispatchEvent( new Event("eventType"));
   } 
}

elsewhere:

import com.foo.MyClass;

var obj:MyClass = new MyClass();
obj.addEventListener("eventType", onEvent);

obj.doSomething();

function onEvent(event:Event):void
{
    trace("the event happened");
}

Upvotes: 1

K Z
K Z

Reputation: 30453

You need:

import flash.events.EventDispatcher;

See reference.

For eg., if you extend EventDispatcher like this:

public class MyClass extends EventDispatcher {
...
}

Then you can use addEventListener() on any instance of MyClass class.

Upvotes: 0

Related Questions