Be a Supporter!

Random Attached Movieclips

  • 346 Views
  • 4 Replies
New Topic Respond to this Topic
Legzl
Legzl
  • Member since: Oct. 10, 2007
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Random Attached Movieclips 2007-10-10 19:17:25 Reply

Hi. Ive been working at this for a while, but still cant get it. I need to attach (using attachMovie) a random movieclip every x amount of milliseconds (ill probably be using random intervals). I know how to use attachMovie, I don't know how to attach random movieclips. I'll explain. Say I have 5 Mcs. Lets call them sword, gun, knife, dagger, and missile. I want one of those 5 to appear every x amount of seconds, and have it completely random. How should I go about doing this?

Example codes would really help, and a little explanation. Thanks a lot for any help.

knugen
knugen
  • Member since: Feb. 7, 2005
  • Offline.
Forum Stats
Member
Level 42
Programmer
Response to Random Attached Movieclips 2007-10-10 19:26:55 Reply

Using an array would probably be the best way to do it. If you don't know how they work, go and read about them in AS:Main, if you do:

-Make an array containing the 'linkage-names' of the mc's that you want to attach
-Then do something like this when attaching:

attachMovie(yourArrayName[random(yourArrayName.length-1)], etc.., etc...)
Archmage-flash
Archmage-flash
  • Member since: Jul. 3, 2007
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to Random Attached Movieclips 2007-10-10 19:32:26 Reply

At 10/10/07 07:17 PM, Legzl wrote: Hi. Ive been working at this for a while, but still cant get it. I need to attach (using attachMovie) a random movieclip every x amount of milliseconds (ill probably be using random intervals). I know how to use attachMovie, I don't know how to attach random movieclips. I'll explain. Say I have 5 Mcs. Lets call them sword, gun, knife, dagger, and missile. I want one of those 5 to appear every x amount of seconds, and have it completely random. How should I go about doing this?

Example codes would really help, and a little explanation. Thanks a lot for any help.

Well it would involve intervals.

This is how I make random countdowns

var timer:Number = 0;
var randomCounter:Number= Math.ceil(Math.random()*5); //will create a number from 1 to 5

function fPlus(){
timer +=0.1;
}
setInterval(fPlus, 1000);

onClipEvent(enterFrame){
if (randomCounter>=timer){
timer=0;
randomCounter=Math.ceil(Math.random()*5)
; //randomized countdown
//attach your movie clip here
}}

to randomly select a MC generate a random number like this

Math.ceil(Math.random()*number of MCs);

then attach an MC depending on what value you get from the randomizer.


Flash is happy and it knows it.

BBS Signature
Legzl
Legzl
  • Member since: Oct. 10, 2007
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Random Attached Movieclips 2007-10-10 20:02:03 Reply

Thanks guys. I'm gonna go with Dreamworx way and see how it works, as it was the way I originally tried. I was on the right track, just forgot to use the length method. I'll tell you if it works.

theslof
theslof
  • Member since: Nov. 12, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to Random Attached Movieclips 2007-10-10 20:03:02 Reply

var myInterval:Number;

spawnWeapon = function(){
      var rndNum:Number = Math.round(Math.random() * 4);
      switch(rndNum){
            case 0: var weapon = "dagger";
            case 1: var weapon = "sword";
            //And so on
            default: "var weapon = "default"; //eg. none
      }
      attachMovie(weapon, "newWeapon", getNextHighestDepth();
}

myInterval = setInterval(this. spawnWeapon, 1000);

Just adapt this code to fit your cause.
Worst case scenario: Paste it into flash, click on the words you dont understand (switch, case, setInterval, etc.) and press F1.