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

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;
		}

	}
}

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 > 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");
        }
    }
}

How to use URLVariables() in Flash AS3?

Using URLVariables in Flash or Flex we can send and receive data from server he is the example

This is the PHP Code nested in the server
Create a PHP file and place it in the server

<?php
 $email=$_POST['email'];
 $password=$_POST['password'];

echo "email=".$_POST['email']."&amp;password=".$password;
?>

Here is the code in Flash/Flex

//create URLRequest instace withe the target URL
var request:URLRequest = new URLRequest("http://www.example.com/data.php");

//create instance of the class
var variables:URLVariables = new URLVariables();

variables.email = druva.flash@gmail.com;
variables.password = 'dontexpectit';

//add the data to the URLRequest
request.data = variables;

//Choose a method as POST
request.method = URLRequestMethod.POST;

var loader:URLLoader = new URLLoader();
//Create EventListener
loader.addEventListener(Event.COMPLETE, handleComplete);

//send the request with URLLoader()
loader.load(request);

 function handleComplete(event:Event)  {
var loader:URLLoader = URLLoader(event.target);
var vars:URLVariables = new URLVariables(loader.data);

//Read data for the result
trace("vars.email: "+vars.email);
trace("vars.password: "+vars.password);
 }

Create Color Wheel using Flex (Actionscript) AS3

Working on color wheel i hope it looks good.
Cick to view working sample

Create Color Picker like Photoshop Color Picker using Flex and Actionscript 3 (AS3)

Recently started making a color picker which is similar to photoshop color picker

Create Text Effects in Flash and AS3 (Actionscript 3.0)

Just playing with Text and this is my first version of text effects

This movie requires Flash Player 9

sample 0.1

Validate Email without RegExp in AS2 and AS3 in Flash

The below code shows how to use the class


import druva.emailValidator;

trace('druva.flash@gmail.com &gt; '+emailValidator.isValid('druva.flash@gmail.com'));
// true
trace('druva.flash@gmail &gt; '+emailValidator.isValid('druva.flash@gmail'));
// false
trace('druva.@.com &gt; '+emailValidator.isValid('druva.@.com'));
//false
trace('druva.@gmail.com &gt; '+emailValidator.isValid('druva.@gmail.com'));
//true
trace('.aa@gmailcom &gt; '+emailValidator.isValid('.aa@gmailcom'));
//false

This is the actual class for validation


package druva {
  import flash.display.Sprite;

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

    public static function isValid(em:String):Boolean {

        var sEmail:String = new String(em);
        var validEmail:Boolean = true;
        var numDotPos:int = sEmail.indexOf(&quot;@&quot;);
        var nDotIndex:int = sEmail.lastIndexOf(&quot;.&quot;);
        if(numDotPos == -1 || nDotIndex == -1) {
          validEmail = false;
        }
        if(!(numDotPos &gt; 0)) {
          validEmail = false;
        }
          if(!(nDotIndex &gt; numDotPos)) {
        validEmail = false;
        }
        if(!(numDotPos &lt; sEmail.length - 1) || !(nDotIndex &gt; numDotPos + 1)) {
          validEmail = false;
        }
        return validEmail;
    }

  }
}