A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: place mc randomly between _x 100 and 300

  1. #1
    Senior Member
    Join Date
    Apr 2003
    Posts
    126

    place mc randomly between _x 100 and 300

    I'm trying to place my mc's at a different position each time the game starts but within a certain area. I've used random before...
    random(300);

    ...but I want it to be randomly between the coords
    _x 150 and 450
    _y 250 and 350

    I'm hoping for some help, I do all of the 3d animation and really struggle with the actionscript part of it.

    Thanks,
    Puke

  2. #2
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    _x=Math.floor(Math.random()*300+150);
    _y=Math.floor(Math.random()*100+250);
    Z¡µµ¥ D££

    Soup In A Box

  3. #3
    Senior Member
    Join Date
    Apr 2003
    Posts
    126

    Thanks

    I'm sorry, I'm kinda slow as far as actionscripting.
    I have 5 cars, cars1mc, cars2mc etc.

    The way I'm doing it doesn't seem to be the correct way to make each car locate to at random coordinates.

    You're declaring a variable _x to be have thess random coords then I add a setProperty to each car mc?


    _x=Math.floor(Math.random()*300+150);
    _y=Math.floor(Math.random()*100+250);


    setProperty (car1, _x,);
    setProperty (car1, _y,);

    Thanks for your patience, I'm more of an animator than a developer.

  4. #4
    Senior Member
    Join Date
    Apr 2003
    Posts
    126

    Cars

    Oh, I think I got it.

    I placed the code on each car

    onClipEvent (load) {
    _x=Math.floor(Math.random()*300+150);
    _y=Math.floor(Math.random()*100+250);


    setProperty (rabbit2, _x, random);
    setProperty (rabbit2, _y, random);
    }

    It sets each car at a different location everytime it loads.
    If you have a sec could you explain this...
    _x=Math.floor(Math.random()*300+150);

    otherwise, thanks again.

  5. #5
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    The Math.random() function randomly returns a number between 0 and .99999...

    The line Math.random()*300 will generate a random number between 0 and 299.9999...

    However, you don't want this random number to start at zero, you want it to start at 150, so you simply add 150.

    This would work fine just the way it is, however you wouldn't get a nice, clean integer such as 236. You'd probably get something more like 236.59938953.

    So just to clean it up, I used Math.floor(Number). This extremely useful function takes any number and simply rounds down to the nearest integer. It ALWAYS rounds down, no matter what. You could think of it as removing the decimal places.
    (There is also Math.ceil(Number) which always rounds up, and Math.round(Number) which rounds to the nearest integer.)

    Putting all of that together, you have this:
    _x=Math.floor(Math.random()*300+150);

    I hope that all made sense!

    Just as a side note, I'm really glad that you asked me to explain this. It's a huge pet-peeve of mine when programmers use lines of code without actually understanding what those lines are doing.

    Keep it up!
    Zippy Dee
    -Ted Newman
    Z¡µµ¥ D££

    Soup In A Box

  6. #6
    Senior Member
    Join Date
    Apr 2003
    Posts
    126

    Thanks.

    Thanks for taking the time to explain, it's tough for guys like me who concentrate primarily on design. I'm creating a race game using 3d animation as the background. Even though the cars are standing still, it looks like they're racing down the track.

    The shell program sends me a string of numbers (25), I use the [0] first number in the array to determine the predetermined number as the winner. We're taking bingo games from paper into electronics.

    It's folks like you at FlashKit that have helped tremendously. So, thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center