Saturday, 27 September 2014

HIT TEST IN AS3 AND SCORE COUNT










-----------------------------------------------------------------------------------------------
EXAMPLE 4 CRATE SCORE COUNTER
CRATE TEXT BOX AND GIVE INSTANCE NAME
scorecounter
PASTE BELOW ACTION SCRIPT IN ACTION SCRIPT PANEL
SOURCE FILES DOWLOAD BY GOOGLE
---------------------------------------------------------------------------------------------------------------------
AUTO COUNTER IN AS2 WITH ENTER FRAME
if(var1==10){var1=var1-var1}
YOU CAN CHANGE THIS LINE 
if(var1==10){var1=var1-10}
YOU CAN CHANGE THIS LINE  
if(var1>=10){var1=var1-10}LIKE IN AS3 COUNTER  if(score >= 100 ) {score = score - 100}

------------------------------------------------------------------------------------------------------------------------
var var1:Number =0;
this.onEnterFrame=function(){
var1+=1
txt1.text=var1
if(var1==10){var1=var1-var1}
}

---------------------------------------------------------------------------------------------------------------------
var score:uint = 0;
addEventListener(Event.ENTER_FRAME, ScoreCounter);
function ScoreCounter(e:Event):void{
score += 1;
scorecounter.text = " SCORE:"+score.toString()}

-----------------------------------------------------------------------------------------------------
var score:uint;
score = 0;
addEventListener(Event.ENTER_FRAME, ScoreCounter);
function ScoreCounter(e:Event):void{
score += 1;
scorecounter.text = " SCORE:"+score.toString()}
--------------------------------------------------------------------------------------------------
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;
}


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

------------------------------------------------------------------------------------------------------
CREATE TWO MOVIE CLIPS AND  TWO TEXT BOXES AND ONE BUTTON
GIVE  INSTANCE NAME SET BUTTON LABEL INTO START
scorecounter,A, MOVIE1,MOVIE2 , myButton
myButton LABLE NAME = START
-------------------------------------------------------------------------------------------------------

//STAGE WIDTH =500 , HEIGHT = 400 import flash.display.Sprite; import flash.events.MouseEvent; import fl.controls.Button; var score:uint; score = 0;//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX scorecounter.text =" SCORE:"+ score.toString(); MOVIE1.addEventListener(MouseEvent.MOUSE_DOWN,startDragme); function startDragme(event:Event):void{if (myButton.selected == true)MOVIE1.startDrag(true)} MOVIE1.addEventListener(Event.ENTER_FRAME,checkHitTest) function checkHitTest(event:Event){ if(MOVIE1.hitTestObject(MOVIE2)){A.text="HIT TEST FIND"} if(MOVIE1.hitTestObject(MOVIE2)){ score += 1;//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX scorecounter.text = " SCORE:"+score.toString()} if(score >= 100 ) {score = score - 100} else{A.text=""}} MOVIE2.addEventListener(MouseEvent.MOUSE_DOWN,startDragme1); function startDragme1(event:Event):void{if (myButton.selected == true)MOVIE2.startDrag(true)} MOVIE2.addEventListener(Event.ENTER_FRAME,checkHitTest2) function checkHitTest2(event:Event){ if(MOVIE2.hitTestObject(MOVIE1)){A.text="HIT TEST FIND";} else{A.text=""}} MOVIE1.addEventListener(MouseEvent.MOUSE_UP, dropViking); function dropViking(evt:MouseEvent):void { MOVIE1.stopDrag()} MOVIE2.addEventListener(MouseEvent.MOUSE_UP, dropViking2); function dropViking2(evt:MouseEvent):void { MOVIE2.stopDrag()}
//mybutton Toggle Box Should Be Checked Without it Doesn't Work myButton.addEventListener(MouseEvent.CLICK, clickHandler); function clickHandler(evt:MouseEvent):void { if (myButton.selected == true) { myButton.label = "START " MOVIE1.buttonMode = true; MOVIE2.buttonMode = true; } else { myButton.label = "STOP " MOVIE1.buttonMode = false; MOVIE2.buttonMode = false}}
---------------------------------------------------------------------------------------------------------------
AS3 SERIES COUNTER
---------------------------------------------------------------------------------------------------------------
================================================================== AS3 SERIES COUNTER https://www.youtube.com/watch?v=_mlYAiVUw28 ================================================================== var Num1:Number = 0; Btn1.addEventListener(MouseEvent.CLICK,BTN1) function BTN1(e:MouseEvent){ Num1 = Num1 + 1; trace(Num1); Txt1.appendText([Num1]+", ") } var Num2:Number = 0; Btn2.addEventListener(MouseEvent.CLICK,BTN2) function BTN2(e:MouseEvent){ Num2 = Num2 - 1; trace(Num2); Txt1.appendText([Num2]+", ") }

---------------------------------------------------------------------------------------------------------------
READ MORE:
http://www.makeflashgames.com/tutorials/tut5.php
http://www.dcs.bbk.ac.uk/lo/mp/mp-0090.pdf
http://alanberaquit.com/ICT/dragdrop_template/
----------------------------------------------------------------------------------------------------------------------
HIT TEST EXAPLES;

DRAG AND DROP GAME FLASH CS5

A.  Setting up the "dragable" object and target.

1.  Open Adobe Flash and start a new Actionscript 3.0 document.  Save it as dragdrop_template.
2.  Add the following layers in your document. (Take note of the uppercase and lowercase letters)
3.  Select the Shape layer and draw the figure below. (red square with black stroke)
4.  Convert it to a movie clip symbol with the name square.
5.  At this point your red square is now in your stage.  Give the red square an instance name of square_mc.
6.  We are now done creating our "dragable" object.  Let us now create the target for our "dragable" object.
7.  Select the Target layer in your timeline and draw the figure below (yellow square).
     Make the figure a little smaller than the red square.  Convert the yellow square figure to a movie clip symbol with the name targetsquare.
 
8.  At this point your targetsquare is now in your stage.  Give it an instance name of targetsquare_mc.

B. Creating the Text Response. 

  1. In order to provide the user with some feedback, we will display a message when the shape is dropped, indicating whether or not they were placed successfully. Click frame 1 of the Text Response layer. Use the Text tool to draw a text box. Type "Match the shape" to give the box an initial message.  Give the text box an instance name of “reply_txt“. Use the Selection tool to position the text box if necessary.


  1. In the Properties inspector, change the text box to a Dynamic Text box using the drop down menu. Set the font to Arial Rounded Bold, the font color to black, and the size to 16pt. Next click on Embed...


choose All and click on OK.

C.  Adding the Actionscript for the whole program.

Next we will write some ActionScript to allow us to drag and drop the red square. Select frame 1 of the actions layer, and open the Actions panel by pressing F9. Copy and Paste the following:
var counter:Number = 0;
var startX:Number;
var startY:Number;

square_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
square_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);


function pickUp(event:MouseEvent):void {
     event.target.startDrag(true);
     reply_txt.text = "";
     event.target.parent.addChild(event.target);
     startX = event.target.x;
     startY = event.target.y;
}
function dropIt(event:MouseEvent):void {
     event.target.stopDrag();
     var myTargetName:String = "target" + event.target.name;
     var myTarget:DisplayObject = getChildByName(myTargetName);
     if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
         reply_txt.text = "Good Job!";
         event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
         event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
         event.target.buttonMode = false;
         event.target.x = myTarget.x;
         event.target.y = myTarget.y;
         counter++;
     } else {
         reply_txt.text = "try again";
         event.target.x = startX;
         event.target.y = startY;
     }
        if(counter == 1){
        reply_txt.text = "You're Done";
    }
}

square_mc.buttonMode = true;

 
Now test your movie if everything is working properly.
Adding additional "dragable" objects.


----------------------------------------------------------------------------------------------------------------
OR USE THIS CODE
---------------------------------------------------------------------------------------------------------------------

var objectoriginalX:Number;
var objectoriginalY:Number;

polygon_mc.buttonMode = true;
polygon_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
polygon_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);

circle_mc.buttonMode = true;
circle_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
circle_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);

square_mc.buttonMode = true;
square_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
square_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);


function pickupObject(event:MouseEvent):void {
event.target.startDrag(true);
event.target.parent.addChild(event.target);
objectoriginalX = event.target.x;
objectoriginalY = event.target.y;
}
function dropObject(event:MouseEvent):void {
event.target.stopDrag();
var matchingTargetName:String = "target" + event.target.name;
var matchingTarget:DisplayObject = getChildByName(matchingTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == matchingTarget){
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropObject);
event.target.buttonMode = false;
event.target.x = matchingTarget.x;
event.target.y = matchingTarget.y;
else {
event.target.x = objectoriginalX;
event.target.y = objectoriginalY;
}}

-------------------------------------------------------------------------------------------------------------------
OR USE THIS CODE:
--------------------------------------------------------------------------------------------------------------------

//http://flashcollege.blogspot.co.uk/2014/09/hit-test-in-as3-and-score-count.html

var counter:Number = 0;
var startX:Number;
var startY:Number;

square_mc.buttonMode = true;
GoodJob.visible = false;
TryAgain.visible = false;

square_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
square_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);

function pickUp(event:MouseEvent):void {
event.target.startDrag(true);
reply_txt.text = "";
event.target.parent.addChild(event.target);
startX = event.target.x;
startY = event.target.y;
}
function dropIt(event:MouseEvent):void {
event.target.stopDrag();
var EventTargetName:String = event.target.name;
//HERE I REMOVE ABOVE CODE
if (event.target.dropTarget != null){
reply_txt.text = "Good Job!";
GoodJob.visible = true;
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
event.target.buttonMode = false;
event.target.x = event.target.x;
event.target.y = event.target.y;
counter++;
} else {
reply_txt.text = "try again";
TryAgain.visible = true;
event.target.x = startX;
event.target.y = startY;
}
if(counter == 1){
reply_txt.text = "You're Done";
}
}











EmoticonEmoticon