Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 06-17-2009, 09:20 AM   #1
chaudiovisual
Member
 
Join Date: Jun 2009
Posts: 85
Default help with circular alignment as3

Hi I was wondering if anybody could give me a little bit of help. I am working on a new project which uses nothing more than as3 and a single movie clip. I am pretty good with as3 (long as its not too complex), and I can decipher code pretty well but I am getting a really big headache when it comes to alignment. Basically I want the swf to open and create a series of movie clips, aligned in a circle. The only way I have been able to achieve anything like this so far has been to put in the coordinates manually for each instance:


var c1:circle = new circle();
var c2:circle = new circle();
var c3:circle = new circle();
this.addChild(c1);
this.addChild(c2);
this.addChild(c3);
c1.x = 100;
c1.y = 100;
c2.x = 300;
c2.y = 100;
c3.x = 500;
c3.y = 100;

the only problem with using this method is that at any one time I will not know how many mcs need to be generated on startup and therefore I can't put in the code manually. Now if I knew the number of mcs that needed to be generated I could use the above method and hardcode co-ordinates for each instance of the clip. Can anyone help me out? I am kind of new to as, only learned the basics before in college so everything else I know is self taught so please bare with me

(P.S. I know that the above code does not even begin to generate a circular alignment, but I just used the co-ordinates as a proof of concept )
chaudiovisual is offline   Reply With Quote
Old 06-17-2009, 09:51 AM   #2
daveystew
Dave Stewart
 
daveystew's Avatar
 
Join Date: Aug 2005
Location: London
Posts: 770
Default

ActionScript Code:
// types omitted for clarity var total = 10; var radius = 100; var increment = 360 / total; for(var i = 0; i < total; i++) {     var angle = increment * i;     var radians = angle * (Math.PI / 180);         var circle = new circle();     circle.x = Math.cos(radians) * radius;     circle.y = Math.sin(radians) * radius;     addChild(circle); }
__________________
xJSFL | Creative | Technical
daveystew is offline   Reply With Quote
Old 06-17-2009, 09:52 AM   #3
vedder
Member
 
Join Date: Jan 2006
Posts: 59
Default

Divide the number of radians in in a circle (2 * pi) by the number of Movieclips you want. Take that number and multiply it with the movieclip number then use the Sine and Cosine functions to find the right coordinate.

edit: beaten to it!
vedder is offline   Reply With Quote
Old 06-17-2009, 12:20 PM   #4
daveystew
Dave Stewart
 
daveystew's Avatar
 
Join Date: Aug 2005
Location: London
Posts: 770
Default

Vedder's way would indeed be slightly more efficient, as I'm recalculating radians each time within the loop! You should move this calculation outside the loop, then as he said, simply multiply it by the iterator value
__________________
xJSFL | Creative | Technical
daveystew is offline   Reply With Quote
Old 06-17-2009, 01:22 PM   #5
chaudiovisual
Member
 
Join Date: Jun 2009
Posts: 85
Question thanks but im still a little bit lost with it...

thanks daveystew and vedder, I get the logic behind all that but I just need to mess with it and then get it to work, but I do have one question which I hope either of you can help me out with, when the instances of the mc are being created inside the for loop, is there a way of giving each one an instance name i.e. as the first one is being created call it 'c1', I had thought there might have been a way to use the variable 'i' with something like this but its not working for me:

var total:Number = 5;
var radius:Number = 100;
var increment:Number = 360 / total;

for(var i = 0; i < total; i++)
{
var angle:Number = increment * i;
var radians:Number = angle * (Math.PI / 180);

var ['c'+i] = new circle();
['c'+i].x = Math.cos(radians) * radius;
['c'+i].y = Math.sin(radians) * radius;
}



The above of course could make as much sense as I do so I apologize in advance as I have never done anything with as3 to this level of complexity, really just controlled pre-existing mc with it to a great extent.
chaudiovisual is offline   Reply With Quote
Old 06-17-2009, 02:01 PM   #6
daveystew
Dave Stewart
 
daveystew's Avatar
 
Join Date: Aug 2005
Location: London
Posts: 770
Default

The instance reference and the instance name are two completely different things.

ActionScript Code:
var circle1 = new Circle(); // variable reference trace(circle1.name); // instance1

Therefore, you don't need to do a kind of "eval" (very AS2) in AS3, you just assign the name property a value:

ActionScript Code:
var circle = new Circle(); circle.name = 'circle' + i; // circle1

The reference circle will still point to the object. Hell you could even name it "IveGotBigPurpleBalls" and "circle" will still point at it!

Later when you want to get the instance back, either use a reference you've already stored, say in an array, or use getChildByName(), and leave all the this['name' + iterator] stuff behind with AS2

Helps?

Dave
__________________
xJSFL | Creative | Technical

Last edited by daveystew; 06-17-2009 at 02:04 PM.
daveystew is offline   Reply With Quote
Old 06-17-2009, 02:01 PM   #7
shawnblais
Senior Member
 
Join Date: Feb 2009
Posts: 360
Default

Something like that should be able to work, but why do you need to give them a name anyways? Maybe it's easier to add them to an array and reference them that way, or put them in a container clip and use a 'for each' loop to access them?

The more I work in AS3 I find myself rarely using instance names anymore on dynamic instances.
shawnblais is offline   Reply With Quote
Old 06-17-2009, 02:35 PM   #8
daveystew
Dave Stewart
 
daveystew's Avatar
 
Join Date: Aug 2005
Location: London
Posts: 770
Default

Agreed 100%
__________________
xJSFL | Creative | Technical
daveystew is offline   Reply With Quote
Old 06-18-2009, 09:10 PM   #9
chaudiovisual
Member
 
Join Date: Jun 2009
Posts: 85
Question sorry for such a late reply

Hi guys, thanks for all the help so far and I am sorry I am getting back to you so late but the laptop decided to die the day I posted. The reason I like to work with instance names is because I work so much with them already and because I have already created quite a large portion of the project using instance names and referring to specific names. I have never used getChildByName() and honestly I dont see how it works. I know I sound thick don't worry. Also I tried using that "circle.name = 'circle' + i;" solution but it doesnt work at all for me I don't understand why maybe that "eval" type of solution doesn't work with .name?

/Conor
chaudiovisual is offline   Reply With Quote
Old 06-18-2009, 10:49 PM   #10
shawnblais
Senior Member
 
Join Date: Feb 2009
Posts: 360
Default

http://livedocs.adobe.com/flash/9.0/...etChildByName()

It's really easy,

var _tmp = container.getChildByName("circle1");

If there is no container try using this.getChildByName("circle1");

Quote:
Also I tried using that "circle.name = 'circle' + i;" solution but it doesnt work at all for me I don't understand why
What do you mean it doesn't work at all? Did you try tracing out the instanceName to see if it's working?
circle.name = 'circle' + i;
trace(circle.name);

Last edited by shawnblais; 06-18-2009 at 10:53 PM.
shawnblais is offline   Reply With Quote
Reply


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How powerful is as3 and other questions ecsancho ActionScript 3.0 1 12-09-2008 03:22 AM
Circular Mask Alignment Problems OneninetyArtwork Flash 9 General Questions 1 10-01-2008 06:49 PM
Publishing in AS3 breaks AS2 script ATNO ActionScript 3.0 7 08-11-2008 03:24 PM
loading multiple as2 in as3 problems? player bug? Project1.exe ActionScript 3.0 5 07-28-2008 01:05 PM
AS3 in Flash 9 Discussion Linnium ActionScript 3.0 8 05-16-2007 09:41 PM


All times are GMT. The time now is 07:42 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.