0
votes

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.

3
Can you post the loop code? How many objects are you adding? There shouldn't be a problem with the number of listeners.takteek
No, it would seem the error you are receiving is that you're accessing enemyButton[X] while iterating... using object[X] is referencing a property on that object and one of them isn't valid for attaching a listener to. However, because you've posted no code and havn't shared the exact error message, this is pure speculation and could be completely wrong advice.user562566
This is definitely a weird problem. Last night tried maybe 15 times to get the error to come up, and couldn't. First try today, error came up. ReferenceError: Error #1069: Property enemyID not found on flash.text.TextField and there is no default value. at WarmingInDanger_fla::MainTimeline/mouseUpHandler()Evan Carslake
The code for the loop I am using is pretty large, but here is a snippet: 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);Evan Carslake
enemyButton[] is an array of course, c belongs to a MovieClip of the enemy (to be scaled down and added to the button, no issues there) var C:Class = Class(getDefinitionByName(enemies[i])); var c:* = new C(); enemyScroll is a MovieClip created which holds all the buttons. Sorry for the bad formatting and multiple comments, I had to break it up for the character count.Evan Carslake

3 Answers

0
votes

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.

0
votes

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.

0
votes

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.