9
DRAG AND DROP and COLLISION DETECTION ENGR. SOLIEL G. MUTYA JUNE 2016

Drag and drop- ActionScript 3.0

Embed Size (px)

Citation preview

Page 1: Drag and drop- ActionScript 3.0

DRAG AND DROPand

COLLISION DETECTION

ENGR. SOLIEL G. MUTYAJUNE 2016

Page 2: Drag and drop- ActionScript 3.0

COLOR CODES• Variable Name –

• Data Type –

• Instance Name –

• Function Name –

• AS3 Keywords –

• COMMENTS –

Page 3: Drag and drop- ActionScript 3.0

EXAMPLE 1: Simple Drag and Dropstop(); //hat to be draggable upon mousedown

function DragHat(event:MouseEvent): void {hatMC.startDrag();}

hatMC.addEventListener(MouseEvent.MOUSE_DOWN, DragHat); //stop the hat from dragging when we release our left click button

function ReleaseHat(event:MouseEvent):void {hatMC.stopDrag(); //stop dragging the hat}

hatMC.addEventListener(MouseEvent.MOUSE_UP, ReleaseHat);

Page 4: Drag and drop- ActionScript 3.0

EXAMPLE 1: Simple Drag and Drop //drag undershirtfunction dragUnderShirtMC(event:MouseEvent):void {undershirtMC.startDrag();}

undershirtMC.addEventListener(MouseEvent.MOUSE_DOWN, dragUnderShirtMC); //release undershirtfunction releaseUnderShirtMC(event:MouseEvent):void { undershirtMC.stopDrag();}

undershirtMC.addEventListener(MouseEvent.MOUSE_UP, releaseUnderShirtMC); 

Page 5: Drag and drop- ActionScript 3.0

EXAMPLE 1: Simple Drag and Drop //drag skirtfunction dragSkirt(event:MouseEvent):void {skirtMC.startDrag();}

skirtMC.addEventListener(MouseEvent.MOUSE_DOWN, dragSkirt); //release skirtfunction releaseSkirt(event:MouseEvent):void {skirtMC.stopDrag();}

skirtMC.addEventListener(MouseEvent.MOUSE_UP, releaseSkirt);

Page 6: Drag and drop- ActionScript 3.0

EXAMPLE 2: DRAG-AND-DROP WITH DIMENSIONS

circle_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);square_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);stage.addEventListener(MouseEvent.MOUSE_UP, drop); function drag(e:MouseEvent):void{ e.target.startDrag(false, new Rectangle(75,50,350,250));}

function drop(e:MouseEvent):void{ stopDrag();}

Page 7: Drag and drop- ActionScript 3.0

EXAMPLE 3: DRAG-AND-DROP WITH DYNAMIC TEXT

stop(); //drag circle

function dragCircle(event:MouseEvent):void {circleMC.startDrag();}

circleMC.addEventListener(MouseEvent.MOUSE_DOWN, dragCircle); //release circle

function releaseCircle(event:MouseEvent):void {circleMC.stopDrag();}

circleMC.addEventListener(MouseEvent.MOUSE_UP, releaseCircle);

Page 8: Drag and drop- ActionScript 3.0

EXAMPLE 3: DRAG-AND-DROP WITH DYNAMIC TEXT

circleMC.addEventListener(MouseEvent.MOUSE_UP, releaseCircle); //check if circle is inside the squarefunction checkCircleLocation(event:MouseEvent): void {if (circleMC.x > 300 && circleMC.x <425 && circleMC.y >130 && circleMC.y <250)

{myText.text = "Correct";}

}circleMC.addEventListener(MouseEvent.MOUSE_UP, checkCircleLocation);

Page 9: Drag and drop- ActionScript 3.0

DRAG-AND-DROP WITH COLLISION DETECTION

square1_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);stage.addEventListener(MouseEvent.MOUSE_UP, drop); function drag(e:MouseEvent):void{ square1_mc.startDrag();} function drop(e:MouseEvent):void{ stopDrag(); if (square1_mc.hitTestObject(square2_mc)) { trace("Collision detected!"); } else { trace("No collision."); }}