How to Create Falling Hearts with Flash CS4 Action Script AS3

Posted by druva | Flash, as3, utils | Saturday 6 February 2010 1:45 pm

This code show how to create falling hearts with flash cs4

you can also use the code for cs3
create a movieclip with a heart in your library

package {

	import flash.display.MovieClip;
	import flash.events.*;

	public class fallingheart extends MovieClip {
		public function fallingheart() {
			for (var i = 0; i < 30; i ++) {
				var h:heart = new heart();
				h.x=Math.round(Math.random()*stage.stageWidth);
				h.y=Math.round(Math.random()*stage.stageHeight);
				h.scaleX = h.scaleY = .4+ ((Math.random()*1)/2);
				h.swing = 15 + (Math.random()*15);
				h.n=Math.random()*2;
				h.speed = 1 + (Math.random()*4);

				addChild(h);
				h.addEventListener(Event.ENTER_FRAME, onEnterLoop);
				//h.onEnterFrame = fall;
			}
		}

		//--  you can also use timer to call this
		public function onEnterLoop(e:Event) {
			trace(stage.stageHeight);
			var h:heart=e.currentTarget as heart;
			h.y+=h.speed;
			h.n+=0.25;
			h.rotation=Math.cos(h.n)*h.swing;
			if (h.y>stage.stageHeight) {
				h.x=Math.round(Math.random()*stage.stageWidth);
				h.y=-50;
			}
		}
	}
}

This movie requires Flash Player 9