Flash Game Tutorial: Adding Randomly Falling Objects

Randomly falling objects can be one of the main elements of a flash game. You can create randomly falling objects that the player has to collect or they can be objects that must be avoided. Regardless of whether you want to make objects that give you points, make you lose health or enable you to fly, you can use this method to create them.

I will show you how to create randomly falling stars for your game, using two classes, one for your game character: Player, and one for the Star movie clip: Star. This tutorial uses object oriented programming and assumes you have a main class, Player, that controls your game character. If you have already created a flash file based on my tutorial Move Character with Arrow Keys, you can open that and work from there.

Otherwise create a Flash file: File> New> Flash File (ActionScript 2)

Draw the Object in Your Flash File

Set the stage to 450px wide by 300px tall in the properties window.

Draw whatever object you would like to fall across the screen. If you want to follow the tutorial, draw a star.

Convert the image to a movie clip: Modify> Convert to Symbol

Name it Star (or whatever your object is), check the box next to export for actionscript, and put Star for the Identifier and Class.

Click okay and you are back to the stage.

Create a Star Class

Now create a new actionscript file: File> New> ActionScript File

Save it as Star and make sure it is in the same location as your .fla file.

Here is the code for our Star class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Star extends MovieClip
{
    var speed;

    function onLoad()
    {
        speed = Math.random()*10+5;
        _x = Math.random()*400+50;
        _y = -10;
    }

    function onEnterFrame()
    {
        _y += speed;
        _rotation -= 5;

        if(_y > 310)
        {
            this.removeMovieClip();
        }
    }

}

In the Star class we want to define how a Star movie clip will behave when it is attached to the stage.

Line 3: We name a variable speed to control how fast the star will move.
Line 7: Under onLoad() we set the speed equal to a random number between 1 and 10 plus five. The minimum speed will be 6 and the max will be 15.
Line 8: We position it randomly on the x axis, taking into account the width of our stage.
Line 9: We position it 10 pixels above the stage so it does not appear suddenly on the stage, but rather falls from above.
Line 14-15: Under onEnterFrame() we give it speed in the y direction and a counter clockwise rotation so that the stars slowly spin as they fall.
Line 17-20: When the star reaches ten pixels past the end of our stage, it is removed.

That is it for our star class. But we still need to attach the stars to the stage. We will do that using a timer in our Player class.

Create a Player Class

You will need a movie clip that is linked to a Player class, in order to create random falling objects with this method. If you do not already have a movie clip linked to a Player class, follow the directions below and visit Move Character with Arrow Keys if you would like to program arrow key movement for your game character.

To Create Player Movie Clip

Draw your game character. Select the image and go to Modify> Convert to Symbol. Name it Player. Select Movie Clip as the type, under Linkage check the box for export for actionscript and put Player as the Identifier and the Class. Click okay to return to the stage.

Now create a new actionscript file: File> New> Actionscript File. Go to File>Save Name it Player and save it in the same folder as your .fla file. Or open your existing player class.

Here is the code for our Player class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Player extends MovieClip
{
    var starTimer;

    function onLoad()
    {
        starTimer = 0;

    }

    function onEnterFrame()
    {
        starTimer += 1;
        if(starTimer > 5)
        {
            starTimer = 0;
            var star = _root.attachMovie("Star", "Star" + _root.getNextHighestDepth(), _root.getNextHighestDepth());
        }

    }
}

Line 3: We define a vaiable, starTimer, that will keep track of how often a star is placed on the stage.
Line 7: Under onLoad() the starTimer is set to zero.
Line 13: Under onEnterFrame() we start the timer and have it increase by increments of one.
Line 14-18: If the starTimer is greater than five, the timer will be reset and an instance of our movie clip Star will be added to the stage. The timer runs continually and every time the Star timer reaches 5 a star is added. You can increase the timer to decrease the number of stars, or decrease the timer to increase the number of stars appearing on the stage.

Now the stars will be attached to the stage through the player class, so you can delete the star movie clip from the stage in your .fla file. Make sure you have that game character that is linked to the Player class on the stage though, or no stars will appear. If you are not making a game, but still want to use this method to create randomly falling objects, you can make an empty or invisible movie clip on the stage and link it to a Player class with the timer code.

That is it for creating randomly falling objects.

Here are our falling stars:

Flash Tutorial: Create Random Falling Objects

Subscribe and Share:
  • RSS
  • Twitter
  • StumbleUpon
  • Digg
  • Facebook

7 thoughts on “Flash Game Tutorial: Adding Randomly Falling Objects

  1. I keep getting the message “the class or interface ‘star’ could not be loaded. Help!

  2. why is it if i run the animation it will always prompt error saying code cannot be nested. how will i solved this one? please help.

  3. Hi! I follow this tutorial and work perfectly thank you so much for it!
    I see your games and I want to ask you if you can add a tutorial that teach us to do the same that you do on your Starkit.
    Detect the catching of the stars and get score for it, catch “bad” stuff and lose score, etc.
    Hopes you can :) !

  4. Hi Alden!

    I am glad to hear the tutorial was useful! I will definitely consider making a full “how to make this game” tutorial for Starkit. Thanks for the request! :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>