Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Random Movieclip Duplicates

  1. #1
    Join Date
    Feb 2008
    Posts
    90
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Random Movieclip Duplicates

    I am trying to create a game in which once space bar is pressed to initiate the game i want a movieclip to randomly generate on the y axis and tween across the screen, a few seconds after i want the same movieclip to generate at another random location on the y axis. When the player hits any of these movieclips i want all of them to stop in their tracks.
    If anyone could shed any light on this i would really appreciate it.

  2. #2
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Well a lot of it will depend on how you have the rest set up. for basics, though we will make assumptions... for your MC will be called "enemy"

    Code:
    onClipEvent (load) {
     function reset(){
     this._x=600;
     this._y=random(200)+100;
     enemySpeed=random(4)+1;
     }
     reset();
    }
    This code will take your enemy MC, and make it appear at random along the y axis of your stage. It will also randomize the speed it will (eventually) travel across the stage (in pixels)

    Next is the number for loop:

    Code:
    numEnemy=3;
    for (i=2; i<=numEnemy; i++){
     enemy1.duplicateMovieClip( "enemy"+i, i+100 );
    }
    This will take the enemy MC and duplicate it, giving each a specific name enemy1, enemy2, etc.. the numEnemy is the number of enemies on the stage at one time, you can change this as you need.

    Then to make them move, we need something to trigger (press space, etc.)

    Code:
    onClipEvent (enterFrame) {
     if (Key.isDown(Key.SPACE)){
     this._x-=enemySpeed+3;
     } else {
     this._x-=enemySpeed;
     }
     if (this._x<-10) {
     reset();
     }
    }
    Again, depending on your set up and other factors, this will duplicate and move the enemies around. Your collision detection methods will determine how you stop the MCs, a for loop checking for collision then an all stop code should suffice, but this should get you started.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  3. #3
    Join Date
    Feb 2008
    Posts
    90
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default

    Thank you for the quick response. I was able to implement the first part of your explanation and make the enemy appear randomly on the y axis. However im not sure where to put the other two parts of code, having to do with the number of enemies and the start game button. Currently to start the game i just have a button that says
    on (keyPress"<Space>"){
    play();
    }

  4. #4
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Do you have something to interact with the enemies? A hero or ship that will go in the opposite direction as the enemies?

    The start code will go on the main time line, in the Actions layer, for example, the others go on the MCs themselves.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  5. #5
    Join Date
    Feb 2008
    Posts
    90
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default

    im afraid i just dont understand or maybe am just not thinking in the right mind set. I have a ship that is coded like this:
    onClipEvent (load) {
    power = 1;
    yspeed = 0;
    xspeed = 0;
    gravity = .5;
    }
    onClipEvent (enterFrame) {
    if (Key.isDown(Key.SPACE)) {
    yspeed -= power;
    }
    yspeed += gravity;
    _y += yspeed;

    }
    ---
    And to start the game i have the ship as a button, and once space is pressed it goes to the second frame, where the enemy is randomly generated.

  6. #6
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    okay... on the enemy MC:

    Code:
    onClipEvent (load) {
     function reset(){
     this._x=600;
     this._y=random(200)+100;
     enemySpeed=random(4)+1;
     }
     reset();
    }
    on main timeline (usually first frame):

    Code:
    numEnemy=3;
    for (i=2; i<=numEnemy; i++){
     enemy1.duplicateMovieClip( "enemy"+i, i+100 );
    }
    on your ship (assuming it's name is hero)

    Code:
    onClipEvent (enterFrame) {
     if (Key.isDown(Key.SPACE)){
     this._x-=enemySpeed+hero.yspeed;
     } else {
     this._x-=enemySpeed;
     }
     if (this._x<-10) {
     reset();
     }
    }
    If you can't get it to work, zip up your fla and I will take a look when I get home in a few hours.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  7. #7
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    The start code will go on the main time line, in the Actions layer, for example, the others go on the MCs themselves.
    tsk tsk tsk, lol

    If you're creating a game, it might be beneficial to keep all of your code together. Otherwise, it gets to be a real pain to track down where bugs are when they occur.

  8. #8
    Join Date
    Feb 2008
    Posts
    90
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default

    haha well i tried to add stuff but im not sure if i should replace my older code..or add this newer stuff in...and just all around lost. Im going to include my fla file in a zip and would really appreciate it if you could look at it.
    P.S. The game might look familiar as i know i sent it to you months ago. However, since then i have improved the organization of it significantly..i hope.
    Thank you very much.

  9. #9
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    I agree, but if OP is having troubles with duplicating MCs, I am also assuming using Flash 7 or 8 and AS2, and using key presses to activate buttons?? I imagine the code is already all over the place. getting it to work is probably more important at this point to the OP than clean code, which can be done after game is working

    Edit:
    Oh my! I remember this game! LOL!! I will get on it when I get home!
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  10. #10
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    Quote Originally Posted by BLiZZaRD View Post
    I agree, but if OP is having troubles with duplicating MCs, I am also assuming using Flash 7 or 8 and AS2, and using key presses to activate buttons?? I imagine the code is already all over the place. getting it to work is probably more important at this point to the OP than clean code, which can be done after game is working
    Good point.

Bookmarks

Posting Permissions

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