Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 05-15-2012, 08:25 PM   #1
torrential
Registered User
 
Join Date: May 2012
Posts: 8
Default works when extending sprite but not eventdispatcher

I am trying to use an enter_frame listener in my class

addEventListener(Event.ENTER_FRAME, abc, false, 0, true);

to run

private function abc(e:Event):void

but it only works when I extend sprite. but this adds a lot of useless crap to my class and i don't know why it doesn't just work with eventdispatcher
torrential is offline   Reply With Quote
Old 05-15-2012, 08:51 PM   #2
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

The enter frame event is dispatched by the Flash playhead and only DisplayObject can listen to it since they are the only ones concerned by the playhead. For that reason you are contradicting yourself when you affirm you need an enter frame event but don't want to deal with DisplayObject because one doesn't go without the other. If you want an object that dispatches event at regular interval and that is not a DisplayObject then use the Timer class.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 05-16-2012, 01:04 AM   #3
torrential
Registered User
 
Join Date: May 2012
Posts: 8
Default

isn't timer possibly asynchronous?
torrential is offline   Reply With Quote
Old 05-16-2012, 01:48 AM   #4
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

If you mean that a Timer cannot rigorously follow the same rate than the enter frame event, that is correct. It can be set equal to the frame rate but the frame rate can slow down once in a while in which case the timer will be off syncro. If enter frame is really the only regular event type you want to work with but you don't want to extend a DisplayObject class you could instead use the composition pattern and create an internal object that can register enter frame (private var enterframedispatcher:Sprite) or maybe cleaner pass a reference to the stage in your class constructor and register your enter frame event on it.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 05-16-2012, 02:03 AM   #5
torrential
Registered User
 
Join Date: May 2012
Posts: 8
Default

Yes, that is what I meant by asynchronous but did not know the right word (my English is not perfect). I am trying to create a sound fading function which requires lowering volume of something over time.
torrential is offline   Reply With Quote
Old 05-16-2012, 03:32 AM   #6
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

Fading sound? Then why does it have to be in sync with the frame rate? Alternatively you can take a look at this page where I show a SoundManager class that can do this and much more.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 05-16-2012, 03:43 AM   #7
torrential
Registered User
 
Join Date: May 2012
Posts: 8
Default

wasn't able to look at the fading source code.

anyways though, because if i do it by timer there is a chance it won't fade out fast enough and result in ugly overlap
torrential is offline   Reply With Quote
Old 05-16-2012, 04:11 AM   #8
lordofduct
Senior Member
 
lordofduct's Avatar
 
Join Date: Feb 2008
Location: West Palm Beach, FL
Posts: 3,872
Default

won't fade out fast enough???

It will fade out as fast as you set the Timer for. If the Timer is to slow, reduce the time.

If it's because the animation is frame dependent (which it shouldn't be, animate to time, it's more predictable), well than listen to the ENTER_FRAME event of some dependent object... maybe the object you need to sync to!?

You could even tie it directly to that animated DisplayObject by having a property tell you how close it is to being finished (a percentage value), and just the fade of the audio to that. When it's done, the audio will be done.
__________________
www.lordofduct.com - come read my blog!

If you want to know how to program, take a math class, take a lot of math classes!
lordofduct is offline   Reply With Quote
Old 05-16-2012, 05:29 AM   #9
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

Quote:
Originally Posted by torrential
i don't know why it doesn't just work with eventdispatcher
The way it really works is that you can only add event listeners to objects that dispatch those same events. The EventDispatcher class isn't programmed to dispatch ENTER_FRAME events. But somewhere in the DisplayObject class definition (that we don't get to see, except in the documentation), the DisplayObject class is actively dispatching ENTER_FRAME events. And therefore, so is any class that inherits from DisplayObject, for example: Sprite.

(The one exception to the above rule about only being able to add event listeners to the same objects that dispatch them has to do with events that "bubble" up the display list, in which case you can add an event listener to a display object container for an event that is dispatched by one of its children).

Of course, you can add any event listener you want to any object that extends EventDispatcher (and therefore has an addEventListener method), and you won't get any errors. It will just fail silently if the object doesn't also dispatch that event (as programmed by its class).
__________________
My Tutorials * My Website
Mazoonist is offline   Reply With Quote
Old 05-16-2012, 06:31 AM   #10
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

I was going to suggest just extending DisplayObject instead of Sprite or EventDispatcher. But I just tried it, and it failed. Doing some online research, I learned that while you can write your own class that extends DisplayObject, you get an error when you try to instantiate it.

I find this interesting. Perhaps this is a side issue (and sorry for the digression), but I kind of wonder how they enforce this--that they can somehow extend DisplayObject, but we can't. I wonder what that code looks like.
__________________
My Tutorials * My Website
Mazoonist is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:58 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.