home   |   about   |   time   |   appearance   |   space   |   other   |   tips & tricks    

Random Hamsters

This is the last example from the "Randomly Generated Time" section of the website. The new stuff in this page consists in the multiple objects that need to be coordinated, and the introduction of one Movie Clip designed specially to "think" the entire Clip.

First let's explain the making of one of the three Movie Clips with the hamster (same object, just different instances). You need one hamster (you can use our .png image) and an oval shape as the hole (the hamster has to come out from somewhere). Place them on different layers in the order mentioned. To create the impression that the hamster comes out, we need a Mask Layer for the hamster. The important part of the masked layer is the bottom (copy and paste in the same place the oval), in the middle and upper side just draw a rectangle or something, and be sure the image is fully visible.

Check out how we did the mask:

Next convert the image into a Movie Clip, open it for edit and then convert again the image into a Movie Clip which will be used in the motion. In the first frame place the hamster under the hole and few frame further place the hamster in the most exposed position. Create a transition between these frames (Tween Motion). Keep the image in the same place for a few frames (so the hamster is visible) then invert the process so the little guy goes back where he came from. Stop the current Movie Clip at the first frame so we can trigger the event whenever we want.

 

In the Main Stage drag three instances of the Movie Clips with the Hamster's motion in and out of the hole. Name the three object "h1","h2","h3" (click the Clips and open the Properties Panel). As we mentioned earlier, we won't use any code "on" these objects, another Symbol will do the looping and will determine which hamster to pop out. We wrote "Brain" on the stage and converted the text into a Movie Clip. You can use any Movie Clip because we only need the special functions: "onClipEvent(load)" and "onClipEvent(enterFrame)".

Here is the code:

onClipEvent( load ) {
	var effect:Boolean = false;
	var ok, Value:Number;
	Value = 30;
}

onClipEvent( enterFrame ) {
	ok = random(Value);
	if( ok == 1 ) {
		_parent.h1.hamster.play(); //relative path to the desired Clip
	}
	
	if( ok == 2 ) {
		_parent.h2.hamster.play();
	}

	if( ok == 3 ) {
		_parent.h3.hamster.play();
	}
}

After declaring the variables (the same we also used in the previous chapters) inside the "onClipEvent(load)" function, we established the number of possibilities for the randomly generated number at the value 30.That means there are 1/30 chances for each desired number to appear. Here are the differences between this example and the previous one. The looping is done by a different Movie Clip, one that is not visually implicated in the process. We recommend this technique in any flash work: use different frames and object for different tasks.

The events we are triggering have the same structure as the ones presented in the Lightning Effect. The difference in this chapter is that we are generating random numbers even if the Movie Clips are playing. And we test if the obtained number is one of the three numbers assigned to each instance. If "ok" has the value 1, we play the first Hamster Clip and so on. But what happens if we generate the value 1 while the first clip is playing? Absolutely nothing, because we tell the Clip to play while it is already playing. So the number of times the hamster appears will be smaller than the number of times we generate the value "1". See for yourself in the Movie at the top of the page.

Note: We added a few extra elements in the source file, to make the Movie look cooler, so just ignore them and stick to the explanations presented above.

Hope you found useful our four chapters about how you can link Time and randomly generated natural numbers. Of course there are many approaches for this issue so if you have a new one, you have a contact button on the top of the page and you know what to do with it.

Check out a useful implementation in this Bonus Example. Try to punch all the hamsters.

The next few chapters will show how random numbers can determine changes of the Space coordinates in some cool flash effects.

Download .ZIP Download .FLA
< Lighting EffectBonus Game >