Results 1 to 15 of 15
-
October 15th, 2008, 05:55 PM #144Registered User
postsLooping though objects on the stage
In AS2, if I needed to access all the movieclips on the stage, I could do this:
for (var i in this) {
if (typeof (this[i]) == "movieclip") {
do stuff
}
}
with the preceeding code written on the root.
I'm having all kinds of trouble trying to do the same thing in AS3.
The movie clips I'm attempting to access have been manually placed on the stage. They're not beind added via "addChild", if that makes a difference.
-
October 15th, 2008, 06:35 PM #2
-
October 16th, 2008, 10:09 AM #344Registered User
postsWhat if I'm trying to find all instances of movieclips or buttons or any other object?
-
October 16th, 2008, 08:02 PM #4
-
October 17th, 2008, 03:06 PM #544Registered User
postsWow... this seems really complicated... for all the big talk about AS3, I have found it has taken things that were really simple and intuitive in AS2 and made them unnecessarily complicated. I wonder if the people at Adobe really understand what people are trying to do with their software.
Thanks for your help. I think I'm going to get out of this business and start mowing lawns or something.
-
October 20th, 2008, 02:13 PM #644Registered User
postsActually... that is completely overly complicated. There HAS to be a simpler way to do this. There has to be.
-
October 20th, 2008, 02:20 PM #7
-
October 20th, 2008, 03:18 PM #844Registered User
postsWhen I try that, I get an error which reads:
"1120: Access of undefined property Button."
-
October 20th, 2008, 03:22 PM #9
"Button" is the classname, could be MovieClip or Sprite. I recommend reading some tutorials or books about Actionscript 3 fundamentals.
-
October 20th, 2008, 03:26 PM #10
for Buttons as in button components, you would need to import their definition via
import fl.controls.Button;
For native button symbols, it would be SimpleButton - but it all depends on what you're looking for. As Valaran said, it could be MovieClip or Sprite too. It's what you asked for in your second post.
-
October 20th, 2008, 04:21 PM #1144Registered User
postsI am at such a loss with this. I am trying to replicate something in AS3 that I have done many times with AS2.
All I want to do is loop through the buttons, then have the name of the button used to call up a specific node in the XML I already have loaded. I have done this type of thing millions of times in AS2, but trying to do this in AS3 is proving to be immenesely cumbersome.
The following code tells me that "i" is now out of bounds. If I create a new function for the event listener, like you're supposed to in AS3, I have no way of passing the button name to that function.
I am tearing my hair out trying to get this seemingly simple code to work.
for( var i:int = 0; i < this.numChildren; i++ )
{
if (getChildAt(i) is SimpleButton)
{
getChildAt(i).addEventListener(MouseEvent.MOUSE_UP , function(evt:MouseEvent):void {
trace(getChildAt(i));
}
)
}
}
-
October 20th, 2008, 06:02 PM #12
Your problem would still exist in AS2. The same applies with functions defined in loops that reference loop variables (see http://www.senocular.com/flash/tutor...#loopfunctions )
-
October 20th, 2008, 06:38 PM #13
Many people used that loop-through-all-items technique in AS2 and stored data (i) within the individual object - but that doesn't make it the proper (and by proper I mean the most expandable and most clearly understandable) way to do things. That's why AS3 doesn't come with an displayObject.findAndAssignDataToUnknownObjects() method.
AS3 is hard to follow - I think most people have been frustrated in the beginning. One reason is because many of everyone's code snipets need to be scrapped and rebuilt ground-up in AS3. When you're not used to setting up classes in as2 and working with EventDispatcher and Listeners in as2, then these small old-ways of doing things can be extremely frustrating. Everyone feels your pain - but, I think I speak for tons of people when I say the longer you push through the initial learning curves of AS3, you'll really learn to like AS3 and many of the ways it forces you to work.
The core concepts are always the same - many buttons that essentially do the same thing, and how to pair buttons with the appropriate data when clicked. You just have to be open to new ways of achieving the same result and do quite a bit of research to find how people are doing these very same things.
See the attached FLA as an example, lots of comments, hope this helps.Last edited by creatify; October 20th, 2008 at 06:43 PM.
-
October 20th, 2008, 07:31 PM #14
This may help. I use Flex so you might have to change a few things. I use e4x to grab the length of the XML and the name tag.
PHP Code:private var buttonCollection:XML;
private function resultHandler(event:ResultEvent):void
{
buttonCollection = event.target.lastResult;
for (var i:uint = 0; i < buttonCollection.item.length(); i++)
{
var myCoolBtn:Button = new Button();
myCoolBtn.name = "button" + i;
myCoolBtn.label = bannerCollection.item.name[i];
myCoolBtn.x = 0;
myCoolBtn.y = 10 + i * (myCoolBtn.width + 60);
this.addChild(myCoolBtn);
myCoolBtn.addEventListener(MouseEvent.CLICK, detectButton);
}
}
Last edited by ryguy; October 20th, 2008 at 07:34 PM.
-
October 22nd, 2008, 04:26 PM #1544Registered User
postsOK.. so what if "detectButton" needs to have a different variable passed to it depending on which button was clicked? Say the function is going to diplay an XML node based on which button is clicked.

Reply With Quote




Bookmarks