Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 04-12-2012, 10:58 PM   #1
AnimatedCircle
Registered User
 
Join Date: Apr 2012
Location: Manchester
Posts: 21
Exclamation [AS3] Hit Test Object Help

Hi ii want my character to walk down the screen and hit the objectWall and then a movieclip then gets added to the stage and plays but it doesnt seem to work and ii dont know why.

ActionScript Code:
import flash.display.MovieClip; stop(); //1. Set booleans for the status of the arrow presses var downArrow:Boolean = false; //2. Add event listeners for the keys stage.addEventListener(KeyboardEvent.KEY_UP, nokeyHit); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit); stage.addEventListener(Event.ENTER_FRAME, moveObject); //3. Function to detect if a key has been hit function keyHit(event:KeyboardEvent):void {     switch (event.keyCode)     {         case Keyboard.DOWN :             downArrow = true;             break;     } } //4. Function to detect if a key has not been hit function nokeyHit(event:KeyboardEvent):void {     switch (event.keyCode)     {         case Keyboard.DOWN :             downArrow = false;             break;     } } //5. Function to move the object function moveObject(event:Event):void {     var speed:Number = 2;     if (downArrow)     {         objectMove.y +=  speed;     }     hitWalls(); } //6. Function to test hitting the walls function hitWalls():void {     var left:Number = 0;     var right:Number = stage.stageWidth;     var top:Number = 0;     var bottom:Number = stage.stageHeight;     if (objectMove.x  - (objectMove.width / 2) > right)     {         objectMove.x = left - objectMove.width / 2;     }     else if (objectMove.x + objectMove.width / 2 < left)     {         objectMove.x = right + objectMove.width / 2;     }     if (objectMove.y + (objectMove.height / 2) > bottom)     {         objectMove.y = bottom - (objectMove.height / 2);     }     else if (objectMove.y - (objectMove.height / 2) < top)     {         objectMove.y = top + (objectMove.height / 2);     } } objectMove.addEventListener(Event.ENTER_FRAME, testHit); function testHit(event:Event):void {     var Attack:MovieClip = new MovieClip;         if (objectMove.hitTestObject(objectWall))     {         objectMove.removeEventListener(Event.ENTER_FRAME, testHit);         addChild(Attack);     } }

Any help would be great.. thank you.
AnimatedCircle is offline   Reply With Quote
Old 04-13-2012, 10:47 AM   #2
AnimatedCircle
Registered User
 
Join Date: Apr 2012
Location: Manchester
Posts: 21
Default Hit Test Object Help

Hi ii want my character to walk down the screen and hit the objectWall and then a movieclip then gets added to the stage and plays but it doesnt seem to work and ii dont know why.

ActionScript Code:
import flash.display.MovieClip; stop(); //1. Set booleans for the status of the arrow presses var downArrow:Boolean = false; //2. Add event listeners for the keys stage.addEventListener(KeyboardEvent.KEY_UP, nokeyHit); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit); stage.addEventListener(Event.ENTER_FRAME, moveObject); //3. Function to detect if a key has been hit function keyHit(event:KeyboardEvent):void {     switch (event.keyCode)     {         case Keyboard.DOWN :             downArrow = true;             break;     } } //4. Function to detect if a key has not been hit function nokeyHit(event:KeyboardEvent):void {     switch (event.keyCode)     {         case Keyboard.DOWN :             downArrow = false;             break;     } } //5. Function to move the object function moveObject(event:Event):void {     var speed:Number = 2;     if (downArrow)     {         objectMove.y +=  speed;     }     hitWalls(); } //6. Function to test hitting the walls function hitWalls():void {     var left:Number = 0;     var right:Number = stage.stageWidth;     var top:Number = 0;     var bottom:Number = stage.stageHeight;     if (objectMove.x  - (objectMove.width / 2) > right)     {         objectMove.x = left - objectMove.width / 2;     }     else if (objectMove.x + objectMove.width / 2 < left)     {         objectMove.x = right + objectMove.width / 2;     }     if (objectMove.y + (objectMove.height / 2) > bottom)     {         objectMove.y = bottom - (objectMove.height / 2);     }     else if (objectMove.y - (objectMove.height / 2) < top)     {         objectMove.y = top + (objectMove.height / 2);     } } objectMove.addEventListener(Event.ENTER_FRAME, testHit); function testHit(event:Event):void {     var Attack:MovieClip = new MovieClip;         if (objectMove.hitTestObject(objectWall))     {         objectMove.removeEventListener(Event.ENTER_FRAME, testHit);         addChild(Attack);     } }

Any help would be great.. thank you.
AnimatedCircle is offline   Reply With Quote
Old 04-17-2012, 06:55 PM   #3
TessaLee
Registered User
 
Join Date: Apr 2012
Posts: 7
Default

Well... I've noticed that you have two ENTER_FRAME events happening; moveObject and testHit, and moveObject is calling the function hitWalls every frame. I usually don't like having two separate functions for ENTER_FRAME, because whatever they're both doing can most likely be called from the same function that determines the game loop. But I see what you're trying to do here. moveObject is self-explanatory, hitTest is where you're checking for collision, and I believe hitWalls is where you're checking to see if you've hit a wall and are pushing them back from it.

Ok, so try something like this:
ActionScript Code:
import flash.display.MovieClip; var downArrow:Boolean = false; var speed:Number = 2; var left:Number = 0; var right:Number = stage.stageWidth; var top:Number = 0; var bottom:Number = stage.stageHeight; var Attack:MovieClip = new MovieClip(); var isHit:Boolean = false;//TessaLee added this stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit); stage.addEventListener(KeyboardEvent.KEY_UP, nokeyHit); stage.addEventListener(Event.ENTER_FRAME, movieLoop); function keyHit (event:KeyboardEvent):void {      switch (event.keyCode)      {           case Keyboard.DOWN :                downArrow = true;                break;      } } function nokeyHit(event:KeyboardEvent):void {      switch (event.keyCode)      {           case Keyboard.DOWN :                downArrow = false;                break;      } } function movieLoop(event:Event) {      if(downArrow == true)      {           objectMove.y += speed;  //Moves the object           hitWalls()//Collision Check           if(isHit == false)  //Will only be true if we hit the test object           {                testHit()//Collision Check           }      } } function hitWalls() {      //Put all of your if and else if statements for the function here } function testHit() {      if(objectMove.hitTestObject(objectWall))      {           isHit = true; //No longer runs this function in the movie loop           //You can also disable the entire movie loop if you don't need to control movement anymore by doing what you did before with removing the entire listener.           addChild(Attack);      } }

I hope this helps (:

Last edited by TessaLee; 04-17-2012 at 07:37 PM.
TessaLee is offline   Reply With Quote
Old 04-17-2012, 06:58 PM   #4
TessaLee
Registered User
 
Join Date: Apr 2012
Posts: 7
Default

Aside from all of the code suggestion I just made, I noticed that you're missing the parenthesis when declaring a new MovieClip variable. Should be like this:

ActionScript Code:
var Attack:MovieClip = new MovieClip();

Take care!
TessaLee is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:28 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.