Thursday, October 16, 2008

Use class with addEvenListener

We used delegate or Proxy in actionscript 2.0 when we need to parse parameter to a function click or other event with button.

Now in Actionscipt 3.0 how can we do with this issues?
Ans:We will assign a new class to addEventListener and parse parameter into class method.

the Example like this.
1. Create new as file and save its name buttonEvent.as
2. Write this code
package
{
import flash.events.MouseEvent;
import flash.display.Sprite;
public class buttonEvent extends Sprite
{
var val:String;
public function buttonEvent()
{


}

public var setParam(param:String)
{
val=param;
}
public function clickFn(m:MouseEvent):void
{
//the code When click on this button
trace(val);//Hello All Flash BadAss!
}
public function overFn(m:MouseEvent):void
{
//the code When Roll Over on this button
}
public function outFn(m:MouseEvent):void
{
//the code When Roll Out
}
}
}


3. on main time line add tis code
import buttonEvent;
var bt:buttonEvent = new buttonEvent();
var myBt:Button = new Button();
myBt.addEventListener(MouseEvent.CLICK,bt.clickFn);
myBt.addEventListener(MouseEvent.MOUSE_OVER,bt.overFn);
myBt.addEventListener(MouseEvent.MOUSE_OUT
,bt.outFn);
//and then assign any value that we need to use with the button
bt.setParam("Hello All Flash BadAss!");

Good luck!

Hudsadin keox

Saturday, September 13, 2008

How to call stage from class!

This topic will talk shortly about the stage class. When we create our new class and need to access to stage class (Main timeline) example "stage.stageWidth"we will see the error "cannot access to null property" or some thing else -*-.I use some easy tip to access to stage class .

code!
package
{
import flash.display.Stage;
public class flashBadass
{
private var __stage:Stage;
public functon flashBadass(st:Stage)
{
__stage=st;
trace(__stage.stageWidth);///easyly but for newbies you will headache with this problem.
}
}
}

On timeline when you create new instace of this class you must pass stage to the method.
import flashBadass;
var flBadass:flashBadass = new flashBadass(stage);

I dont know if you had other way to do please tell me.*-*

Hudsadin keox

Thursday, September 04, 2008

Easy loading Xml with as3.0

This project will show you how to load data from Xml file.
Actionscript 3.0 help you work with xml more easier than actionscript 2.0 you will love it.
First create xml file like this



We will load this file store value of attributes "name" and value of "url" feilds.
Start Codding!
- Create new as file name loadXml.as and add this code.
package
{
import flash.xml.*;
import flash.events.*;
import flash.net.*;
public class loadXml
{
public var ImageUrlArr:Array;
public var ImageNameArr:Array;
function loadXml(path:String)
{
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest(path));
}
function LoadXML(e:Event):void
{
var xmlData:XML = new XML ();
xmlData.ignoreWhite = true;
xmlData = new XML (e.target.data);

for (var k in xmlData.pic.url.attribute("name"))
{
ImageNameArr[k]=xmlData.pic.url.attribute("name")[k];
}
for (var i in xmlData.pic.url.text())
{
ImageUrlArr[i]=xmlData.pic.url.text()[i];
}

}
}
}
On main time line add tis code.

var lxml:loadXml = new loadXml ("your xml path");

If you want to use the data you can access this variable "lxml.ImageNameArr" and
lxml.ImageUrlArr.
You can implement this example to a flash image gallery or anything else.
easy job.


Hudsadin keox

Wednesday, September 03, 2008

Fun with Tween Class and some combobox!

This topic will talk about how we can play with Tween Class from flash actionscript 3.0.

1. create a ball and convert it to simbol like this picture.

2. Create new two as file first file save its name simpleTween.as and second for ballExt.as
simpleTween.as is the main control of this project and ballExt.as we will assign this class to ball simbol that we had created befor.

3. add this code to simpleTween.as
package
{
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.events.Event;
import fl.controls.ComboBox;
import flash.display.Stage;
import fl.transitions.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
public class simpleTween extends Sprite
{
private var ball_ins:ball;
private var ballArr:Array = new Array();
private var cb:ComboBox = new ComboBox();
private var __stage:Stage;
private var radian:Number = 120;
private var __time:uint=2;
private var twFn:Function;
private var ballCount:Number=30;
public function simpleTween(st:Stage)
{
__stage = st;
for (var i:Number =0; i
{
ball_ins = new ball();
ballArr[i]=ball_ins;
addChild(ballArr[i]);
ballArr[i].visible = false;

}
/////////////////////create new ball object to stage 30 object if you want more ball change the value of ballCount
var childs:Sprite = new Sprite();
childs.graphics.beginFill(0);
childs.graphics.lineStyle(0, 0);
childs.graphics.drawRect(0, 0, __stage.stageWidth,__stage.stageHeight);
childs.graphics.endFill();
childs.alpha=0;
addChild(childs)
////////////////////////create blank rect when click on it balls will flow on stage
addChild(cb);
cb.setSize(155, 22);
cb.addItem( { label: "Strong.easeIn", data:1 } );
cb.addItem( { label: "Strong.easeOut", data:2 } );
cb.addItem( { label: "Strong.easeInOut", data:3 } );

cb.addItem( { label: "Regular.easeIn", data:4 } );
cb.addItem( { label: "Regular.easeOut", data:5 } );
cb.addItem( { label: "Regular.easeInOut", data:6 } );

cb.addItem( { label: "None.easeIn", data:7 } );
cb.addItem( { label: "None.easeOut", data:8 } );
cb.addItem( { label: "None.easeInOut", data:9 } );

cb.addItem( { label: "Elastic.easeIn", data:10 } );
cb.addItem( { label: "Elastic.easeOut", data:11 } );
cb.addItem( { label: "Elastic.easeInOut", data:12 } );

cb.addItem( { label: "Bounce.easeIn", data:13 } );
cb.addItem( { label: "Bounce.easeOut", data:14 } );
cb.addItem( { label: "Bounce.easeInOut", data:15 } );

cb.addItem( { label: "Back.easeIn", data:ballCount } );
cb.addItem( { label: "Back.easeOut", data:17 } );
cb.addItem( { label: "Back.easeInOut", data:18 } );
cb.selectedIndex = 10;
//////////////assign data to combobox all data are the tween function you will choose the function for animate the ball
twFn = fl.transitions.easing.Elastic.easeOut;
cb.addEventListener(Event.CHANGE, FnSelected);
childs.buttonMode = true;
childs.addEventListener(MouseEvent.CLICK,startAnimFn);
///////add listenner to the blank rect
}
private function FnSelected(e:Event):void { // when the combobox was change the Tween function will change follow the combobox
switch (cb.selectedItem.data)
{
case 1: twFn = fl.transitions.easing.Strong.easeIn; break;
case 2: twFn = fl.transitions.easing.Strong.easeOut; break;
case 3: twFn = fl.transitions.easing.Strong.easeInOut; break;
case 4: twFn = fl.transitions.easing.Regular.easeIn; break;
case 5: twFn = fl.transitions.easing.Regular.easeOut; break;
case 6: twFn = fl.transitions.easing.Regular.easeInOut; break;
case 7: twFn = fl.transitions.easing.None.easeIn; break;
case 8: twFn = fl.transitions.easing.None.easeOut; break;
case 9: twFn = fl.transitions.easing.None.easeInOut; break;
case 10: twFn = fl.transitions.easing.Elastic.easeIn; break;
case 11: twFn = fl.transitions.easing.Elastic.easeOut; break;
case 12: twFn = fl.transitions.easing.Elastic.easeInOut; break;
case 13: twFn = fl.transitions.easing.Bounce.easeIn; break;
case 14: twFn = fl.transitions.easing.Bounce.easeOut; break;
case 15: twFn = fl.transitions.easing.Bounce.easeInOut; break;
case ballCount: twFn = fl.transitions.easing.Back.easeIn; break;
case 17: twFn = fl.transitions.easing.Back.easeOut; break;
case 18: twFn = fl.transitions.easing.Back.easeInOut; break;
}
}
private function startAnimFn(m:MouseEvent)////////when you click on stage(blank rect) this function will called
{
__time=randRange(10,20)/10; //set time of tween class the ball will flow fast or slow cos this line I use random function
for (var i:Number =0; i
{
var sc:Number=randRange(1,10)/10;
var xp:Number=randRange(m.stageX-radian,m.stageX+radian);
var yp:Number=randRange(m.stageY-radian,m.stageY+radian);
ballArr[i].visible = true;

ballArr[i].playFn(m.stageX,m.stageY,xp,yp,sc,sc,randRange(1,10)/10,__time,twFn);//call playFn function inside the ballExt class this function will call tween class to Animate the ball
}
}
private function randRange(min:Number, max:Number):Number
{
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}

}
}
4. add this code to ballExt.as
package
{
import flash.display.Sprite;
import fl.transitions.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
public class ballExt extends Sprite
{
var xTween:Tween;
var yTween:Tween;
var xscTween:Tween;
var yscTween:Tween;
var alphaTween:Tween;
public function ballExt()
{
}
public function playFn(stX:Number,stY:Number,xpos:Number,ypos:Number,xsc:Number,ysc:Number,__alpha:Number,_time:Number,twFn)
{
xTween = new Tween(this, "x", twFn, stX, xpos, _time, true);
yTween = new Tween(this, "y", twFn, stY, ypos, _time, true);
xscTween = new Tween(this, "scaleX", twFn, 0, xsc, _time, true);
yscTween = new Tween(this, "scaleY", twFn, 0, ysc, _time, true);
alphaTween = new Tween(this, "alpha", twFn, 0, __alpha, _time, true);
}
}
}

xTween = new Tween(this, "x", twFn, stX, xpos, _time, true);
this line create
new tween instances and store them as the variables xTween . The Tween class constructor requires seven parameters, which are in order:
  1. object – The instance of an object in which to apply the tween. This can be any object, and is not just limited to movie clips.

  2. property – The numeric property of the previous object in which to Tween. The property name is passed to the constructor as a string.

  3. ease class and method – The type of tween and the method of ease applied to it. Flash comes bundled with 6 easing classes:

    1. Back – Extends the animation over one or both ends of the tween.

    2. Bounce – Creates a bouncing effect in the tween at one or both ends.

    3. Elastic – Creates a mixture of the bounce and back classes. The animation is extended over and bounces back at one or both ends of the Tween.

    4. Regular – Slower motion at one or both ends of the tween.

    5. Strong – Similar to regular but is more pronounced when combined with the various easing methods.

    6. None – A simple linear transition from a to b.

These six easing classes each have three methods to control the ease:

  1. easeIn – The ease is applied only at the start of the tween.

  2. easeOut – The ease is applied only at the end of the tween.

  3. easeInOut – The ease is applied at both the beginning and end of the tween.

By combing the six easing classes with the three easing methods, we can create a possible 18 different effects for each property.

  1. begin – The beginning value for the property in the tween.

  2. end – The end value for the property in the tween.

  3. duration – The duration of the tween. This value can either be specified as a number of frames or a number of seconds depending on the value of the next parameter.

  4. use seconds – Specifies whether or not the duration parameter should be measured in frames or seconds. Requires a Boolean of either true (use seconds) or false (use frames).

Last .On the main time line add this code to first frame

import simpleTween;
var smTw:simpleTween = new simpleTween(stage);
addChild(smTw);


down load full code here !

this project will use full for a dynamic animate that can interactive with user .
Without time line animation we will get a smaller file.

Hudsadin keox

Tuesday, September 02, 2008

How to Connect to socket as3.0.

I'm not real English native but I think you all will understand by read the code and some my suggestion.

First what is socket.

The Socket class enables ActionScript code to make socket connections and to read and write raw binary data. It is similar to XMLSocket but does not dictate the format of the received or transmitted data.

The Socket class is useful for working with servers that use binary protocols.

When you use this class, consider the Flash Player security model:

  • Data loading is not allowed if the calling SWF file is in the local-with-file-system sandbox and the target resource is from a network sandbox.
  • Data loading is also not allowed if the calling SWF file is from a network sandbox and the target resource is local.
  • The calling SWF file and the network resource being accessed must be in exactly the same domain. For example, a SWF file at adobe.com can connect only to a server daemon at adobe.com.
  • Websites can permit cross-domain access to a resource through a cross-domain policy file.
Go to Code it .I will show how to connect to the demon and how to sent / recive data.
- create new as file
Save it name clientConnect.as







and add this code.

package
{
import flash.net.Socket;
import flash.events.Event;
import flash.events.ProgressEvent;
////////////////////////////////////////////////////////////// import some class that have to use for this project

public class clientConnect
{
private var client:Socket = new Socket();// instance of Socket class we will use this instace to connect to demon.
private var s_path:String = "";//variable for set server path .
private var s_port:uint;
//variable for set server port .
private var returnData:uint;//variable for store data that client recive form server.
private var c_data:uint;//variable for store data that wee want to sent to server.
private var recived:Boolean=false;//variable for check client recived data yet.
public function clientConnect(path:String,port:uint)//when we define class variable and call this method we must sent serve path and port to this method.
{
s_path=path;
s_port=port;
}
public function connected()//sure this function try to connect to server*-*.
{
try
{
client.connect(s_path,s_port);
}
catch (e:ArgumentError)
{
trace(e);
}
client.addEventListener(Event.CONNECT,sentData);
client.addEventListener(ProgressEvent.SOCKET_DATA,reciveData);
}
public function setData(data_in:uint)//set data that we want to sent to server.
{
c_data = data_in;
}
private function sentData(evt:Event):void
{
client.writeInt(c_data);
}
public function getData():uint//when we need to use the data that server had sent to client call this function .
{
if(recived)
return returnData;
else
return null;
}
private function reciveData(evt:ProgressEvent)
{
returnData = client.readInt();
recived = true;
trace(returnData)
client.close();
}
}
}


At the main swf file add the code like this.

import clientConnect;
var client:clientConnect = new clientConnect("localhost",1500);
client.setData(100);//sent 100 te server.
client.connected();

Now we had finished the client codding.
next go to fun with some easy java code for demon!
you must have jdk download it!
- create new text file and save its name Server.java
- add this code
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String args[])
{
while(true)
{
try{
ServerSocket serverSocket = new ServerSocket(1500); //new server socket instace that set port 1500 (flash client must set like this demon)
System.out.println("Please, wait....");
Socket clientSocket = serverSocket.accept(); //accepted client who connected to this port
System.out.println("Connected : "+clientSocket.getInetAddress().toString());

DataInputStream inData = new DataInputStream(clientSocket.getInputStream()); //instace for store data from client
DataOutputStream outData = new DataOutputStream(clientSocket.getOutputStream());//instace for sent data to client.
System.out.println("recive Data ...."+inData.readInt()+" and return Data 20");
outData.writeInt(20); //write 20 to flash client
clientSocket.close();
serverSocket.close();
}catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
}
You can implement this bacsic socket project to make your own flash chat,mutiplayer game,
or some easy realtime msg sending.

Thank ! for visited this blog.

Hudsadin keox