Archive for February, 2010


Overlap display Object in AS3

This tutor is about overlapping display object to each another. Here, I have three files. Ball.as

Ball.as:
here, we have class of circle shape object

BallSeq.as:
It generates circle ball with different color and set sequence

myFlash.fla
here, we need to mention class BallSeq

Ball.as

package {
import flash.display.*;
import flash.text.*;
public class Ball extends Shape{
public var xPos:Number;
public var yPos:Number;
public var radius:Number;
public var color:uint;

public function Ball(xInput:Number,yInput:Number,rInput:Number,colorInput:uint) {
var xPos=xInput;
var yPos=yInput;
var radius=rInput;
var color=colorInput;
this.graphics.beginFill(color);
this.graphics.drawCircle(xPos,yPos,radius);
}
}
}

Here, we have made Ball Class constructor with argument x postion, y position, radius, color

BallSeq.as

package{

import flash.display.Sprite;
import flash.events.MouseEvent;
public class BallSeq extends Sprite {

public var container2:Sprite=new Sprite();
public var blackBall:Sprite=new Sprite();
public var redBall:Sprite=new Sprite();
public var blueBall:Sprite=new Sprite();
public var greenBall:Sprite=new Sprite();

public function BallSeq(){

// Black Ball
blackBall.addChild(new Ball(100,100,30,0x000000));
blackBall.name="blackBall";
//addChild(blackBall);

// Green Ball
greenBall.addChild(new Ball(130,140,30,0x29990b));
greenBall.name="greenBall";
//addChild(greenBall);

// Red Ball
redBall.addChild(new Ball(130,90,30,0xff0000));
redBall.name="redBall";
//addChild(redBall);

// blue Ball
blueBall.addChild(new Ball(150,100,30,0x0000ff));
blueBall.name="blueBall";

container2.addChild(blueBall);
container2.addChild(redBall);
container2.addChild(blackBall);
container2.addChild(greenBall);

container2.swapChildren(blackBall,redBall);

var i:Number=0;
while(i<container2.numChildren){
container2.getChildByName(container2.getChildAt(i).name).addEventListener(MouseEvent.CLICK,putFront);
i++;
}

this.addChild(container2);

}

public function putFront(event:MouseEvent){
container2.setChildIndex(container2.getChildByName(event.target.name),container2.numChildren-1);

}

}

}

Advertisements

hi, I am comming with simple easy way to drag and drop of flash as3. I have use startDrag and stopDrag to drag and drop movieClip or any display Object. But it gives some disturbe . So, made tiny but flexible to drag and drop your object.

import flash.ui.Mouse;

function startDragging(event:MouseEvent):void{
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveBall);
}

function moveBall(event:MouseEvent):void{
circle.x=event.stageX-circle.width/2;
circle.y=event.stageY-circle.height/2;

}

function stopDragging(event:MouseEvent):void{
stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveBall);
}

circle.addEventListener(MouseEvent.MOUSE_DOWN,startDragging);
circle.addEventListener(MouseEvent.MOUSE_UP,stopDragging);

Note : here circle is the movieclip