Flash Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignFlash Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 24th, 2012, 11:30 PM
rePete rePete is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 658 rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 3 h 47 m 13 sec
Reputation Power: 30
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??

Reply With Quote
  #2  
Old June 25th, 2012, 02:53 AM
stillwell stillwell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 60 stillwell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.
Attached Files
File Type: zip Example01.zip (5.5 KB, 37 views)

Reply With Quote
  #3  
Old June 25th, 2012, 08:20 PM
rePete rePete is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 658 rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level)rePete User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 3 h 47 m 13 sec
Reputation Power: 30
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.

Reply With Quote
  #4  
Old June 26th, 2012, 03:38 AM
stillwell stillwell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 60 stillwell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old June 28th, 2012, 05:31 AM
YEAV YEAV is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 4 YEAV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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???

Reply With Quote
  #6  
Old June 28th, 2012, 01:03 PM
Tann San Tann San is offline
Gotta get to the next screen..
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Nov 2003
Location: Legion of Dynamic Discord
Posts: 6,663 Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)  Folding Points: 14767 Folding Title: Novice Folder
Time spent in forums: 1 Month 1 Week 3 Days 20 h 9 m 55 sec
Reputation Power: 3163
Facebook MySpace
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).
Comments on this post
stillwell agrees!
__________________
Quis custodiet ipsos custodes?

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignFlash Help > ActionScript 3 - How do I refer to an object on stage??

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap