PDA

View Full Version : Events and classes


Incrue
07-12-2006, 04:47 PM
I want to make an object that, when i create an instance or it, the instance will receive the ENTER_FRAME event and do something
I want to do the -addEventListener- stuff inside the constructor of my object, so i can easily go

myst:mystuff = new mystuff();

and thats enough for him to start making things each frame
BUTT my code is not working

package{
class mystuff{
function mystuff(){
this.addEventListener(Event.ENTER_FRAME, this.EnterFrame);
}
function EnterFrame(event:Event):void {
trace("something");
}

}
}

What am i doint wrong?

senocular
07-12-2006, 04:57 PM
A) to have access to addEventListener (etc) your class needs to inherit from flash.events.EventDispatcher. That will give you access to those methods. B) to recieve enter frame events, your class needs to be a subclass of DisplayObject (Note you cannot extend this class directly, you'd need to extend a subclass). Alternatively, you can create an instance in your class that receives enter frame event and use that.

Incrue
07-12-2006, 05:05 PM
So, there is no equivalent to the onEnterFrameBeacon?I mean, isnt possible to make a single object to receive ENTER_FRAME events the way onEnterFrameBeacon did in AS2?
I wonder if i am not wasting processing power creating a Sprite to do something a single object could do before...

senocular
07-12-2006, 05:07 PM
OnEnterFrameBeacon was just another class. You can always make your own that does the same (there might be one in the flex mx framework)... and theres no difference in processing power. An event is an event no matter where its coming from.

Incrue
07-12-2006, 05:14 PM
Thanks Sen!
By processing power i mean:I want to create dozens of this object, So if they have to be Sprites i worry if they are not wasting memory with the things Sprites have and a single object dont have
But i think i will do this way anyway, cos i have no idea about how create an onEnterFrameBeacon equivalent in AS3

Incrue
07-12-2006, 05:26 PM
Sprites inherit from EventDisplatcher, so if my class extends Sprites, addEventListener will work on it, rigth?
Now this stuff is extending Sprite but is not working...whats my mistake now?

package{
import flash.display.Sprite;
class vbt extends Sprite {
function vbt(){
this.addEventListener(Event.ENTER_FRAME, this.EnterFrame);
}
public function EnterFrame(event:Event):void {
trace("something");
}
}
}

senocular
07-12-2006, 05:31 PM
theres nothing wrong with that class (aside from not importing Event). As long as you instantiate a vbt instance (and its defined to a persistent variable), the enter frame event handler should run.

Incrue
07-12-2006, 05:39 PM
I added this line

import flash.events.Event;

But its still giving the folowing error:
ReferenceError: Error #1074: Illegal write to read-only property vbt on global.
at Timeline0_b5a4c63c8ae66a45b9f7bf9e59d6fa94/::frame1()
something
something
something
something
something

In the main timeline i just did

sat:vbt = new vbt();

senocular
07-12-2006, 05:49 PM
use var

Incrue
07-12-2006, 05:59 PM
I cant belive...shame on me SHAMEEEEEEE
Thanks again Sen!!!!
now taht i am happy mystuff is working i can relax and watch the most beautiful thing on earth
http://www.youtube.com/watch?v=G2uo8zvaEg4&search=Alizee

senocular
07-12-2006, 06:07 PM
welcome ;)

Tink
07-12-2006, 07:25 PM
You could pass a reference to stage, through to you class and add the listener to that. Then you wouldn't need to extend a DisplayObject.

senocular
07-12-2006, 07:38 PM
stage... or any other display object such as root (for enter frame). Though, it should be noted that for global mouse clicks (equiv to onMouseDown and onMouseUp) and mouse moves, etc, the stage would be needed.

Incrue
07-16-2006, 11:09 AM
I am not sure if i understand
You mean, like this:

import flash.events.Event;
class mystuff{
function mystuff(){
Stage.addEventListener(Event.ENTER_FRAME, this.EnterFrame);
}
?
I am geting this error:

ReferenceError: Error #1065: Variable Stage is not defined.
at ::mystuff$iinit()
at Timeline0_406d341c97f0484d90deff7884665fd4/::frame1()

Incrue
07-16-2006, 11:13 AM
I dont like this new event thing.
He also dont like me.

Tink
07-16-2006, 11:34 AM
The reason your getting this error is that your clip only gets a stage property when it has been added to the a displayList. Obviously it would be impossible to add it to a displayList before the constructor has ran or if you object isn't a DisplayObject.

Therefore you will need to pass a valid stage reference in the constructor. Lets say your class is created on the _root, you could usepackage myPackage
{

import flash.display.DisplayObject;
import flash.events.Event;

class MyClass
{
public function MyClass( displayObject:DisplayObject )
{
displayObject.addEventListener( Event.ENTER_FRAME, onEnterFrame );
}

private function onEnterFrame( event:Event ):void
{
trace( "onEnterFrame" );
}

}

}the where is created on your _rootimport myPackage.MyClass;

var myClass:MyClass = new MyClass( stage );or you could use
myPackage.MyClass;

var myClass:MyClass = new MyClass( this );orimport myPackage.MyClass
import flash.display.Sprite;

// create a displayObject
mySprite:Sprite = new Sprite();
// make sure its in a displayList
addChild( mySprite );
//pass it as a reference to add the listener to
var myClass:MyClass = new MyClass( mySprite );


You jus need to make sure that whenever u create this object you pass it a valid reference to a DisplayObject that is currently in a displayList (see http://www.actionscript.org/forums/showthread.php3?t=111165). You'll soon get the hang of it!

Incrue
07-16-2006, 12:00 PM
Thanks Tink!
Its working when i use

var myClass:MyClass = new MyClass( this );

or

var myClass:MyClass = new MyClass( stage );


But when i go

import MyClass;
import flash.display.Sprite;

mySprite:Sprite = new Sprite();
addChild( mySprite );
var myClass:MyClass = new MyClass( mySprite );

It goes:

ReferenceError: Error #1074: Illegal write to read-only property Sprite on global.
at Timeline0_3b5c32287e669d4ba5749adf69e05ff4/::frame1()

Incrue
07-16-2006, 12:02 PM
Wich is not such a great problem, i consider myproblem solved already but i would like to know why is this error comming, cos this can be the solution of future problems

Tink
07-16-2006, 12:25 PM
sorryimport myPackage.MyClass
import flash.display.Sprite;

// create a displayObject
var mySprite:Sprite = new Sprite();
// make sure its in a displayList
addChild( mySprite );
//pass it as a reference to add the listener to
var myClass:MyClass = new MyClass( mySprite );