Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 08-02-2012, 08:33 AM   #1
MaxVergil
Registered User
 
Join Date: Aug 2012
Posts: 2
Default [AS3] hitTestObject help

I am trying to make a simple as3 game, but right now stuck at collision detection. Bullets spawn on key press, birds keep spawning on timer, but no matter where I use the hittest (I tried in the createBird, after each bullet was spawned, and in Main) it simply does not register the hit or gives null object reference errors. here is the code :

ActionScript Code:
package  {     import Classes.F_Bird;     import flash.display.Sprite;     import Classes.F_Bullet;     import Classes.F_Splatter;     import flash.events.KeyboardEvent;     import flash.ui.Keyboard;     import flash.utils.Timer;     import flash.events.TimerEvent;     import flash.display.MovieClip;             public class Main extends Sprite {                 public var flock:Array;         public var spawnTimer:Timer;         public var shootTimer:Timer;         public var shootL:int=1;         public var shootM:int=1;         public var shootR:int=1;         public var score:int=0;         var bullet:F_Bullet;         var newBird:F_Bird;                         public function Main() {                         flock = new Array();             spawnTimer= new Timer(800);             spawnTimer.addEventListener(TimerEvent.TIMER, createBird);             spawnTimer.start();             spawnTimer= new Timer(900);             spawnTimer.addEventListener(TimerEvent.TIMER, allowShoot);             spawnTimer.start();             var splatter1:F_Splatter;             splatter1=new F_Splatter(100, 100);             addChild(splatter1);             stage.addEventListener(KeyboardEvent.KEY_DOWN, processKeys);                                 }                 public function allowShoot(timerEvent:TimerEvent):void{             shootL=1             shootM=1             shootR=1         }                 public function createBird(timerEvent:TimerEvent):void{             var randomY:Number = Math.floor(Math.random()*71)+20;             var newBird=new F_Bird(20, randomY) ;             flock.push( newBird);             addChild(newBird);             if (newBird!=null){                                             if ( bullet.hitTestObject(newBird)){                                                 trace ("hit");                                     }             }                                                                         }                         private function processKeys(evt:KeyboardEvent):void{             if ((evt.keyCode== Keyboard.A) && (shootL==1)){                                 bullet=new F_Bullet(54, 350);                 addChild(bullet);                 shootL=0;                 }else if ((evt.keyCode==Keyboard.S) && (shootM==1)){                                 bullet=new F_Bullet(250, 350);                 addChild(bullet);                 shootM=0;             }else if ((evt.keyCode==Keyboard.D) && (shootR==1)){                                 bullet=new F_Bullet(450, 350);                 addChild(bullet);                 shootR=0;             }         }                     }     }
MaxVergil is offline   Reply With Quote
Old 08-02-2012, 08:45 AM   #2
MaxVergil
Registered User
 
Join Date: Aug 2012
Posts: 2
Default

I also tried spawning one single bullet at the main function so the program went through the hit test actually(found out in debugger that if statement was being skipped since bullet was not spawned it) but still, no trace of hit.
MaxVergil 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 11:17 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.