Moshe
Moshe

Reputation: 58097

Getting started with a simple Flash game

I'd like to make a simple game where "discs" fall from the top of the screen and the user must catch them. I have a MovieClip which I want to resize to one of three randomly chosen sizes.

As I see it, there are four things that must be done.

  1. Create and size the MovieClip

  2. Position the MovieClip

  3. Make the MovieClip fall

  4. Determine when it is finished "falling" and see if the user has "caught" it.

My question is: How do I create, size and position the MovieClip? I've given it an identifier of "disc". Now what? Do I make an ENTER_FRAME event and do my creation there? How do I move the disc downwards? Do I use tweens, something else?

I'm primarily asking this as a sanity check.

Upvotes: 0

Views: 323

Answers (3)

dubbeat
dubbeat

Reputation: 7857

You might want to look at programming particles.

http://r3dux.org/2010/01/actionscript-3-0-particle-systems-3-rain-effect/

At a very high level what you would do is.

You need to create a disc class.

You could give this class some variable properties like width, height,x etc.

In an enter frame in your main class you would add an enterframe function that creates new instances of disc passing it random values for each property.

Each instance of disc could also have its own entframe which increases its y position until it reaches the bottom of the screen. The disc would then remove its self from the stage. You could use an easing function passing it a random number to determine the rate at which its falls.

Say if its y position is greater than the stage height, remove the disc. If the user catches it (using a hit test maybe) also remove it.

It really recommend having a look at that link I posted.

Upvotes: 0

loxxy
loxxy

Reputation: 13151

Assuming that you have MovieClips named 'disc' & 'userHand' exported into actionscript, I'll summarize it in the foll way:

  • Generate the no of discs & Randomize their location. Start with something like:
var n:int = 30; //Total no of Discs

for(i:int=0;i<n;i++) 
   {
   var mc:disc = new disc();
   mc.x=Math.random()*stage.width(); //to scatter the discs across the stage 
   mc.y=-mc.height;  //initially hide out a disc
   addchild(mc);
   }
  • Also add the Movie clip 'userhand' to stage.
  • Add enterframe handler function.
  • Fill in the enterframe handler to update (keep increasing) the y position of each disc.
  • Use hitTestObject() in the enterframe handler to determine hit among each 'disc' & 'userHand'.
  • Reset position of all the 'disc' clips which fall off the screen to random positions as initially.

Upvotes: 0

jdecuyper
jdecuyper

Reputation: 3963

I would use some kind of factory class that would be responsible for dropping random discs from the top of the stage.

Beside what you correctly mentioned, you will also need to:

  • define if the speed for falling is constant or not, you may need some acceleration tween. To move the objects downwards, you could use a native tween method, you will need to apply it to every disk that gets dropped.

  • define where the disk will start falling, it can be random or always from the same place.

  • you can find out if two objects are colliding using the AS3 hitTestObject method which belongs to the DisplayObject class.

  • you factory class could have a start() and stop() method. Once start() is fired, an infinite or ENTER_FRAME loop is started and disks start falling. If you want to create disks at a specific rate you could combine your loop with a timer to run code on a defined interval. For instance, every 3 seconds create 10 disks (using main loop) and drop them onto the stage.

Upvotes: 1

Related Questions