Flash actionscript 3.0

how to add click event dynamically in As3

0

Posted by Sankar.G | Posted in | Posted on 2:43 PM

var i:Number;
for(i=0;i<=(buttonHolder.numChildren-1) ; i++)
{
buttonHolder.getChildAt(i).addEventListener(MouseEvent.CLICK, starter);
}

function starter(e:MouseEvent):void {
var ob=e.currentTarget;
trace(ob.name.substr(1));
}


select all buttons and create a movie clip and name it as buttonHolder and put this script it will work

how to put stage objects in array using flash AS 3.0

0

Posted by Sankar.G | Posted in | Posted on 10:58 AM

var a:Array=new Array();
var b:Array=new Array();
var c:Array=new Array();
var objec:Array=new Array();
var myarray:Array=new Array();
var i:Number;
var temp:Number;
for(i=0;i<
container.numChildren;i++)
{
objec.push(container.getChildAt(i));
}
for(i=0;i<
objec.length;i++)
{
temp=objec[i].name.slice(1);
myarray[temp]=objec[i];
}
for(i=0;i<
container.numChildren;i++)
{
trace(myarray[i].name);
}

Select all MovieClip which are all you want to put in array and create a new Movie Clip and

name it as container and push that container to array and name the movie clip inside the container in line like a1,a2,a3,a4....... like that and slice the array and store the last number in temp and corrosponding object in that array ......

Attach a MovieClip from Library dynamically using AS3.0

0

Posted by Sankar.G | Posted in | Posted on 11:03 AM

import flash.utils.getDefinitionByName;
var i:Number;
for(i= 1;i<4;i++)
{
var myCube:Class = getDefinitionByName("Cube" + i) as Class;
var cube:Object = new myCube();
cube.name = "cube" + i;
addChild(DisplayObject(cube));
cube.x = i * 100;
cube.y = i * 100;
cube.addEventListener(MouseEvent.ROLL_OVER,eventfunction);
}
function eventfunction(event:MouseEvent):void
{
var bc=(event.target.name.substr(4));
trace("cube"+bc);
getChildByName("cube"+bc).addEventListener(MouseEvent.MOUSE
_DOWN,fun);
getChildByName("cube"+bc).addEventListener(MouseEvent.MOUSE
_UP,fun1);
}
function fun1(e:Event)
{
var ob=e.target;
ob.stopDrag();
}
function fun(e1:Event)
{
var ob1=e1.target;
ob1.startDrag();
}
create 5 Different Movie Clip in flash and name it as cube1,cube2,cube3,........ and give link name and same cube1,cube2,cube3...... and put this script it will work

gravity force in AS3.0

0

Posted by Sankar.G | Posted in | Posted on 11:45 AM

stop();
var speedx:Number=0;
var speedy:Number=0;
var fri:Number=0.98765;
var radius:Number=ball_mc.height/4;
var ax:Number=0;
var ay:Number=0;
var gravity:Number=2.5;
addEventListener(Event.ENTER_FRAME,pull);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keydown);
stage.addEventListener(KeyboardEvent.KEY_UP,keyup);
function pull(e:Event) {

speedy+=gravity;
speedx+=ax;
speedy+=ay;
speedy*=fri;
speedx*=fri;
ball_mc.y+=speedy;
ball_mc.x+=speedx;
if (ball_mc.y+radius>stage.stageHeight) {
ball_mc.y=stage.stageHeight-radius;
speedy*=-1;
} else if (ball_mc.y-radius<0) { ball_mc.y=radius; speedy*=-1; } else if (ball_mc.x>stage.stageWidth)
{
ball_mc.x=stage.stageWidth-radius;
speedx*=-1;
}
else if(ball_mc.x-radius<0) { ball_mc.x=radius; speedx*=-1; } } function keydown(e:KeyboardEvent):void { switch (e.keyCode) { case Keyboard.LEFT : ax = -1; break; case Keyboard.RIGHT : ax = 1; break; case Keyboard.UP : gravity = 0; ay = -0.5; break; case Keyboard.DOWN : ay = 1; break; } } function keyup(e:KeyboardEvent):void { gravity = 1; ax = 0; ay = 0; } Examples: Gravity in AS3.swf

how to do addition in flash using class file

0

Posted by Sankar.G | Posted in | Posted on 12:19 PM

add this code in flash class script file and save this file as checking.as
class checking
{
var mc:MovieClip;
var t1:TextField;
var t2:TextField;
var re:TextField;
public function main(mc,t1,t2,re)
{
mc.onPress=function()
{
var a:Number;
var b:Number;
var c:Number;
a=parseInt(t1.text);
b=parseInt(t2.text);
c=a+b;
re.text=c;
}
}
}
In stage create a 3 text box 2-input text box name(text1 & text2) & 1 - dynamic text box name(res) and create a button name(ball3)and write this code in a frame

code:

var ch:checking=new checking();
ch.main(ball3,text1,text2,res);

and save it as checking.fla