Monday 29 September 2014

Hit TestPoint And HitTest Object In AS3

Hit TestPoint And HitTest Object In AS3




Flash College



Flash College



====================================================================
COPY AND PASTE BELOW CODE IN ACTION LAYER FRAME 1
=====================================================================



addEventListener(Event.ENTER_FRAME, hitTest);
function hitTest(event:Event):void {
if (mCircle.hitTestPoint(mouseX, mouseY, true)) {
hitText1.text="hitTestPoint- True";
}
else {
hitText1.text="hitTestPoint- False";
}
mRectangle.x=mouseX;
mRectangle.y=mouseY;
if (mCircle.hitTestObject(mRectangle)) {
hitText2.text="hitTestObject- True";
}
else {
hitText2.text="hitTestObject- False";
}
}

=====================================================================
HIT TEST EXAPLES
-------------------------------------------------------------------------------------------------------------------
1. create movieclip with instance name of 'star'.
2. create movieclip with instance name of 'circle'.
3. create one dynamic text box with the instance name of 'messageText'.

code : -


// move star with mouse
addEventListener(Event.ENTER_FRAME, test_fun1);

function test_fun1(event:Event):void {
 star.x=mouseX;
 star.y=mouseY;


// test star versus circle
if (star.hitTestObject(circle)) {
 messageText.text="collide";
} else {
 messageText.text="Not collide";
}
}

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

ballYellow.addEventListener(MouseEvent.MOUSE_DOWN, grabMe);
ballYellow.buttonMode = true;
ballGreen.addEventListener(MouseEvent.MOUSE_DOWN, grabMe);
ballGreen.buttonMode = true;

var me:Object;
function grabMe(e:MouseEvent):void{
    me = e.currentTarget;
    me.removeEventListener(MouseEvent.MOUSE_DOWN, grabMe);
    me.startDrag();
    stage.addEventListener(MouseEvent.MOUSE_MOVE, dragMe);
    stage.addEventListener(MouseEvent.MOUSE_UP, dropMe);
}
function dropMe(e:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_UP, dropMe);
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragMe);
    me.stopDrag();
    me.addEventListener(MouseEvent.MOUSE_DOWN, grabMe);
}
function dragMe(e:MouseEvent):void {
    e.updateAfterEvent();
}


===========================================================
[TXT]flash-cs3-drag-and-drop-matching-3.html16-Dec-2011 14:502.2K
[   ]flash-cs3-drag-and-drop-matching-3.swf16-Dec-2011 14:5080K
[   ]flash-cs3-drag-and-drop-matching.as
https://forums.adobe.com/thread/680199?tstart=0
FIND THIS WORD: var target = obj.dropTarget;
==============================================================


/ Daniel K. Schneider - TECFA - sept 2007
// Copyright: See http://edutechwiki.unige.ch/en/
 
var dict = new Dictionary ();
 
// =================== START USER Config =====================
// Insert as many "dict[text_box] = movie;" statements you like
//  Replace: text_box by the name of a matching dynamic text_box
//           movie by the name of movie instances users can move around.
 
dict[box_c] = cat;
dict[box_d] = dog;
dict[box_r] = rocket;
dict[box_b] = bat;
dict[box_a] = apple;
 
// Do NOT change/delete any other line. Also make sure to respect
// the syntax, e.g. dont forget the ";" at the end of each line.
// ===================== END USER Config ==================== 
 
// Sound
// should I preload this somehow ?
 
var request:URLRequest = new URLRequest("applause_3.mp3");
var applause:Sound = new Sound();
applause.load(request);
 
var request2:URLRequest = new URLRequest("music.mp3");
var music:Sound = new Sound();
music.load(request2);
 
var request3:URLRequest = new URLRequest("baby_laugh.mp3");
var laugh:Sound = new Sound();
laugh.load(request3);
 
// Drag and match code
var hits = 0; // counts succesful hits
var max = 0;  // used to compute dictionary length
 
var ori_x;
var ori_y;
 
// For each item in the dictionary we add event listeners
// "for each" will loop through the values ... not the keys
 
for each (var item in dict)
{
 item.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
 item.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
 max = max + 1;
 item.buttonMode = true;
}
 
 
// Define a mouse down handler (user is dragging)
function mouseDownHandler(evt:MouseEvent):void {
 var object = evt.target;
 ori_x = object.x
 ori_y = object.y
 object.useHandCursor = true;
 object.startDrag();
}
 
function mouseUpHandler(evt:MouseEvent):void {
 //stop all sounds
 SoundMixer.stopAll();
 var obj = evt.target;
 // obj.dropTarget will give us the reference to the shape of
 // the object over which we dropped the circle.
 var target = obj.dropTarget;
 // If the target object exists the we ask the test_match function
 // to compare moved obj and target where it was dropped.
 if (target != null)
 {
  test_match(target, obj);
 }
 obj.stopDrag();
}
 
 
function test_match(target,obj) {
 // test if the pairs match
 if (dict[target] == obj)
 {
  // we got a hit
  hits = hits+1;
  textField.text = "Yes ! You got one !";
  applause.play();
  // make the object transparent
  obj.alpha = 0.5;
  // kill its event listeners - object can't be moved anymore
  obj.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  obj.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
  // Test if we are done
  if (hits == max)
  {
   textField.text = "Made it !!";
   music.play(0,5);
  }
 }
 else
 {
  obj.x = ori_x;
  obj.y = ori_y;
  textField.text = "Missed :(";
  laugh.play();
 }
}

==============================================================
Here's the script I'm currently using:

var dict = new Dictionary ();

// =================== Edit =====================

dict[box_a] = cloudy;
dict[box_n] = artichoke;

// ===================== END  ====================

var hits = 0; // counts succesful hits
var max = 0;  // used to compute dictionary length

// For each item in the dictionary we add event listeners
// "for each" will loop through the values ... not the keys

for each (var item in dict)
{
    item.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    item.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
    max = max + 1;
    item.buttonMode = true; //needed for the hand cursor
}


// Define a mouse down handler (user is dragging)
function mouseDownHandler(evt:MouseEvent):void {
    var object = evt.target;
    // we should limit dragging to the area inside the canvas
    object.useHandCursor = true;
    object.startDrag();
}

function mouseUpHandler(evt:MouseEvent):void {
    var obj = evt.target;
    // obj.dropTarget will give us the reference to the shape of
    // the object over which we dropped the circle.
    var target = obj.dropTarget;
    // If the target object exists the we ask the test_match function
    // to compare moved obj and target where it was dropped.
    if (target != null)
    {
        test_match(target, obj);
    }
    obj.stopDrag();
}

function test_match(target,obj) {
    // test if the pairs match
    if (dict[target] == obj)
    {
        // we got a hit
        hits = hits+1;
        textField.text = "Yes ! You got one !";
        // make the object transparent
        obj.alpha = 0.5;
        // kill its event listeners - object can't be moved anymore
        obj.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
        obj.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
        // Test if we are done
        if (hits == max)
        {
            textField.text = "Excellent!";
        }
    }
    else
    {
        textField.text = "Try Again :(";
    }
}
--------------------------------------------------------------------------------------------------------------
DRAG DROP HIT TEST POINT GAME IN AS3
INSTANCE NAME:
item1_mc,item2_mc,item3_mc,dropZone1_mc,dropZone2_mc,dropZone3_mc
right_mc,wrong_mc
done_btn,reset_btn
----------------------------------------------------------------------------------------------------------------




--------------------------------------------------------------------------------------------------------------
COPY AND PASTE CODE IN ACTION SCRIPT PANEL
Download Source File :
http://www40.zippyshare.com/v/1PAhcDmf/file.html
--------------------------------------------------------------------------------------------------------------

right_mc.visible=false;
wrong_mc.visible=false;
var orig1X:Number=item1_mc.x;
var orig1Y:Number=item1_mc.y;
var orig2X:Number=item2_mc.x;
var orig2Y:Number=item2_mc.y;
var orig3X:Number=item3_mc.x;
var orig3Y:Number=item3_mc.y;
item1_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
item1_mc.addEventListener(MouseEvent.MOUSE_UP, item1Release);
item2_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
item2_mc.addEventListener(MouseEvent.MOUSE_UP, item2Release);
item3_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);  
item3_mc.addEventListener(MouseEvent.MOUSE_UP, item3Release);  
done_btn.addEventListener(MouseEvent.CLICK, checkAnswers);  
reset_btn.addEventListener(MouseEvent.CLICK, reset);
item1_mc.buttonMode=true;  
item2_mc.buttonMode=true;  
item3_mc.buttonMode=true;
function dragTheObject(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.startDrag();
var topPos:uint=this.numChildren-1;
this.setChildIndex(item, topPos);  
}
function item1Release(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();    
if (dropZone1_mc.hitTestPoint(item.x,item.y)) {
item.x=dropZone1_mc.x;
item.y=dropZone1_mc.y;
}
else {
item.x=orig1X;
item.y=orig1Y;
}}  
function item2Release(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (dropZone2_mc.hitTestPoint(item.x,item.y)) {
item.x=dropZone2_mc.x;
item.y=dropZone2_mc.y;
}
else {
item.x=orig2X;
item.y=orig2Y;
}}  

function item3Release(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (dropZone3_mc.hitTestPoint(item.x,item.y)) {
item.x=dropZone3_mc.x;
item.y=dropZone3_mc.y;
} else {
item.x=orig3X;
item.y=orig3Y;
}}
function checkAnswers(event:MouseEvent):void {
if (dropZone1_mc.hitTestPoint(item1_mc.x,item1_mc.y) &&
dropZone2_mc.hitTestPoint(item2_mc.x,item2_mc.y) &&
dropZone3_mc.hitTestPoint(item3_mc.x,item3_mc.y)) {
wrong_mc.visible = false;
right_mc.visible = true;
} else {
wrong_mc.visible = true;
right_mc.visible = false;
}}
function reset(event:MouseEvent):void {
item1_mc.x=orig1X;
item1_mc.y=orig1Y;
item2_mc.x=orig2X;
item2_mc.y=orig2Y;
item3_mc.x=orig3X;
item3_mc.y=orig3Y;
right_mc.visible=false;
wrong_mc.visible=false;
}
function itemRelease(event:MouseEvent):void {
var thisItem:MovieClip = MovieClip(event.target);
thisItem.stopDrag();
if (dropZone1_mc.hitTestPoint(thisItem.x,thisItem.y)) {
thisItem.x = dropZone1_mc.x;
thisItem.y = dropZone1_mc.y;
}
else if (dropZone2_mc.hitTestPoint(thisItem.x,thisItem.y)) {
thisItem.x = dropZone2_mc.x;
thisItem.y = dropZone2_mc.y;
}
else if (dropZone3_mc.hitTestPoint(thisItem.x,thisItem.y)) {
thisItem.x = dropZone3_mc.x;
thisItem.y = dropZone3_mc.y;
}
else if (thisItem==item1_mc) {
event.target.x = orig1X;
event.target.y = orig1Y;
}
else if (thisItem==item2_mc) {
event.target.x = orig2X;
event.target.y = orig2Y;
}
else {
event.target.x = orig3X;
event.target.y = orig3Y;
}}



2 comments

function transp(evt:MouseEvent, theMessage:String)
{
evt.target.alpha = 0.1;
trace("theMessage = ", theMessage);
}

square_btn.addEventListener(MouseEvent.CLICK, transp(evt, "hello!"));

Hit Testpoint And Hittest Object In As3 - Flash College >>>>> Download Now

>>>>> Download Full

Hit Testpoint And Hittest Object In As3 - Flash College >>>>> Download LINK

>>>>> Download Now

Hit Testpoint And Hittest Object In As3 - Flash College >>>>> Download Full

>>>>> Download LINK tk


EmoticonEmoticon