The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> Flash Help
|
ActionScript 3 - How do I refer to an object on stage??
Discuss How do I refer to an object on stage?? in the Flash Help forum on Dev Shed. How do I refer to an object on stage?? Flash Help forum discussing all products originally created by Macromedia including DreamWeaver, Contribute, Flash, Fireworks, Freehand, Director, Authorware and HomeSite. Adobe bought Macromedia in 2005.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 24th, 2012, 11:30 PM
|
|
|
|
ActionScript 3 - How do I refer to an object on stage??
Example:
I have two dynamic text fields on stage named text1_field, text2_field
I am trying to use a loop to fill those dynamic text fields
for(var i:int=0;i<2,i++){
this.Object("text"+[i+1]+"_field").text = hello;
}
Generally, how do you target objects, movieclips, etc. on stage by their instance name by using a loop??
|

June 25th, 2012, 02:53 AM
|
|
Contributing User
|
|
Join Date: Feb 2008
Posts: 60
Time spent in forums: 18 h 50 m 14 sec
Reputation Power: 6
|
|
I'd advice you to rename your instances to textField0 and textField1, to make it easier to reference them. Then you can use an array to hold your fields, which you need for the loop.
Code:
var textFields:Array = new Array(2);
textFields[0] = textField0;
textFields[1] = textField1;
for(i = 0; i < textFields.length; ++i)
{
textFields[i].text = "Hello " +(i+1);
}
Hope it helps.
|

June 25th, 2012, 08:20 PM
|
|
|
|
Thanks stillwell,
I was hoping there was a shorter process than having to create an array and just directly target an object, movieclip that is on stage.
|

June 26th, 2012, 03:38 AM
|
|
Contributing User
|
|
Join Date: Feb 2008
Posts: 60
Time spent in forums: 18 h 50 m 14 sec
Reputation Power: 6
|
|
Maybe there is, I just don't know it
Don't really think it's a complicated process, though.
|

June 28th, 2012, 05:31 AM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 4
Time spent in forums: 2 h 14 m 26 sec
Reputation Power: 0
|
|
|
Why not just use an array as icio suggested. You could then just have the one mc on stage, and then determine a cell of the array from which frame you are on???
|

June 28th, 2012, 01:03 PM
|
|
Gotta get to the next screen..
|
|
Join Date: Nov 2003
Location: Legion of Dynamic Discord
|
|
So with 3 TextFields on the stage "text0, text1 and text2" you can use:
Code:
import flash.text.TextField;
for(var i:int = 0; i < 3; i++)
{
(this.getChildByName("text" + i) as TextField).text = "Hello x" + i;
}
getChildByName lets you use the names you assign via the properties panel or when you do:
some_clip.name = "text" + i;
It returns an Object though so you have to cast it as I did. In the example I cast it to be a type TextField which then lets us call the ".text" property.
getChildByName is kinda slow when it comes to access, not something you'll notice with less than a hundred things but when you get into hundreds it may become noticable. The faster method is to use getChildAt which uses the index/layer/depth of the clip instead:
Code:
import flash.text.TextField;
for(var i:int = 0; i < 3; i++)
{
(this.getChildAt(i) as TextField).text = "Hello x" + i;
}
Same deal as before though, if you want to access any properties or functions then you have to cast it i.e. (this.getChildAt(i) as TextField).
__________________
Quis custodiet ipsos custodes?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|