Go Back   kirupaForum > Flash > ActionScript 1.0/2.0

Reply
 
Thread Tools Display Modes
Old 11-12-2009, 04:36 PM   #1
mercuryfx
Registered User
Place 35 movie clips in random order on stage in specific XY coordinates

I have 35 movie clips named mcMyObject1, mcMyObject2, etc. to mcMyObject35

Each time the playhead enters the frame this code sits on, I want all of these 35 movie clips to be placed on the stage in random order

I want each one of these 35 movie clips to land on one of these X coordinates: 57, 187, 317, 447, 577, 707, 837 and on one of these Y coordinates: 53, 183, 131, 443, 573. (It's a 7 x 5 grid)

Movie size is 1024 x 768

I need help. Thanks!
Here's my code, which doesn't work:

stop();

var myXArray=[57, 187, 317, 447, 577, 707, 837]; //cordordinates for x
var myYArray=[53, 183, 313, 443, 573, 573, 573]; //cordordinates for y
var myArrayLength:Number=myXArray.length * myYArray.length;

for (i=1; i<=myArrayLength; ++i) {
this["mcFindObject"+1].Number="mcFindObject"+(i+1);
var setNewLocations = myXArray * myYArray;
}

trace(setNewLocations); //output says NaN

function setNewLocations() {
myXArray.sort(shuffle);
for (var j=1; j<=myXArray.length; j++){
this["mcFindObject"+((i-1)*7+j)].newXLoc=myXArray[i-1];
this["mcFindObject"+((i-1)*5+j)].newYLoc=myYArray[j-1];
}
}

trace(myXArray.sort(shuffle));

for (i=1; i<=myArrayLength; i++){
this["mcFindObject"+i].onEnterFrame=function(){
this._x += (this.newXLoc - this._x);
this._y += (this.newYLoc - this._y);
};
}

function shuffle(a,b):Number {
return Math.floor(Math.random() * 30) + 1; //Math.floor truncates the number
//generated by Math.random to a whole number
}

Last edited by mercuryfx; 11-12-2009 at 04:41 PM..
mercuryfx is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 11-12-2009, 05:18 PM   #2
cjke.7777
Registered User
 
cjke.7777's Avatar
Location Melbourne, Australia

Posts 147
Hi mate,

First you have a variable called setNewLocations and a function called setNewLocations() this will lead to problems. Second your trace (that returns NaN) is because you can't times two arrays.

Can i ask why you were * the arrays?

Also what are the names of the stage objects?
cjke.7777 is offline   Reply With Quote
Old 11-12-2009, 05:28 PM   #3
mercuryfx
Registered User
Quote:
Originally Posted by cjke.7777 View Post
Hi mate,

First you have a variable called setNewLocations and a function called setNewLocations() this will lead to problems. Second your trace (that returns NaN) is because you can't times two arrays.

Can i ask why you were * the arrays?

Also what are the names of the stage objects?

Thanks for the reply... I'm new to AS so am still trying to understand some things. This code was copied from someone who was doing the same thing with slightly different outcome.

I just need the code that will get me the results I'm looking for as specified at the top of my initial question.

The movie clip symbols I want put on the stage are named mcMyObject1, mcMyObject2, etc. up to mcMyObject 35.

I have been trying to figure this out for 3 solid days now, having been to every conceivable forum, every single tutorial available, etc. with no progress. Any code that will get me the results I need will help.
mercuryfx is offline   Reply With Quote
Old 11-12-2009, 06:20 PM   #4
glosrfc
Registered User
 
glosrfc's Avatar
Location Halley Research Station, Latitude 75°35' S, Longitude 26°39' W, Brunt Ice Shelf, Coats Land, Antarctica

Posts 4,160
ActionScript Code:
stop();
// Use just a single Object
var myArray = [
    {x:57, y:53}, {x:57, y:187}, {x:57, y:317}, {x:57, y:447}, {x:57, y:577}, {x:57, y:707}, {x:57, y:837},
    {x:187, y:53}, {x:187, y:187}, {x:187, y:317}, {x:187, y:447}, {x:187, y:577}, {x:187, y:707}, {x:187, y:837},
    {x:317, y:53}, {x:317, y:187}, {x:317, y:317}, {x:317, y:447}, {x:317, y:577}, {x:317, y:707}, {x:317, y:837},
    {x:447, y:53}, {x:447, y:187}, {x:447, y:317}, {x:447, y:447}, {x:447, y:577}, {x:447, y:707}, {x:447, y:837}
    {x:577, y:53}, {x:577, y:187}, {x:577, y:317}, {x:577, y:447}, {x:577, y:577}, {x:577, y:707}, {x:577, y:837},
    {x:707, y:53}, {x:707, y:187}, {x:707, y:317}, {x:707, y:447}, {x:707, y:577}, {x:707, y:707}, {x:707, y:837},
    {x:837, y:53}, {x:837, y:187}, {x:837, y:317}, {x:837, y:447}, {x:837, y:577}, {x:837, y:707}, {x:837, y:837}
    ];
// Now randomise it
myArray.sort(function () {
    return Math.round(Math.random());
});
// Retrieve the length (although it's always going to be 35 anyway!?)
var arrayLength:Number = myArray.length;
// Loop through the MC's and Array
// (note that i = 0 because Arrays are zero-based)
for (i = 0; i < arrayLength; i++) {
    // Assign the dynamic clip reference to a variable
    // (note the use of i + 1 because your MC's are NOT zero-based)
    var mc:MovieClip = this["mcFindObject" + (i + 1)];
    // Place the MC at the appropriate place according to the Array .x and .y properties
    mc._x = myArray[i].x;
    mc._y = myArray[i].y;
}

__________________
©2006 GlosRFC - Searching 8,168,684,336 brain cells
glosrfc is offline   Reply With Quote
Old 11-12-2009, 06:27 PM   #5
mercuryfx
Registered User
Quote:
Originally Posted by glosrfc View Post
ActionScript Code:
stop();
// Use just a single Object
var myArray = [
&nbsp;&nbsp;&nbsp;&nbsp;{x:57, y:53}, {x:57, y:187}, {x:57, y:317}, {x:57, y:447}, {x:57, y:577}, {x:57, y:707}, {x:57, y:837},
&nbsp;&nbsp;&nbsp;&nbsp;{x:187, y:53}, {x:187, y:187}, {x:187, y:317}, {x:187, y:447}, {x:187, y:577}, {x:187, y:707}, {x:187, y:837},
&nbsp;&nbsp;&nbsp;&nbsp;{x:317, y:53}, {x:317, y:187}, {x:317, y:317}, {x:317, y:447}, {x:317, y:577}, {x:317, y:707}, {x:317, y:837},
&nbsp;&nbsp;&nbsp;&nbsp;{x:447, y:53}, {x:447, y:187}, {x:447, y:317}, {x:447, y:447}, {x:447, y:577}, {x:447, y:707}, {x:447, y:837},&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;{x:577, y:53}, {x:577, y:187}, {x:577, y:317}, {x:577, y:447}, {x:577, y:577}, {x:577, y:707}, {x:577, y:837},
&nbsp;&nbsp;&nbsp;&nbsp;{x:707, y:53}, {x:707, y:187}, {x:707, y:317}, {x:707, y:447}, {x:707, y:577}, {x:707, y:707}, {x:707, y:837},
&nbsp;&nbsp;&nbsp;&nbsp;{x:837, y:53}, {x:837, y:187}, {x:837, y:317}, {x:837, y:447}, {x:837, y:577}, {x:837, y:707}, {x:837, y:837}
&nbsp;&nbsp;&nbsp;&nbsp;];
// Now randomise it
myArray.sort(function () {
&nbsp;&nbsp;&nbsp;&nbsp;return Math.round(Math.random());
});
// Retrieve the length (although it's always going to be 35 anyway!?)
var arrayLength:Number = myArray.length;
// Loop through the MC's and Array
// (note that i = 0 because Arrays are zero-based)
for (i = 0; i < arrayLength; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;// Assign the dynamic clip reference to a variable
&nbsp;&nbsp;&nbsp;&nbsp;// (note the use of i + 1 because your MC's are NOT zero-based)
&nbsp;&nbsp;&nbsp;&nbsp;var mc:MovieClip = this["mcFindObject" + (i + 1)];
&nbsp;&nbsp;&nbsp;&nbsp;// Place the MC at the appropriate place according to the Array .x and .y properties
&nbsp;&nbsp;&nbsp;&nbsp;mc._x = myArray[i].x;
&nbsp;&nbsp;&nbsp;&nbsp;mc._y = myArray[i].y;
}

awesome, I'll try it and reply later... many thanks mate!
mercuryfx is offline   Reply With Quote
Old 11-12-2009, 06:38 PM   #6
glosrfc
Registered User
 
glosrfc's Avatar
Location Halley Research Station, Latitude 75°35' S, Longitude 26°39' W, Brunt Ice Shelf, Coats Land, Antarctica

Posts 4,160
No worries...it does work

Not surprised your script didn't work as there were a host of syntax/logic errors in addition to those already highlighted:
this["mcFindObject"+1].Number - there is no built-in Number property for MovieClips and no indication that you've assigned your own property anywhere
this["mcFindObject"+((i-1)*7+j)].newXLoc - Same as above
this.newXLoc - no variable with this name has been declared
function shuffle(a,b) - this function appears to need two arguments but none have been passed to the function nor are they used in the function
Trying to shuffle the array partway through placing your movieclips into position is always going to be a problem...but shuffling it on every iteration of the loop is going to be disastrous.

As ever, it's a useful exercise to keep things as simple as possible. Use a single object rather than trying to merge two of them together. That can be done but certainly not by using the * operator. You're placing your clips in a linear fashion so shuffle the array once and then iterate through it in a linear fashion too.

Hope that helps

__________________
©2006 GlosRFC - Searching 8,168,684,336 brain cells
glosrfc is offline   Reply With Quote
Old 11-12-2009, 07:16 PM   #7
mercuryfx
Registered User
Quote:
Originally Posted by glosrfc View Post
No worries...it does work

Not surprised your script didn't work as there were a host of syntax/logic errors in addition to those already highlighted:
this["mcFindObject"+1].Number - there is no built-in Number property for MovieClips and no indication that you've assigned your own property anywhere
this["mcFindObject"+((i-1)*7+j)].newXLoc - Same as above
this.newXLoc - no variable with this name has been declared
function shuffle(a,b) - this function appears to need two arguments but none have been passed to the function nor are they used in the function
Trying to shuffle the array partway through placing your movieclips into position is always going to be a problem...but shuffling it on every iteration of the loop is going to be disastrous.

As ever, it's a useful exercise to keep things as simple as possible. Use a single object rather than trying to merge two of them together. That can be done but certainly not by using the * operator. You're placing your clips in a linear fashion so shuffle the array once and then iterate through it in a linear fashion too.

Hope that helps

Thanks again. It worked. I just adjusted the x y for my project and it worked perfectly! I'll study your code so I understand it fully. I hope you're around when I need help for other issues... but for now, you answered my question after a 3 day search! Would it be possible to get an email to ask you AS2 questions occasionally? Mine is mercurygrafx@comcast.net. Thanks much!
mercuryfx is offline   Reply With Quote
Old 11-12-2009, 07:20 PM   #8
glosrfc
Registered User
 
glosrfc's Avatar
Location Halley Research Station, Latitude 75°35' S, Longitude 26°39' W, Brunt Ice Shelf, Coats Land, Antarctica

Posts 4,160
The script was commented so you should be able to follow it. If I'm around, it'll be in here so feel free to post...if I'm not around, there are plenty of others who'll provide assistance.

__________________
©2006 GlosRFC - Searching 8,168,684,336 brain cells
glosrfc is offline   Reply With Quote
Old 11-12-2009, 09:09 PM   #9
mercuryfx
Registered User
Quote:
Originally Posted by glosrfc View Post
The script was commented so you should be able to follow it. If I'm around, it'll be in here so feel free to post...if I'm not around, there are plenty of others who'll provide assistance.

Fair enough. Thanks again!
mercuryfx is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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


All times are GMT -4. The time now is 06:33 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com