A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: Clouds/stars moving horizontally help?

  1. #1
    Senior Member
    Join Date
    Nov 2007
    Location
    California
    Posts
    118

    Clouds/stars moving horizontally help?

    So I have this great code that works for creating stars/clouds/moving objects randomly across the stage, but I cant get it to change direction to horizontally rather vertically. Help? Heres the code, but I think that a simple change will get the results I want.:

    var stars = 40;
    var maxSpeed = 16;
    var minSpeed = 2;

    for (var i = 0; i<stars; i++) {
    var mc = this.attachMovie("star", "star"+i, i);
    mc._x = random(Stage.width);
    mc._y = random(Stage.height);
    mc.speed = random(maxSpeed-minSpeed)+minSpeed;
    var size = random(2)+0.6*(random(4));
    mc._width = size;
    mc._height = size;
    }


    function moveStars() {
    for (var j = 0; j<stars; j++) {
    var mc = this["star"+j];
    if (mc._y<Stage.height) {
    mc._y += mc.speed;
    } else {
    mc._y = 1
    mc.speed = random(maxSpeed-minSpeed)+minSpeed;
    mc._x = random(Stage.width);
    }
    }
    }
    help?

  2. #2
    Trainee coder Viza's Avatar
    Join Date
    Sep 2006
    Location
    Melbourne, Down under
    Posts
    513
    Change the _y propery to _x (as I've done below)
    PHP Code:
    function moveStars() {
    for (var 
    0j<starsj++) {
    var 
    mc this["star"+j];
    if (
    mc._y<Stage.height) {
    mc._x += mc.speed;
    } else {
    mc._x 1
    mc
    .speed random(maxSpeed-minSpeed)+minSpeed;
    mc._x random(Stage.width);
    }
    }

    Viza.
    Last edited by Viza; 02-08-2008 at 01:40 AM.

  3. #3
    Elvis...who tha f**k is Elvis? phreax's Avatar
    Join Date
    Feb 2001
    Posts
    1,836
    Well, the if check has to be based on x values too:
    Code:
    if (mc._x<Stage._width) {
    instead of
    Code:
    if (mc._y<Stage.height) {
    ...also change this:
    Code:
    mc._x = random(Stage.width);
    to this:
    Code:
    mc._y = random(Stage._height);
    In flash:
    x means moving sideways
    y means moving up and down
    Streets Of Poker - Heads-Up Texas Hold'em Poker Game

  4. #4
    Senior Member
    Join Date
    Nov 2007
    Location
    California
    Posts
    118
    It still doesn't seem to work... In my original code, before you posted, I already changed x to y and it would work for a few seconds, then stop recreating stars. What am I to do?

  5. #5
    Senior Member
    Join Date
    Nov 2007
    Location
    California
    Posts
    118
    I've tried a few more things, but not getting much progress. Help Plezzz?

  6. #6
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    Post your new code so we can see what you've changed.

    Squize.

  7. #7
    Senior Member
    Join Date
    Nov 2007
    Location
    California
    Posts
    118
    So basically, the original code I posted works fine, but if I change the x to y thing (obvious thing I know) it works temporarily and suddenly doesn't create any more random stars (bold is changed):
    PHP Code:
    var stars 40;
    var 
    maxSpeed 16;
    var 
    minSpeed 2;

    for (var 
    0i<starsi++) {
    var 
    mc this.attachMovie("star""star"+ii);
    mc._x random(Stage.width);
    mc._y random(Stage.height);
    mc.speed random(maxSpeed-minSpeed)+minSpeed;
    var 
    size random(2)+0.6*(random(4));
    mc._width size;
    mc._height size;
    }


    function 
    moveStars() {
    for (var 
    0j<starsj++) {
    var 
    mc this["star"+j];
    if (
    mc._y<Stage.height) {
    [
    B]mc._x += mc.speed;[/B]
    } else {
    mc._y 1
    mc
    .speed random(maxSpeed-minSpeed)+minSpeed;
    mc._x random(Stage.width);
    }
    }

    So i took what viza and phreax said and adjusted it as well, but it doesn't work period.(I don't think I should post because its up there) I mean there code works, no offense, no errors are made, but it doesn't create any stars (not worthy, not meaning to bash). So, i still am at a dead end. Help?

  8. #8
    Senior Member
    Join Date
    Nov 2007
    Location
    California
    Posts
    118
    Woops tried to bold the code up there, but it thought it was actionscript. ignore that, and thats what I changed

  9. #9
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Your moveStars function should look like this:
    PHP Code:
    function moveStars() {
        for (var 
    0j<starsj++) {
            var 
    mc this["star"+j];
            if (
    mc._x Stage.width) {
                
    mc._x += mc.speed;
            } else {
                
    mc._x 1;
                
    mc.speed random(maxSpeed-minSpeed) + minSpeed;
                
    mc._y random(Stage.height);
            }
        }


  10. #10
    Senior Member
    Join Date
    Nov 2007
    Location
    California
    Posts
    118
    Tonypa, it works great, but what if I wanted my stars to go the opposite way. Can you teach me how to distinguish the height and the location of stars so I can make my stars go left right, up, or down?

  11. #11
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Making the stars move in every direction is possible:

    Add direction variable when star is created
    PHP Code:
    mc.speed random(maxSpeed-minSpeed)+minSpeed;
    mc.direction 1;
    if(
    random(2) == 1){
        
    mc.direction = -1;

    Move star by adding direction to speed:
    PHP Code:
    mc._x += mc.speed mc.direction
    You also need to check if star has gone out of stage from both ends:
    PHP Code:
    if (mc._x Stage.width && mc._x 0) { 
         
    mc._x += mc.speed mc.direction;

    And reset the star to other side based on its direction:
    PHP Code:
    else {
         if(
    mc.direction == 1){
              
    mc._x 1;
         }else{
              
    mc._x Stage.width;
         }
         
    mc.speed random(maxSpeed-minSpeed) + minSpeed
         
    mc._y random(Stage.height); 


  12. #12
    Senior Member
    Join Date
    Nov 2007
    Location
    California
    Posts
    118
    Making the stars move in every direction is possible:
    I wasn't really asking to move them in all direction, just be able to change a but of code to make them move in the direction I want. What you posted ^^^ probably works just as well. So, how can I use your direction variable to change my direction? Can you break down what variables I change to move them in either north, south, east, or west?

  13. #13
    Senior Member
    Join Date
    Nov 2007
    Location
    California
    Posts
    118
    Tonypa. I still am confused. How do I move the clouds up rather than down, or right rather than left?

  14. #14
    learning
    Join Date
    Jan 2008
    Posts
    16
    Wait where is this script, the frame or the star? And what is the star's instance name?
    + +Chuck Norris = Awesome

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