Actionscript 3.0 :: Buttons Event Listeners And For Loops
Jun 20, 2010
I want to say that everything I code and work with is created through a main class and added to the stage through actionscript, the stage is completely empty before running.I've got a button in my library, and I create 8 of them and add them to the stage. I also add them to an aaray which i then compare in a for loop to add an event listener to them. I do it this way because they all run the same function. Each of the 8 buttons has a variable which is unique to that specific instance which I will use to do execute different sections of the function that gets called when I click one of the buttons.[code]
I have an array that returns multidimensional data from an mysql database, when this is collected the createNews function creates the user interface. The problem I am having is the loop is iterating quicker than the UI is being created, is there a way to use event listeners with loops so it only continues after my function has completed its work?
I'm trying to set up a simple button navigation system with a for loop that pulls a mc out of the library "i" number of times, adds it to the stage, then adds an event listener to it with a unique function name -- something like "onClick1, onClick2, etc", but with onClick[i] somehow in the loop instead.
var btnArray:Array = new Array(); for (var i:uint = 0; i<6; i++){ var myBtn:btn = new btn(); btnArray.push(myBtn); addChild(btnArray[i]); btnArray[i].x = i*50+50;
I have a button that is a movie clip that is set up with animations triggered with MOUSE_OVER and MOUSE_OUT event listeners. The glitch is that once the mouse is over the button ... each ensuing pixel that the mouse is moved, loops the animation again. That probably didn't make a whole lot of sense, so I posted the file on my test site [URL]. You have to drag your mouse slowly across the button to see this. The bottom part of the button works as I'd expect. When you roll over the text at the bottom of the button, everything functions as it should. The looping problem starts when you roll over the image portion of the button. For some reason it considers each consecutive pixel rolled over to be a new event and consequently triggers the "over" animation again.
The code I have is below. I've commented out the MOUSE_OUT function for now. There is also a screenshot at the above link of my Layers palette for the button clip in question. I put a "stop" label at each frame that has an action so you see the actions used. oneBedButton_mc.buttonMode = true; oneBedButton_mc.addEventListener(MouseEvent.MOUSE_OVER, overEffect); //oneBedButton_mc.addEventListener(MouseEvent.MOUSE_OUT, outEffect); function overEffect (e:MouseEvent) { oneBedButton_mc.gotoAndPlay("over");} function outEffect (e:MouseEvent) { oneBedButton_mc.gotoAndPlay("out");}
I'd like to add event listeners to movieclips (already on the stage), whose names are called from an array one by one, into a for loop.
Code: // there are 4 movielcips on the stage with name a1 - a4. // create array var Bro:Array = ["a1","a2","a3","a4"]; // for loop for ( var i:Number = 0; i < 4; i++) { // Add event listener to array values to buttons. Bro[i].addEventListener(MouseEvent.ROLL_OVER, roll, false, 0, true); //execute this function on rollover function roll() { trace("why wont my arrays work in for loops"); }}
i have a rookie question to ask and it's how to assign the same event listeners to multiple buttons in as3 instead of having to write out the same code over and over again like so...
I have 22 buttons (each named buttoni where i is a number between 0-21...e.g. button0, button1, button2) that I've created and artfully arranged on the stage in my .fla file. The document class for the .fla file is People.as, and in People I'd like to give each button a rollover and click event listener, but I can't figure out how to refer to them using a loop.
Here's my code, which obviously doesn't work:
ActionScript Code: private function initButtons() : void { for (var i:int = 0; i<numSquares; i++) { var thisButton : String = "button" + i;
Anyway, I am just trying to figure out movie clips as buttons, and have been following along on however, I seem to be doing these things right, but when i test my movie, the button just loops regardless of rollovers or stop actions.You can check this addressto see the failed anim, and you should be able to import it as well. Here is my coding:
function rollover (e:EVENT){ myBtn.gotoAndPlay ("in");} myBtn.addEventListener (MouseEvent.MOUSE_OVER, rollover); function rollout (e:EVENT){ myBtn.gotoAndPlay ("out");}
The problem is I get Error: Error #2094: Event dispatch recursion overflow. Why does removechild keep getting called if this.parent does not exist? Why doesn't removing event listeners work?
Can event listeners only be added to the class which dispatched the event? I ask because I want to have the logic for the listener on the main class, and the action is dispatched on click from a thumb which is instantiated in a scroller class. Kinda like this:
ActionScript Code: pseudo code: class main { main() {
[code]....
This doesn't work. Why can't the main class listen for an event on the thumb class?
I've been trying to make a sound-loop player for quite some time now (i'm new to as3) and i'm pretty close to do it but still Ok, i've made six letters converted them into buttons and i want them to play a different wav sound loop each one whenever i click on them and to stop the sound when i click again.This works fine for the first button but not for the rest of them because i dont know where to insert the similar code for the rest of the buttons.
import flash.media.Sound; var sound:Sound = new The_Rloop5(); var soundControl: SoundChannel = new SoundChannel();
I have an mc that when you rollover opens a menu system. There are buttons behind this menu system that I would like to have disabled when the menu system is open and re-enabled when the menu system is closed. I have this script on my timeline:
[AS] function lnkenable(isIt) { lnk0.enabled = isIt;
So I've gotten into using my own as Classes, and have an interesting question. My main swf calls a php script that gets data from a mysql database which is returned as XML. Part of that data that is retrieved is a filepath to a jpg image. It then takes that filepath and other information, and passes it to my external class, the "news story" class.
All fine and good. Then, the "news story" class uses a loader to load that image along with some text, organize it, and voila, you've got your own very pretty "news story". (I'm extending the MovieClip class here, though I suppose it doesnt matter)
Now, my main swf uses this component to load multiple of these "news stories". The problem is, theres that load time for the image. The main swf is loading them all one at a time, because they are putting each one together on their own.
The key here is that it be dynamic. So, since we don't know how much text is in each "news story", we also therefore don't know it's height. What I'm doing is creating each story in the main swf, pushing it into a "news story" array, adding an event listener to each ones' loader, and when the event listener is triggered, I want it to go to the 'add content' function where it sets the news story's Y value based off the position and height of the previous story that was added (stacking them, obviously). This makes sense, because otherwise it'd add them all on top of each other and it'd be a jumbled mess.
The problem is, the event listener is on the loader object INSIDE the component actionscript. So, how do I figure out which news story to set the Y value for? Because the eventlistener is listening to the loader, not the class. I hope this makes sense AT ALL. Here is the code:
Quote:
while (fancy shmantzy conditions) { /*creates the news story and passes xml data to it (filepath, etc)*/ storyBlock = new StoryBlock(a bunch of parameters); subCBArray.push(storyBlock); //array that stores each news story info
I have made an event listener:text0_mc.addEventListener(MouseEvent.CLICK, showMe);showMe is a function, but I need it to call more than 1 function at once i.e. -ext0_mc.addEventListener(MouseEvent.CLICK, showMe1, showMe2, showMe3, showMe4, showMe5, showMe6, showMe7);Each function contains an if statment as below.
function showMe1(event:MouseEvent):void { if (shuffledArray[1]==orderArray[1]){
Lately I've been twitching in the back of my head wondering just how many references to objects that I'm making, and whether or not they are getting GC'd or not.My question in this sense refers to event listeners and whether or not they are removed in certain cases where you get rid of an object. Two examples:
First, and I think this one is an easy one... when you create an object that adds an event listener to itself INSIDE it's own methods (e.g. this.addEventListener(e:Event, function)), when you REMOVE that object (this), do it's event listeners disappear too? I can only say yes, because they are referencing THAT object and are INSIDE that object.
So I think that question answers itself (but I'd love to know if the truth says otherwise).
But secondly, if you add an event listener to an object from a sibling or parent object, and then remove the object, does that event listener stick around, therefore maintaining a reference to that object and keeping it in memory? And is that the concept that people keep referring to of 'weak' event listeners? E.g. someChildObject.addEventListener(e:Event, function)Also, are event listeners by default strong or weak?
And I am assuming that 'yes', it DOES keep the reference. Which, if you are a n00b programmer like me, you probably are all like, 'aww sh*t damn, how am I supposed to make sure that my objects are getting thrown in the garbage collector's mouth of goodness every once in a while?
I found this code for a contact form at[url]...and it dynamically creates input fields and buttons. I didn't like the dynamically drawn submit and reset buttons, so I added my own MCs(my 'buttons') to the stage.I tried modifying the code and took out the dynamically drawn buttons..However, I cant figure out how to add Event Listeners to the buttons which exist on the stage--Im getting undefined errors...[code]...
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.[code]
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?[code]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)
In my Flash project I have a movieclip that has 2 keyframes. Both frames contain 1 movieclip each.
frame 1 - Landing frame 2 - Game
The flow of the application is simple:
User arrives on landing page (frame 1) User clicks "start game" button User is brought to the game page (frame 2) When the game is over, the user can press a "play again" button which brings them back to step 1
Both Landing and Game movieclips are linked to separate classes that define event listeners. The problem is that when I end up back at step 1 after playing the game, the Game event listeners fire twice for their respective event. And if I go through the process a third time, the event listeners fire three times for every event. This keeps happening, so if I loop through the application flow 7 times, the event listeners fire seven times. I don't understand why this is happening because on frame 1, the Game movieclip (and I would assume its related class instance) does not exist - but I'm clearly missing something here.
I've run into this problem in other projects too, and tried fixing it by first checking if the event listeners existed and only defining them if they didn't, but I ended up with unexpected results that didn't really solve the problem.
How do you know when you should remove event listeners? For example, I know that if I have a TimerEvent listener, I can safely remove that event when I have stopped and / or deleted my timer. Or, if I have a display object on the stage, and I remove it, I can safely remove any event listeners associated with the display object.
Im sure this has been asked a million times, so forgive me but I cannot seem to find an answer or my search query is wrong. I am using Tweener for animation.
I have 2 MC's on my stage. I have an event listener for my main MC (MOUSE_OVER, MOUSE_OUT) that triggers Tweener to animate another MC on top of the main clip. The problem is that when it overlaps the listener no longer works, and tried to execute the MOUSE_OUT. I understand why its not working, I just cant find a solution. Here is the code and a simple FLA.
well i wish to know, whats the maximum amount of events which can be called?
previously in as2 i ran into this problem when i kept using roll over and roll out events. and very quickly it reached its limit. so i wish to know what the maximum is, and is there any efficient way of asking flash how many are being called?
function myFunction(argument1:int, argument2:int):void { trace(argument 1 + argument2); }
i can use myFunction(1,2); to get a trace of "3" but if it's a mouse event, or timer event, or anything myMC.addEventListener(MouseEvent.MOUSE_DOWN, myFunction); we can't put arguments in this because it would have been
I've an array of movieclips created via code that have some textfields. Then i have added some listeners to the movieclips.
Here the code:
Code: arrNot[i].addEventListener(MouseEvent.MOUSE_OVER, titoloOver); private function titoloOver(e:MouseEvent){ var nome:String = e.target.name; TweenMax.to(curs,1 ,{y:contMain.getChildByName(nome).y + 23}); }
This code moves a cursor calculating the y coordinate depending the moviclip that is mouse overed. Now the movieclips have some textfields so when i mouseover the movieclips all works ok but when i mouseover the textfield i receive a #1009 error.
I have 8 movieclips on the stage: but1,but2,but3,..,but8.How do I use a for loop to add event listeners to each? I am trying this, but i'm sure its not correct: