• home
  • forum
  • my
  • kt
  • download
  • Efficient Collision Detection with Dynamically Created MC`s

    Author: 2007-06-06 14:56:19 From:

    In this tutorial we will check for collisions between MC¡¯s (Movie Clips) that are created through actionscript. The way we will be doing it won¡¯t lag very much compared to a lot of the other ways you can do this and I will explain why later. I am using Adobe Flash CS3 but Macromedia Flash 8 and Flash MX 2004 will work as well. If you are using Flash CS3 make sure you select a AS 2.0 file.

    Making the Movie Clips

    Since we will be attaching the bullets through actionscript delete the one you just made of the stage. Put the wall up close to the top of the stage since we will be putting the bullets down the bottom. Open the library (Ctrl+L or Windows>Library) and right click on the bullet MC. Select ¡®Linkage¡­¡¯. A new window should appear.

    Linking the MC1

    Make sure the box reading ¡®Export for Actionscript¡¯ is checked. Enter ¡®bullet¡¯ into the identifier box. Here is what it should look like. (The Flash CS3 Linkage box looks different to the Flash 8 one so I put an example for each).

    Flash CS3
    Linking the MC2

    Flash 8
    Linking the MC2

    Give the wall an instance name of ¡®wall¡¯.

    Now paste this script onto the frame.

    The first two lines are declaring two variables. The first one is an array called bullets. This is where all the bullets are being stored when they are created. There other is a number called i, which increases everytime a bullet is created.

    ¡®onEnterFrame¡¯ tells flash to run the code between the curly braces every time the frame is entered.

    This is an if statement that checks if a random number between 0 and 9 is equal to 0. If it is it will attach the MC ¡®bullet¡¯ to the stage with an instance name of ¡®b¡¯+i. So if i is equal to 0 its instance name will be b0, if i is 12 than its instance name will be b12. It is setting its _x coordinates to a random number from 0 to the stage width (so its _x could be anywhere on the stage) and its setting its _y coordinates to the stage height.
    It is adding the value of i to the end of the bullets array. bullets.length equals how many numbers are in the bullets array. After it does all that i increases by one so that none of the bullets have the same name.

    This is what makes our script run faster. The most common way is ¡®for(j=0;j<i;j++)¡¯. So if 165 bullets had been created this script would run 165 times. But since not all of the bullets are going to be on the stage (they get deleted when they hit the wall) this is making flash run the script more than it has to. Since this code only runs of j is less than b bullets.length, and when a bullet gets deleted so does its number in the array, flash only runs the code as many times as it needs to. Sorry if I didn¡¯t explain that very well. If you still need me to explain it better just post below.
    The 2nd line is making a variable called n equal to bullets[j]. If j is equal to 12, and the 12 value in the bullets array is equal to 34 then n is going to equal 34. This just saves us from writing bullets[j] over and over and lets use put ¡®n¡¯ instead.
    The next line is telling each bullet on the stages _y to decrease by 20, so it moves up. If n is equal to 54 than _root[¡±b¡±+n] means _root.b54.

    This checks if any of the bullets are hitting the wall. If they are they are removed with the removeMovieClip function. The second last line is removing that bullet from the bullets array so that flash doesn¡¯t run the script for that bullet anymore. We are using the splice function to do this. The first parameter is the value you want flash to start deleting from. The next parameter is how many values after that you want to delete. So if you put 12 flash would delete 12 values from your array, starting from the value you put.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      3D (20)
      Math Physics (14)
      3rd Party (5)
      Navigation (60)
      Actionscripting (26)
      Optimization (16)
      Animation (32)
      Projector (9)
      Audio (46)
      Special Effects (112)
      Backend (25)
      Text Effects (65)
      Drawing (18)
      Tips and Techniques (41)
      Dynamic Content (25)
      Tricks (6)
      Games (66)
      Utilities (19)
      Getting Started (71)
      Video (10)
      Interactivity (21)
      Web Design (22)

    New

    Hot