Event Listeners VS Event Functions
I'm working on a new project at work where there's lots of
custom objects with lots and lots of event listeners. I keep
finding myself saying, "why didn't they just use the built in
objects with built in event handling?". However, from what I can
see it seems like AS3 leans heavily towards using Event Listeners
as opposed to putting event responsibility on the object itself
(E.G. XML.onload). Is there an advantage to this (other than making
it more Java like?) I've always favoured keeping the responsibility
on the object itself because it seems like one less object to keep
track of and also makes things feel more self contained. If I have
10 objects that fire 10 different events, I have to create 10
different event handlers (or 10 different case statements within
the same event handler), whereas with self event handling, I just
code in the event function.
Anyways, I wanted to open discussion to which method of event
handling you prefer and why, and also what you believe the
advantages to this. I was searching google for previous discussions
on this but I couldn't seem to find anything, so I thought I'd get
the ball rolling! Looking forward to reading these, try not to be
too elitist in your posts!!
I tried to change getFileOperation to getFileOperation2 in one of the functions as follows but same problem appeared.
function f3 (event:MouseEvent):void
trace("f3 triggered");
getFileOperation2.addEventListener(ResultEvent.RESULT, f4);
Do you have an idea how I can avoid the same function from triggering twice?? thanks a lot
Similar Messages
-
[JS] Basic question about event listeners and event handlers
I am very new to the whole topic of event listeners and event handlers. I'd run the test for the following problem myself, but I don't know where to start, and I just want to know if it's impossible -- that is, if I'm misunderstanding what event listeners and event handlers do.
Say I have an InDesign document with a text frame that is linked to an InCopy story. Can I use an "afterImport" event listener on the text frame to perform some action any time the link to the InCopy story gets updated? And will the event handler have access to both the story that is imported, and the pathname of the InCopy file?
Thanks.Thank you, Kasyan.
Both of those are good solutions.
We are currently using InDesign CS4 and InCopy CS5. I'm hoping to get them to purchase the whole CS5 suite soon, since I'd like to start writing scripts for InDesign CS5 as soon as possible, as long as I'm going to have to be doing it in the not too distant future anyway. The greater variety of event handlers sounds like it might be something that is crucial to the project I'm working on, so that adds to the argument for getting CS5.
Thanks again. You have no idea how helpful this is. I made some promises to my boss yesterday that I later realized were based on assumptions of mine about the InDesign/InCopy system that didn't make any sense, and I was going to have to retract my promises. But after reading your response I think I can still deliver, in a slightly different way that I had been thinking before.
Richard -
How Do I create a Modular Function for use with Event Listeners
OK I'm very new to AS3 and I'm curious as to how to get
modular functions to work in AS3.
for instance if I have this code:
function snowfall(snowflake:MovieClip, movement:Number):void
snowflake.y = movement;
snowfall(snowflake1_mc, 10);
snowfall(snowflake2_mc, 20);
snowfall(snowflake3_mc, 30);
Now on the stage I place 3 instances of a snowflake movie
clip. Each time I call the "snowfall" function I refer to a new
instance of the snowflake movie clip and a different amount of
movement along the y axis.
This is great but what if I want to use a button to control
these snowflake movie clips and I want to maintain that modular
nature? I don't really get how to do this. I created a button with
an instance name of "myButton" and I've tried this code but it is
throwing an error I don't really understand:
myButton.addEventListener(MouseEvent.CLICK,
snowfall(snowflake3_mc, 30));
Can I not pass information to a function using an
EventListener?Hi --
What you want to do on your button is this..
myButton.addEventListener(MouseEvent.CLICK,snowfall)
Then in the function you can check which button called the
function using
properties of the event..
function snowfall(event:MouseEvent) {
trace(event)
I am not sure off the top of my head of the data available in
the "event"
but the trace will show you all the variables you can access.
As far as the amount of movement -- how is this information
set? You can
keep this in a global variable and then read it from inside
the event
handler rather than needing to pass it to the event.
Rich
"HalcyonCollective" <[email protected]>
wrote in message
news:fl695r$l95$[email protected]..
> OK I'm very new to AS3 and I'm curious as to how to get
modular functions
> to
> work in AS3.
>
> for instance if I have this code:
>
>
function snowfall(snowflake:MovieClip, movement:Number):void
> {
> snowflake.y = movement;
> }
>
> snowfall(snowflake1_mc, 10);
> snowfall(snowflake2_mc, 20);
> snowfall(snowflake3_mc, 30);
>
> Now on the stage I place 3 instances of a snowflake
movie clip. Each time
> I
> call the "snowfall" function I refer to a new instance
of the snowflake
> movie
> clip and a different amount of movement along the y
axis.
>
> This is great but what if I want to use a button to
control these
> snowflake
> movie clips and I want to maintain that modular nature?
I don't really get
> how
> to do this. I created a button with an instance name of
"myButton" and
> I've
> tried this code but it is throwing an error I don't
really understand:
>
>
myButton.addEventListener(MouseEvent.CLICK,
snowfall(snowflake3_mc,
> 30));
>
> Can I not pass information to a function using an
EventListener?
> -
Using Multiple Event Listeners
Hi,
I have a movielcip (A) class in which I have used a Tween class effect on a child movieclip (B) scrollRect. The (B) Movieclip in turn has several movieclips whose have tween class effect being executed on thier child movieclips.
the tweens are all unique to each movieclip
and the event listeners are taken off once completed.
This works all well and good in FLASH IDE...
My problem arises when I try to view this in a browser on a Windows XP
it doesnt work in
Opera Version 9.63
Firefox 2.0
and Google Chrome 2.0
The only browser it works fluently in is
Internet Explorer 7.0.5
What is happening in most cases it that the animation appears to "stick" but i think what may be happening is the listening or removal of the event listeners. The animations are left incompleted.
Is there any rule of thumb when using multiple event listeners?
here is a snippet of some of my code
on click event from movieclip (A)
private function scrollToSlidePrev(e:MouseEvent) {
if (((slideIndex - 1) >= 0)) {
nextButton.mouseEnabled = nextButton.enabled = previousButton.enabled = previousButton.mouseEnabled = false;
var position:Number = 0-SLIDEAREA.width;
var slide1:TileListSlide = slides[slideIndex] as TileListSlide;
var slide2:TileListSlide = slides[--slideIndex] as TileListSlide;
scrollSlide(position,slide1,slide2);
tween animation in movieclip (A) on (B)
private function scrollSlide(pos:int,slide1:TileListSlide,slide2:TileListSlide) {
slide1.resizeSlideTo(0.6); // execute tween on child movie clips in B
slide2.resizeSlideTo(1); // same as above;
var rect:Rectangle = sliderMc.scrollRect;
var tween1:Tween = new Tween(rect,"x",Regular.easeOut,rect.x,rect.x + pos,3,true);
tween1.addEventListener(TweenEvent.MOTION_CHANGE,setSliderScroll,false,4);
tween1.addEventListener(TweenEvent.MOTION_FINISH,toggleButtonEnabled,false,3);
tween animation in movieclip (B) children
public function resizeSlideTo(sc) {
var m:Matrix = tileList.transform.matrix as Matrix;
var p:Point = new Point (m.a, 0);
var tween2:Tween = new Tween(p,"x",Regular.easeOut,p.x,sc,3,true);
if (numericStepper != null) {
if (sc != 1) {
numericStepper.visible = false;
tween2.removeEventListener(TweenEvent.MOTION_FINISH,showStepper);
if (sc === 1) {
tween2.addEventListener(TweenEvent.MOTION_FINISH,showStepper,false,2);
tween2.addEventListener(TweenEvent.MOTION_CHANGE,setScaleOnScroll,false,3);
here is the link
http://visual_admin.web.aplus.net/ticker/ticker_widget.html
the effect disables and re-enables the buttons when its done.... then the listeners are removed.
each one with the exception cretes its own unique tween (obviously this is a custom class built as each clip)
i really don't know what to make of it guysapparantly making the tween a property of the class rather than a random variable in a function worked.....go figure
-
Add event listeners to dynamically loaded symbols?
Hi,
I'm trying to dynamically load different MovieClips based on user input. Several of those clips have embedded url link symbols. How can I add event listeners to the url links if the dynamically loaded MovieClip does not have an instance name? Seems like I can (right?) apply an instance name when the clip is loading, but the event listener doesn't compile because the name is not there yet, so that seems out.
Am I on right track?Are you saying that you will instnatiate this symbol several times and each time url for this link is going to be different?
There are at list two ways to deal with it.
1. Inside symbol you can dispatch a custom event once it is clicked:
myLink.addeventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void {
dispatchEvent(new Event("linkClick"));
Wherever you instantiate the symbol (perhaps on timeline):
var symbolInstance:MySymbol = new MySymbol();
symbolInstance.addEventListener("linkClick", onLinkClick);
function onLinkClick(e:Event):void {
// do whatever
Second way would be to pass url value into symbol instance itself and deal with with it on a symbol level. I personally prefere way 1. -
Spry event listeners with dynamic form elements
Hi ,
I am trying to implement spry event listeners with a form that has dynamic check boxes and text field name. I now i can get the elements in a var and loop through with a normal function but can this work for spry events listenersShort answer is yes. But I have a feeling that you need further assistance, in which case give us a URL to a page that you want to apply the event listener to.
-
JSP have event listeners???
Hi Would like to check if JSP have event listeners?? Eg.Like onblur that is used to activate javascript. Instead of calling a javascript function, can it call a JSP code or function.
Do JSP has functions like javascript?
Also does anyone know how if javabean is able to return a value and place it in a textfield on a JSP page??
Please help me I am going more and more headache
as mine deadline draws near....sign :(No.
JSP can have Java code as scriptlets, but tag libraries are preferred. I don't put any scriptlet code in my JSPs.
You can add a JavaBean to page, request, session, or application scope in a JSP page and access the properties that have getters. Then you can assign the values to a text field.
I don't know how tight your deadline is, but the best approach in my view is to use Java Standard Tag Library (JSTL). Jakarta has a free implementation. I'd also recommend reading Shawn Bayern's "JSTL In Action" book. You'll be up to speed with JSTL very quickly. It will provide a solution to what you're asking, if your deadline will allow you time to learn it. The book is very well-written and technically excellent, IMO. - MOD -
Remove Event Listeners (at the right time)
I have a app which loads multiple flv's, I also have a timer
(digial) which shows how long the movie has been played for. The
problem is that whenever i load and new move (through the same
"loadVideos()" function, the timer variables ("lagTime", "endTime")
are not reset (even though the correct variables are being passed.
I believe it has something to do with the event listeners which are
being used to load the movies. How can i clear all the listerner
before reloading the movies?
Here is a code snippet:
Thanks in advanceThanks, kglad
Yes it traces 'endTime' but what happens is that when i play
the first movie is will display for example ("40,40,40,40" etc...)
until the video stops playing. If i load another movie it will then
trace ("40, 20, 40, 20, 40" etc...) so i need to kill the
listereners and reload the variables.
Jake -
Managing various event listeners in multi-frame movie
I'm working on a gunslinger game. I have numerous frames
which correspond to different stages of the game. The frame that
corresponds to the actual gameplay has a really involved
ENTER_FRAME function attached to particular item on the stage. When
the game is over (or an error occurs, or the network connection
fails) I want to leave the gameplay frame and move to some other
frame. When this happens, I would like the ENTER_FRAME function on
the gunslinger to be removed because the player may finish a game
and then re-enter the gameplay frame later after selecting a new
opponent.
QUESTION: Can anyone recommend a good way to manage the
keyboard event listeners, all the button event listeners, etc? As
you might imagine, I have no reliable way to determine which frame
might be entered after the gameplay frame because the frame change
could be triggered by a variety of different reasons.I have tried using hasEventListener to check for an existing
ENTER_FRAME function before adding the event listener again but it
never seems to notice that there is an existing enterframe function
running. Furthermore, when I leave the frame, the enterframe
function (which is attached to a particular movie clip on frame 2)
continues to run even though that movie clip doesn't exist on any
other frame in the flash movie.
The first frame in my movie just has a button that takes the
movie to frame 2. frame 2 has a movie clip (myMovie) and a button
(btnFrame1) which goes back to frame 1. It also has the attached
actionscript.
I start the movie.
I click 'goto frame 2'
the enterframe listener is added and starts to output trace
statements of the current frame of myMovie (e.g., 1-2-3).
i click 'goto frame 1' on the main movie
although i am looking at frame 1 of my main movie which has
no instance of myMovie, the enterframe function keeps tracing the
current frame--interestingly, the frame of myMovie does not
advance...it stays on 1, 2, or 3 and announces that frame over and
over again.
Repeating the visit to frame 1 and frame2 over and over again
results in more listeners being added over and over again.
I have uploaded a sample FLA here:
http://jaith.net/questions/listenerExample.zip
I hope that you might help me determine how to remove the
listener upon exiting this frame. I don't want that enterframe
function running (and referring to movieclips that don't exist)
when I exit the frame where it is relevant. I'm wondering if I
should try adding the event listener to the root movie
instead. -
Is it possible to have event listeners apply to multiple objects, instead of creating multiple functions?
I'm just wondering if there is an easier way than making a new function for the same event listeners for each object I want to apply it to. Here's my code thus far, just to clarify what I'm talking about:
import gs.*;
var mc_homeX:Number = mc_home.x;
var mc_productsX:Number = mc_products.x;
var mc_msdsX:Number = mc_msds.x;
var mc_environmentX:Number = mc_environment.x;
mc_home.addEventListener(MouseEvent.MOUSE_OVER, onOver, false, 0, true);
mc_home.addEventListener(MouseEvent.MOUSE_OUT, onOut, false, 0, true);
function onOver(e:MouseEvent):void
TweenLite.to(e.target, .2, {x:mc_homeX-10});
function onOut(e:MouseEvent):void
TweenLite.to(e.target, .2, {x:mc_homeX});
I've only starting adding the listeners to mc_home, but I'd like to apply them to all. Any help would be appreciated. Thanks.import gs.*;
var mc_homeX:Number = mc_home.x;
var mc_productsX:Number = mc_products.x;
var mc_msdsX:Number = mc_msds.x;
var mc_environmentX:Number = mc_environment.x;
var mcA:Array=[mc_homeX,mc_productsX,etc];
for(var i:uint=0;i<mcA.length;i++){
mcA[i].addEventListener(MouseEvent.MOUSE_OVER, onOver, false, 0, true);
mcA[i].addEventListener(MouseEvent.MOUSE_OUT, onOut, false, 0, true);
function onOver(e:MouseEvent):void
TweenLite.to(e.target, .2, {x:mc_homeX-10});
function onOut(e:MouseEvent):void
TweenLite.to(e.target, .2, {x:mc_homeX});
I've only starting adding the listeners to mc_home, but I'd like to apply them to all. Any help would be appreciated. Thanks. -
I have a problem with the firing of event listeners. I have 4 functions (see below). Function f1 is triggered when button1 is clicked and function f3 is triggered when button3 is clicked. Then f1 calls getFileOperation (a webservice operation) and should call function f2 whereas function f3 should call function f4.
function f1 (event:MouseEvent):void
trace("f1 triggered");
getFileOperation.addEventListener(ResultEvent.RESULT, f2);
function f2 (ev:ResultEvent):void {
trace("f2 triggered");
function f3 (event:MouseEvent):void
trace("f3 triggered");
getFileOperation.addEventListener(ResultEvent.RESULT, f4);
function f4(e:ResultEvent):void {
trace("f4 triggered");
However the output being given when I press button1 followed by button2 is:
f1 triggered
f2 triggered
f3 triggered
f2 triggered ///repeated!!!
f4 triggered
Similarly the output being given when I press button2 followed by button1 is:
f3 triggered
f4 triggered
f1 triggered
f4 triggered ///repeated!!!
f2 triggered
Why is f2 fired again in the first case and f4 in the second case? How can I go about it to avoid this repetition. Any help would be appreciated...thanks!!I tried to change getFileOperation to getFileOperation2 in one of the functions as follows but same problem appeared.
function f3 (event:MouseEvent):void
trace("f3 triggered");
getFileOperation2.addEventListener(ResultEvent.RESULT, f4);
Do you have an idea how I can avoid the same function from triggering twice?? thanks a lot -
How to block execution of event listeners
Hi all,
JDev version : 11.1.1.6
My requirement is that I want to block all event listeners like ActionListeners, SelectionListeners, DisclosureListeners, RowDisclosure listeners when the screen is opened in readonly mode.
I could block ActionListeners by disabling command links and command buttons etc.
But there's no way to block SelectionListeners, DisclosureListeners, RowDisclosure listeners.
So Is there any common code which can block all listeners?
Or is there any EventController or something like that which will allow me to control event execution?I would have if it was just one screen.
There are hundreds of screens.
Anyways, can't I use any javascript to do this? There are some interfaces like EventListeners , classes like EventConsumer etc. Do none of them provide feature to block event listeners? -
Help me I'm trying to create event listeners
I am trying to create event listeners
Whenever any keyboard event occurs the string s gets appended by a,c
or p.At the end of the program its supposed to print value of s
But s always happens to null.Please tell me how to correct it....so
that detection is possible for any case
import java.io.*;
import java.awt.event.*;
public class trial {
int x;
public static String s;
//OutputStream f1=new FileOutputStream("file2.txt");
public static void keyReleased(KeyEvent e)
s=s+"a";
public static void keyPressed(KeyEvent e)
s=s+"c";
public static void keyTyped(KeyEvent e)
s=s+"p";
public static void main(String args[])
throws IOException
char c;
BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Entercharactes,'q' to quit.");
do
c=(char)br.read();
System.out.println(c);
while(c!='q');
// for(int i=0;i<s.length();++i)
System.out.println(s);I suggest looking at the java tutorial. Your code completely misses the mark and you need to see examples.
-
In iPhoto, how can I find a particular event using the search function?
NO - read my last answer over a year ago
That is because iPhoto is all about photos - not about files
You do not access the piles in iphoto - you access the photos using either iPhoto or the media browser - see this user tip for photo access details
LN -
Passing multiple events as parameters to functions
I want to pass MouseEvent and a custom ComboSelectedEvent to
a function. I really would like to do this:
public function getPublications(event:MouseEvent,
event:ComboSelectedEvent)
But it doesn't work in Flex. To make it work, I ended up
using the Event class, which includes all events:
public function getPublications(event:Event)
Is this the correct Flex way or is there a better way? Thanks
for your help.You should be able to pass the two event objects into the
function, but as Richard indicated, the two cannot have the same
name within the method, and they don't need to either. Perhaps
offer more details on your situation.
Maybe you are looking for
-
Ok this is an odd one I noticed today when I was looking at my notes on my Iphone 4 that any links or phone numbers would flicker every once in a while but Anything that was not a phone numbers of web link would not flicker. I thought something was w
-
Is there a way to find out who created a user account?
Thanks.
-
Display SXMB_MONI XML errors in Solution Manager (CEN)
Hello All, Requirement is to display the XML error messages in SXMB_MONI in our Solution Manager (CEN)? Could you please let me know the major steps that would be involved on the same? Please share your thoughts! Thank You, Antarpreet
-
Sap.m.Table: How to disable includeItemInSelection for one column?
Dear UI5 Experts, I have used includeItemInSelection method for whole table as it is required for me on selectionChange event in sap.m.Table control. Now, I have a requirement where I need to disable that method for one column. Is there a way to disa
-
How to insert a record & reset the sequence for all record
Hi All, I am using Oracle Form 6i. In one of my forms, I need to delete all records id and reset the ids in a sequence when i do a new insert id value 1 x 2 y 3 z when i insert a record 4 between 2 & 3 records with value 'A' id value 1 x 2 y 3 z 4 A