CS4 spring Motion using action script


var springX:Number=0;
var springY:Number=0;
var spring:Number=.95;
var radius:Number=300;

ball.addEventListener(Event.ENTER_FRAME, Event_ENTER_FRAME);
ball.x=ball.y=Math.random()*300;
stage.addEventListener(MouseEvent.MOUSE_DOWN, onClick);

function onClick(event:Event):void {
	ball.x=mouseX;
	ball.y=mouseY;
}

function Event_ENTER_FRAME(event:Event):void {
	var ax:Number=0;
	var ay:Number=0;
	ball.x+= (springX += ((radius - (2 * ball.x)) * .1));
	ball.y+= (springY += ((radius - (2 * ball.y)) * .1))
	springX*=spring;
	springY*=spring;
}

This movie requires Flash Player 9

Flash CS4 TextElement TextBlock FontDescription TextBlock TextLine

This code show how to use TextElement, TextBlock, FontDescription, TextBlock, TextLine

package druva{
import flash.display.Sprite;
import flash.text.engine.*;

public class TextTest extends Sprite {

public function TextTest() {

for (var j:int=0; j<=10; j++) {
var myString:String="Flashallys";
var myFormat:ElementFormat = new ElementFormat();

var myFontDesc:FontDescription=new FontDescription('Georgia','normal','italic','device');
myFormat.fontSize=2+2*j;
myFormat.fontDescription=myFontDesc;
myFormat.color = Math.random() * 0xFFFFEE;

var textElement:TextElement=new TextElement(myString, myFormat);
var textBlock:TextBlock = new TextBlock();
textBlock.content=textElement;

var myTextLine:TextLine=textBlock.createTextLine(null,300);

myTextLine.x = 30+(3*(j*(j+1)/2));
myTextLine.y = 150;

addChild(myTextLine);
myTextLine.addEventListener(Event.ENTER_FRAME, onLoop);
}

}
import flash.events.*;
private function onLoop(e:Event) {
e.currentTarget.rotationY += 3;
e.currentTarget.rotation = mouseX;
}
}
}

This movie requires Flash Player 9

Flash CS4 PerspectiveProjection projectionCenter with action script

This code show how to use PerspectiveProjection, projectionCenter tranform, point

var img:Bitmap=new Bitmap(new heart(158, 58));
img.x=-158/2;
img.y=-58/2;
var holder:Sprite=new Sprite();
addChild(holder);
holder.x=stage.stageWidth/2;
holder.y=stage.stageHeight/2;
holder.addChild(img);

var pp:PerspectiveProjection=new PerspectiveProjection();
pp.fieldOfView = 120
pp.projectionCenter=new Point(holder.x,holder.y);
holder.transform.perspectiveProjection=pp;

function swingImage(e:Event):void {
	holder.rotationY+=3;
}
this.addEventListener(Event.ENTER_FRAME, swingImage);

This movie requires Flash Player 9

How to Create Falling Hearts with Flash CS4 Action Script AS3

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

Get Even or Odd – getParity using Action Script AS

The below code shows how to use the class


import druva.NumberUtil;

trace('500', NumberUtil.getParity(500));
// true
trace('489', NumberUtil.getParity(489));
// false
trace('5', NumberUtil.getParity(5));
//false
trace('1', NumberUtil.getParity(1));
//true
trace('400', NumberUtil.getParity(400));
//false

This is the actual class


package druva {
  import flash.display.Sprite;

  public class NumberUtil extends Sprite{
    public function NumberUtil(){
    }

    public static function getParity(num:Number):String {
		return (num % 2) ? 'odd' : 'even';
	}

  }
}

How to Convert TextField to Bitmap using Flash and AS3

Convert TextField to Bitmap

package {
	import flash.display.*;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;

	public class BitmapUtils extends Sprite {
		public function BitmapUtils() {
			addChild(tf2bm('Druva'));
		}
		public function tf2bm(str:String) {
			var fmt:TextFormat;
			var bmd:BitmapData;
			var bm:Bitmap;
			var tf:TextField;

			fmt = new TextFormat();
			fmt.font='Verdana';
			fmt.size=30;

			tf = new TextField();
			tf.text=str;
			tf.setTextFormat(fmt);
			tf.autoSize=TextFieldAutoSize.LEFT;
			bmd=new BitmapData(tf.width,tf.height,true,0);
			bmd.draw(tf);
			bm=new Bitmap(bmd);
			bm.smoothing=true;
			return bm
		}
	}
}

get Random Number Between using Actionscipt AS3

The below code shows how to use the class


import druva.NumberUtil;
trace(NumberUtil.getRand(5, 10));

This is the actual class for validation


package druva{
	import flash.display.Sprite;

	public class NumberUtil extends Sprite {
		public function NumberUtil() {
		}

		public static function getRand(min:Number, max:Number):Number {
			return Math.floor(Math.random()*(max+1-min))+min;
		}

	}
}

BlurFilter for Images using Flash ActionScript

package {

import flash.utils.*;
import flash.display.*;
import flash.net.*;
import flash.geom.*;
import flash.events.*;
import flash.filters.*;

public class DocumentClass_blurfilter extends MovieClip {

private var urlLoader:URLLoader = new URLLoader();
private var mc:MovieClip = new MovieClip();

public function DocumentClass_blurfilter() {
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load(new URLRequest('http://www.flashallys.com/blog/blogsamples/rotary-phone1.jpg'));
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
blurX.addEventListener(Event.CHANGE , onChange);
blurY.addEventListener(Event.CHANGE , onChange);
}

private function completeHandler(event:Event):void {
var loader:Loader = new Loader();
loader.loadBytes(urlLoader.data);

mc.addChild(loader);
addChild(mc);

blurX.value = 10;
blurY.value = 10;

blurX.minimum = 0;
blurY.minimum = 0;

blurX.maximum = 100;
blurY.maximum = 100;

Blur_Filter();
}

private function onChange(e:Event) {
Blur_Filter();
}

private function Blur_Filter() {
var blur:BlurFilter = new BlurFilter();
blur.blurX=blurX.value;
blur.blurY=blurY.value;
blur.quality=BitmapFilterQuality.LOW;
mc.filters=[blur];
}
}
}

(more…)

perlinNoise using Flash ActionScript

Simple Example to show how to use perlinNoise in Flash ActionScript

In the below example the stage is divided into two parts with two different effects

example

import flash.display.*;
import flash.events.Event;
import flash.geom.Point;

var _bitmap1:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight/2, true, 0xCCCCCC);
var _bitmap2:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight/2, true, 0xCCCCCC);

function perlinNoise() {
addChild(new Bitmap(_bitmap1));
_bitmap1.perlinNoise(10, 10, 2, 50, false, true,1, true);

var b:Bitmap = new Bitmap(_bitmap2);
b.y = stage.stageHeight/2;
addChild(b)
}

perlinNoise();

var t:Timer = new Timer(50)
t.addEventListener(TimerEvent.TIMER , onTimer);
var Size = 0;
var xPos = 0;
var yPos = 0;
function onTimer (e:TimerEvent) {
var point:Point=new Point(++xPos,++yPos);
_bitmap1.perlinNoise(10, 10, 2, 50, false, true,1, true, [point, point]);
++Size;
_bitmap2.perlinNoise(Size,Size, 2, 50, false, true,1, true, [point, point]);
if(Size &gt; 50){
Size = 0;
}
}

t.start();

(more…)

Load Date from XML File using flash actionscript AS3

Simple Example to show how to load xml

package
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    public class DocumentClass extends Sprite
    {
        private var loader:URLLoader;
        private var xmlPath:URLRequest;

        public function DocumentClass()
        {
	 xmlPath = new URLRequest("http://server.com/xml.xml")
	loader = new URLLoader(xmlPath);
            loader.addEventListener(Event.COMPLETE, completeListener);
            loader.addEventListener(ProgressEvent.PROGRESS, progressListener);
        }

        private function completeListener(event:Event):void
        {
            trace(" all done loading " + loader.data + " and here's the xml file we loaded ");
        }

        private function progressListener(event:Event):void
        {
            trace(" loading.... " + loader.bytesLoaded + " / " + loader.bytesTotal + " bytes");
        }
    }
}