AS3 Using Many Event Listeners Causing Problems, How to Reduce? - actionscript-3

Confusing title, my bad.
Basically, I have a list of names. Looping through, I add a MovieClip, Set 2 properties to it, the name, and an ID. The MovieClip is at the same time made to function as a button and I add 4 listeners, mouse up, over, down, or out. I do this with every name. The function each one is set to is the same.
EX: enemyButton[i].addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
The enemyID turns up "not valid property," time to time when I click, it doesn't crash at all, but sometimes I have to hit the button a few times.
I have narrowed the problem down to having to be caused by the listeners.
The function as simple as:
EX: function mouseUpHandler(e:MouseEvent):void { enemySelected(e.target.enemyID); }
My question is, is too many listeners likely to be the problem? and how can I reduce them?
Here's a snippet of the loop:
var C:Class = Class(getDefinitionByName(enemies[i]));
var c:* = new C();
c.gotoAndStop(1);
enemyButton[i].enemyID = i;
c.name = "select" + i;
c.enemyID = i;
trace(c.enemyID);
enemyButton[i].addChild(c);
enemyScroll.addChild(enemyButton[i]);
enemyButton[i].enemyName.text = info[i][Const.NAME];
enemyButton[i].setChildIndex(enemyButton[i].getChildByName("enemyName"), enemyButton[i].numChildren-1);
Thanks.

If enemyButton is a MovieClip (created via attachMovie, maybe) and not strongly typed as a EnemyButton class, then the ID property becomes dynamic. In this situation, if your list of names contains incorrect data (missing ID field, maybe), then the ID property will remain undefined on some instances of the MovieClip.
You can check the list of data used to generate movie clips. You can run into the same error if you have blank lines in your data.
This has nothing to do with event listeners.

So you just want to generate a bunch of buttons with unique properties and know what button was clicked last. Generally it is very bad idea to implement button logic outside button object. Why? Because you work with object oriented language. Good news is that you work with as3 and it treats functions as objects, so you can assign function to var like this:
var callback:Function = function(name:String, enemyId:int){ /*do something*/ }
And.. you can pass function as a parameter to another function
function setCalback(func:Function){}
button.setCallback(callback);
So, what you really need is to create your own button class, add listeners inside it, add handlers(static handlers will reduce memory usage) and pass callback function to it, that will be called when user clicks button.

Don't mean to spam this much but this was easily fixed, though the responses might have been a better method.
I just had to change target to the currentTarget, that then allowed clicking anywhere on the "button" to work. Whereas before the target varied from childs added to it.
So, solved.
Thanks for the help.

Related

AS3 Nullify Object through Reference

I have seem people asking this question, and getting an answer saying that this is impossible in AS3.
So I have a function called destroymc:
function destroymc(mcname:MovieClip):void{
mcname = null
}
This doesnot destroy the movieclip I want to destroy. Because it is passed by reference.
Is there really no way to make this work?
thanks.
Hm, as far as I know, to 'destroy' a MovieClip in AS3.0, you need to get rid of all references to it, and you need to remove it from the parent. Then the GC can collect it.
If you have an array of MovieClips, then something like this could work.
var mcs:Array;
// populate array
// to destroy the first one
mcs[0].parent.removeChild(mcs[0]); // remove the first element from its parent
mcs.shift(); // remove the first element in the array
And so if you want in in a function, you could pass the array to it
function destroymc(mcs:Array){
mcs[0].parent.removeChild(mcs[0]);
mcs.shift();
}
I'm using an array here because you need to use functions like this with dynamically generated MovieClips and entity managers and stuff. I hope this helps.

BUTTON LABELS in Action Script 3

Can someone tell me how to dynamically change the button label during run time?
Here is the code i tried:
var go:Button = new Button();
go = symbol_1;
go.label = "GO";
This does not seem to work.
The error stated is "Access of possibly undefined property label through a reference with static type flash.display:SimpleButton"
First of all, you're doing it the wrong way. If you say var go:Button = new Button(); you create a new Button object and then assign another object to go variable. So the new Button you created has been lost, there are no references to it, therefore there is no need to create a new Button in this situation.
Second, your symbol_1 is of type SimpleButton as it's clear from the error. SimpleButton objects do not have label property, that's why you're getting an error when trying to assign to it.
Now for the workaround. You can either place a TextField in your symbol_1 and give it an instance name, which you will then reference like this:
// assuming that instance name for the placed TextField is 'textBox'
symbol_1.textBox.text = "new label";
EDIT: Actually go for the second method described below as this first one is buggy and not dependable.
Or I'd recommend you make your own class based on a MovieClip which has a label property and can change it without much hassle.

Making HTML elements access a dynamically generated value

So I am making a data entry program where the user presses buttons to generate new inputs (numbers text etc.) and when finished the lists are generally between 100-10000 items.
The program has been coming along well, but now I am at a point where one set of data entered must generate the coices for an array [1,2, . . .] which is part of a later set of data.
So what I have done is setup buttons with the ID based on the earlier inputs. (The whole data set is saved as a JSON)
And what I want to do is when the button is pressed it looks pressed and writes to an HTML element the ID of the button which will later be read and saved to JSON.
My problem is centered on getting the correct information back to the user.
function doStuff(container){
for (var u = 0, c = someJSON.length; u < c; u++){
var someButton = document.createElement('button');
someButton.id = someJSON.id;
someButton.className = 'someButton';
someButton.onclick = function() {
writeIDToHTML(container,someButton,someButton.id);
}
container.appendChild(someButton);
}
}
function writeIDToHTML(container,theButton,theID){
console.log("theID")
console.dir(theID)
}
This prints only the last ID in the loop. How do I get each ID to each button?
The other thing to do is to give the button a pressed look.
Bonus points if it is reversable.
You should not add a listener on each element. The way to do it is adding a listener on the container and get the id of the clicked event (via event.target). This is called event delegation.
I could explain it, but this guys made a perfect answer to your question : http://davidwalsh.name/event-delegate
Btw, you should consider using a library like jquery to manipulate your DOM. It implements event delegation and advanced cross browser DOM manipulation utilities. For instance, you would not need to add a 'container' property since you can access it by the parent() method.

AS3 How to delete an object for good?

I create a new Shape, make a listener that starts a function when an object hits the shape, the function clears graphics of the shape, removes listener and deletes the child. But it looks like it leaves some ghost that still triggers when my object comes to it's place. I'm not sure why it happens, I thought clear+remove should make it impossible to hitTest. But somehow children still pile over one another and the last created one triggers the function. So if you understand, please give me a hint, if not please tell me in general what is a way to delete a created Shape for good, so it didn't count existing?
A bit of details. Hitting the shape must start a function that deletes it and creates a new shape of the same name that has a different function that does the same etc. so the current function must be the one I called for, not the last one, whose listener I deleted. Making new shape each time may overload memory, besides I do clean up every time. Did I forget anything?
fun0(){
var bob:Shape = new Shape();
addChild(bob);
bob...drawRect...
addEventListener(...,fun1)
function fun1(){ if(hitTest...){
bob...clear();
removeChild(bob);
removeEventListener(...,fun1)
fun2();
}
}
fun2(){
var bob:Shape = new Shape();
addChild(bob);
bob...drawRect...
addEventListener(...,fun3)
function fun1(){ if(hitTest...){
bob...clear();
removeChild(bob);
removeEventListener(...,fun3)
fun0();
}
}
etc., it only sees fun3 after I once trigger it even though I deleted the listener. Again, I do move object away, it doesn't hitTest anymore, then clear+remove. If you don't understand or cannot help, please don't flame, just ignore this, I need help, not the info that I'm not as smart as you are. Thank you.
first of dont declare functions inside of functions. then use weak reference for event listners.
// params: eventName, listener, capturePhase, priority, useWeakReference
someObj.addEventListener("eventName",myFunct,false,0,true);
you musst get rid of any references to the object, then set it to null
removeChild(bob);
bob= null;
after that GC will collect it!
edit:
if you keep doing the hitTest, like on enterFrame for example. check if the object is on the stage, by checking its .stage property.
if(bob.stage)
{
if(hitTest ...){};
}

How to copy Input Text Value into Dynamic Text that is in a MovieClip1 Inside a MovieClip2

Current my code is as such
setcustomlocation.addEventListener(MouseEvent.CLICK,customlocation2);
function customlocation2(e:MouseEvent):void
{
locationinput.text = FlashingWon.Won1.name.text;
}
I'm trying to make it such that it would copy the input text field values into a dynamic text. However, it throws up the error that
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at main/customlocation2()[main::frame1:9]
Which can I only assume that it is not able to communicate with the dynamic text field in the movieclip within another movieclip.
First, you can be 100% sure that it CAN communicate with the dynamic TextField in the MovieClip.
From you description I understand that you wish to copy from input into dynamic. But from your code I see that you take from the dynamic into the input, please check that out:
// This will copy in such a way: input <= wonText
locationinput.text = FlashingWon.Won1.name.text;
// This will copy from input
FlashingWon.Won1.name.text = locationinput.text;
Anyhow, the error that you get has nothing to do with this, it's rather like you already noticed, that one of your TextField is not 'found'. For this I recommend you a best practice: To create instances of the objects you want to use and populate them from the stage through the getChildByName method. This way you will promptly now (specially if you do this on construction or init) if you had any misspelling or miss structure on the childs you want to get.
like so:
var inputText: TextField;
var dynoText: TextField;
In your Constructor or else where at a soon level, give to your vars the proper value:
inputText = getChildByName('locationinput') as TextField;
dynoText = FlashingWon.Won1.getChildByName('name') as TextField;
This way you will soon enough know if one of this 2 textFields were not found under the object you give, and you have only one place to miss spell it. Also you will get code completion on your TextFields.
Finally the copy text part:
dynoText.text = inputText.text;
Hope it helps.
Alright, sorry for the delay, I was out on holidays.
I have opened your example and can see where the problems are:
1) You are trying to reach the FlashingWon when initiating the dynoText on the FIRST frame like so var dynoText = FlashingWon.Won1.getChildByName('name_txt'); BUT the FlashingWon element is ONLY available on the FIFTH frame. Meaning that you cannot refer to it quite yet, unless you add it on the first frame and make in invisible till the fifth frame. (You can make it visible in the goto1 function if you wish after the goToAndStop(5) line)
2) You called the TextField on the Won1 element 'name' which is a restricted sting in AS3, so change it to name_txt or label if you wish and it will work.
Let me know how it worked.