Sunday, 11 January 2015

HIT TEST IN AS3 AND STOP DRAG

HIT TEST IN AS3 AND STOP DRAG

-----------------------------------------------------------------------------------------
READ MORE:
https://www.youtube.com/watch?v=-8REGjXsfic&feature=youtu.be
CREATE TWO MOVIE CLIPS GIVE INSTANCE NAME
AND PASTE CODE IN ACTION SCRIPT PANEL
player_mc , wall
-----------------------------------------------------------------------------------------
COPY AND PASTE BELOW CODE
------------------------------------------------------------------------------------
import flash.events.Event;
stop();
player_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
player_mc.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(event:MouseEvent):void {
player_mc.startDrag();
}
function drop(event:MouseEvent):void {
player_mc.stopDrag();
}
this.addEventListener( Event.ENTER_FRAME, handleCollision)
function handleCollision( e:Event ):void{
if(player_mc.hitTestObject( wall )){
A.text="HIT TEST FIND"
player_mc.stopDrag();
}}
----------------------------------------------------------------------------------------------
HOW ADD IN THIS FUNCTION REVERSE FUNCTION IF HIT TEST MISS
----------------------------------------------------------------------------------------------

var startPosition:Point;

MOVIE1.addEventListener(MouseEvent.MOUSE_DOWN,startDragme);
function startDragme(event:Event):void {
MOVIE1.startDrag();
startPosition = new Point( MOVIE1.x,MOVIE1.y);
}

MOVIE1.addEventListener(MouseEvent.MOUSE_UP, dragStop4);
function dragStop4(e:Event):void {
MOVIE1.stopDrag();
//set back or tween position
MOVIE1.x = startPosition.x;
MOVIE1.y = startPosition.y;
startPosition = null;
}


this.addEventListener( Event.ENTER_FRAME, handleCollision)
function handleCollision( e:Event ):void{
if(MOVIE1.hitTestObject(MOVIE2)){
A.text="HIT TEST FIND"
MOVIE1.stopDrag();
}}

-----------------------------------------------------------------------------------------
EXAMPLE 2
YOU CAN WRITE THIS CODE UNDER ENTER FRAME FUNCTION
BECAUSE STOP DRAG FUNCTION WORK IMMEDIATELY
---------------------------------------------------------------------------------------------
import flash.events.Event;
stop();
player_mc.addEventListener( Event.ENTER_FRAME, handleCollision)
function handleCollision( e:Event ):void{
player_mc.startDrag();
if(player_mc.hitTestObject( wall )){
A.text="HIT TEST FIND"
player_mc.stopDrag();
}}

----------------------------------------------------------------------------------------
OR USE THIS  HIT TEST CODE
http://www.trainingtutorials101.com/2011/02/simple-as3-collision-detection-using.html
---------------------------------------------------------------------------------------

square1_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);

function drag(e:MouseEvent):void
{
 e.target.startDrag();
}

function drop(e:MouseEvent):void
{
 stopDrag();
 if (square1_mc.hitTestObject(square2_mc))
 {
  trace("Collision detected!");
 }
 else
 {
  trace("No collision.");
 }
}

-----------------------------------------------------------------------------------------
EXAMPLE 3
IF HIT TARGET MISS HOW  MOVIE RETURN BACK TO POSITION
COPY AND PASTE BELOW CODE
CREATE ONE MOVIE CLIP AND GIVE INSTANCE NAME mc_d4
---------------------------------------------------------------------------------------------

var startPosition:Point;
mc_d4.addEventListener(MouseEvent.MOUSE_DOWN, dragD4);
stage.addEventListener(MouseEvent.MOUSE_UP, dragStop4);
function dragD4(e:Event):void {
mc_d4.startDrag();
startPosition = new Point( mc_d4.x, mc_d4.y);
}
function dragStop4(e:Event):void {
mc_d4.stopDrag();
//set back or tween position
mc_d4.x = startPosition.x;
mc_d4.y = startPosition.y;
startPosition = null;
}

-----------------------------------------------------------------------------------------
EXAMPLE 4
IF HIT TARGET MISS HOW  MOVIE RETURN BACK TO POSITION
COPY AND PASTE BELOW CODE
CREATE 4 MOVIE CLIPS AND GIVE INSTANCE NAMES
holder, block1 ,block2,block3
---------------------------------------------------------------------------------------------



---------------------------------------------------------------------------------------------
COPY AND PASTE BELOW CODE
---------------------------------------------------------------------------------------------

var movingBlock:Sprite;
var startingPt:Point = new Point();
var placed:int = 0;
block1.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
block2.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
block3.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
function startBlockMove(evt:MouseEvent):void {
movingBlock = Sprite(evt.currentTarget);
startingPt.x = movingBlock.x;
startingPt.y = movingBlock.y;
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveBlock);
}
function moveBlock(e:MouseEvent):void {
movingBlock.x = stage.mouseX;
movingBlock.y = stage.mouseY;
}
stage.addEventListener(MouseEvent.MOUSE_UP, stopMotion);
function stopMotion(evt:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveBlock);
snapInPlace();
movingBlock = new Sprite();
startingPt = new Point();
}
function snapInPlace():void {
if (holder.hitTestObject(movingBlock)) {
movingBlock.stopDrag();
movingBlock.removeEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
}
else {
movingBlock.x = startingPt.x;
movingBlock.y = startingPt.y;
}}

stage.addEventListener(KeyboardEvent.KEY_DOWN,reset);
function reset(evt:KeyboardEvent) {
if (evt.keyCode == Keyboard.UP) {
block1.x = 50;
block1.y = 50;
block2.x = 50;
block2.y = 200;
block3.x = 175;
block3.y = 100;
block1.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
block2.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
block3.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
placed = 0;
}}
--------------------------------------------------------------------------------------------------------
EXAMPLE 4
IF HIT TARGET MISS HOW  MOVIE RETURN BACK TO POSITION
COPY AND PASTE BELOW CODE
CREATE 4 MOVIE CLIPS AND GIVE INSTANCE NAMES
holder, block1 ,block2,block3
OR USE THIS CODE
http://www.flashandmath.com/basic/dragdroptour/dd_tour5.html
http://www.santiago.bz/as3/math/
---------------------------------------------------------------------------------------------------------

// Global variables to keep up with things

var movingBlock:Sprite;
var startingPt:Point = new Point();
var placed:int = 0;


// Part II -- Add drag and drop functionality

block1.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
block2.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
block3.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);

function startBlockMove(evt:MouseEvent):void {
movingBlock = Sprite(evt.currentTarget);
startingPt.x = movingBlock.x;
startingPt.y = movingBlock.y;
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveBlock);
}

function moveBlock(e:MouseEvent):void {
movingBlock.x = stage.mouseX;
movingBlock.y = stage.mouseY;
}

stage.addEventListener(MouseEvent.MOUSE_UP, stopMotion);

function stopMotion(evt:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveBlock);
snapInPlace();
movingBlock = new Sprite();
startingPt = new Point();
}

function snapInPlace():void {
if (holder.hitTestObject(movingBlock)) {
movingBlock.x = holder.x;
movingBlock.y = holder.y + 100*placed;
placed++;
movingBlock.removeEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
}
else {
movingBlock.x = startingPt.x;
movingBlock.y = startingPt.y;
}
}
// Part III -- Add a reset capability with a keyboard event

stage.addEventListener(KeyboardEvent.KEY_DOWN,reset);

function reset(evt:KeyboardEvent) {
if (evt.keyCode == Keyboard.UP) {
block1.x = 50;
block1.y = 50;
block2.x = 50;
block2.y = 200;
block3.x = 175;
block3.y = 100;

block1.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
block2.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);
block3.addEventListener(MouseEvent.MOUSE_DOWN, startBlockMove);

placed = 0;
}
}

==================================================================
DRAG AND DROP FUNCTION WITH TWEEN FUNCTION IN AS3
http://www.flashandmath.com/basic/tween/example4.html
http://pekingdesign.com/2013/smooth-drag-and-drop-using-tweener-class-as3/
http://www.swinburne.edu.au/design/tutorials/P-flash/T-How-to-drag-and-drop-in-Flash/ID-37/
http://www.trainingtutorials101.com/2012/03/flash-as3-tween-events.html
http://asgamer.com/2009/using-tweener-in-as3-to-move-objects
===================================================================
These import statements allow us to create and control tweens.
import fl.transitions.Tween;
import fl.transitions.easing.*;
Next we declare "tween" variables. Note that we must use one tween for each property of an object we wish to change over time. Since the example above shows movement in the x and y direction as well as a rotation, we will use three tweens.
var moveBackX:Tween;
var moveBackY:Tween;
var moveRound:Tween;
Create spirtes to serve as a holder and a draggable box and add the appropriate graphics. Note thatt we make the (0,0) point of the "dragger" in the center since we will be rotating it, and we make the (0,0) point of the holder 1 px off center to get a nice overlay effect.
var dragger:Sprite = new Sprite();
var holder:Sprite = new Sprite();

dragger.graphics.lineStyle(1,0);
dragger.graphics.beginFill(0xFACADE);
dragger.graphics.drawRect(-50,-25,100,50);
dragger.graphics.endFill();
dragger.x = 100;
dragger.y = 100;
addChild(dragger);

holder.graphics.lineStyle(1,0);
holder.graphics.beginFill(0xFFFFFF);
holder.graphics.drawRect(-51,-26,100,50);
holder.graphics.endFill();
holder.x = 100;
holder.y = 100;

addChild(holder);
We add listeners so that a "mouse down" on the box starts the drag but a "mouse up" anywhere stops the dragging.
dragger.addEventListener(MouseEvent.MOUSE_DOWN, goDrag);

addEventListener(MouseEvent.MOUSE_UP, goBack);
The goDrag function starts dragging the "dragger" box with an added drop shadow for effect.
function goDrag(evt:MouseEvent):void {
dragger.startDrag();
dragger.filters = [ new DropShadowFilter() ];
}
The goBack function removes the drop shadow and constructs three "tweens" to put the box back to the holder over a period of 0.5 seconds with a 180 degree rotation thrown in for a flourish. To see the syntax for the Tween constructor, see that help file for "Tween Class." See the help files for the "fl.motion.easing package" for other choices of easing functions.
function goBack(evt:MouseEvent):void {
dragger.stopDrag();
dragger.filters = [ ];
moveBackX = new Tween(dragger, "x", Strong.easeOut, dragger.x, holder.x, 0.5, true);
moveBackY = new Tween(dragger, "y", Strong.easeOut, dragger.y, holder.y, 0.5, true);
moveRound = new Tween(dragger, "rotation", Strong.easeIn, 0, 180, 0.5, true);
}








=====================================================================
HIT TEST EXAPLES
======================================================================
red_ball1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
red_ball1.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);


function mouseDownHandler(evt:MouseEvent):void

{
var object = evt.target;
object.useHandCursor = true;
object.startDrag();
}

function mouseUpHandler(evt:MouseEvent):void
{
var obj = evt.target;
var target = obj.dropTarget;
}

=================================================================








EmoticonEmoticon