Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to handle EventListeners/Handlers for 1000 buttons?
    primarykey
    data
    text
    <p>I'm working on a game where users can win buttons/badges as awards. I estimate there will be around 500 awards, in all.</p> <p>I want to allow the users to sell their duplicate/unwanted badges to the bank and buy new ones from the bank.</p> <p>I am showing the badges in multiple awards frames. Below the badges, I show the count, along with a "Buy" and "Sell" button.</p> <p>My question is: What's the best way to handle SO MANY buy &amp; sell buttons? Is there a way around coding 1000 eventListeners and 1000 eventHandlers?</p> <p>One possible solution is to name my buttons, incrementally, like "buy_mc1", "buy_mc2", etc. Then do a loop to create listeners for this["buy_mc" + i]. However, I would then have to make VERY sure that I link these back correctly to the awards.</p> <p>Currently, I have an "award_mc" field in my database, which stores the name of the movieclip in my .fla. From that, I can access related assets, like the count field, like: this[db.award_mc + "_count"].txt. </p> <p>Unfortunately, this method does not lend itself well to looped access.</p> <p>Maybe I can create an array of movieclip names that are in synch with the buy/sell buttons. For example:</p> <p>myArray = {aMissionAward, anotherAward, ubernessAward};</p> <p>My buttons would be: buy_0, buy_1, buy_2, etc.</p> <p>When someone clicks a button, I can link buy_2 to "ubernessAward", then look for "ubernessAward" on the database.</p> <p>How can I get the number from the button, though? Even if I can loop through setting up the eventListeners, will I have to set up individuatl eventHandlers to process the correct index number?</p> <p>I'm open to suggestions on another way to do this.</p> <p>-------------------------- EDIT ----------------------------</p> <p>Here's my solution. Props to Jens Struwe for pointing me in the right direction.</p> <p>I was not able to get this to work with buttons on the stage. Apparently, buttons are a static class. MovieClips are dynamic, so I was able to get this to work by using MovieClips. Here's what I did:</p> <p>Added 3 MovieClips to the stage. Named them "mc0", "mc1", and "mc2".</p> <p>Add this AS3 code:</p> <pre><code>for (var i:Number=0; i&lt;3; i++) { this["mc" + i].addEventListener( MouseEvent.CLICK, onMcClick ); this["mc" + i].awardKey = i; } function onMcClick( evt:MouseEvent ):void { trace("MC ID = " +(evt.target).awardKey); switch ((evt.target).awardKey) { case 0: trace(" Do stuff for 0"); break; case 1: trace(" Do stuff for 1"); break; case 2: trace(" Do stuff for 2"); break; } } </code></pre> <p>From here, I can tie the awardKey to an array of MovieClip names. Then, I can use the MovieClip names to read my database.</p> <p>This is why I come to Stack Overflow, FIRST, when I have a problem. :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload