actionscript 3 - how to reference a hit test object between .as files [AS3] -
i'm trying make top down shooter game, , have been following tutorials here: http://gamedev.michaeljameswilliams.com/2008/09/17/avoider-game-tutorial-1/ , here: as3gametuts.com/2013/07/10/top-down-rpg-shooter-4-shooting/
i've managed shooting , movement, need hit test object register when bullet (defined in own seperate class file) , enemy (also defined in seperate file) come contact. code below:
enemy code:
package { import flash.display.movieclip; public class enemy extends movieclip { public function enemy() { x = 100; y = -15; } public function movedownabit():void { y = y + 3; } } }
bullet code:
package { import flash.display.stage; import flash.display.movieclip; import flash.events.event; import flash.utils.timer; import flash.events.timerevent; public class bullet extends movieclip { private var stageref:stage; private var speed:number = 10; private var xvel:number = 0; private var yvel:number = 0; private var rotationinradians = 0; public var enemy:enemy; public function bullet(stageref:stage, x:int, y:int, rotationindegrees:number):void { this.stageref = stageref; this.x = x; this.y = y; this.rotation = rotationindegrees; this.rotationinradians = rotationindegrees * math.pi / 180; } public function bullethit():void{ if (bullet.hittestobject(enemy)){ gametimer.stop(); } } public function loop():void { xvel = math.cos(rotationinradians) * speed; yvel = math.sin(rotationinradians) * speed; x += xvel; y += yvel; if(x > stageref.stagewidth || x < 0 || y > stageref.stageheight || y < 0) { this.parent.removechild(this); } } } }
main.as document class code:
package { import flash.display.stage; import flash.display.movieclip; import flash.events.event; import flash.events.mouseevent; import flash.utils.timer; import flash.events.timerevent; public class main extends movieclip { public var player:player; public var bulletlist:array = []; //new array bullets public var enemy:enemy; public var gametimer:timer; public function main():void { player = new player(stage, 320, 240); stage.addchild(player); enemy = new enemy(); addchild( enemy ); gametimer = new timer( 25 ); gametimer.addeventlistener( timerevent.timer, moveenemy ); gametimer.start(); stage.addeventlistener(mouseevent.click, shootbullet, false, 0, true); stage.addeventlistener(event.enter_frame, loop, false, 0, true); //add eventlistener loop } public function moveenemy( timerevent:timerevent ):void { enemy.movedownabit(); } public function loop(e:event):void //create loop function { if(bulletlist.length > 0) //if there bullets in bullet list { for(var i:int = bulletlist.length-1; >= 0; i--) //for each 1 { bulletlist[i].loop(); //call loop() function } } } public function shootbullet(e:mouseevent):void { var bullet:bullet = new bullet(stage, player.x, player.y, player.rotation); bullet.addeventlistener(event.removed_from_stage, bulletremoved, false, 0, true); //triggers "bulletremoved()" function whenever bullet removed stage bulletlist.push(bullet); //add bullet bulletlist array stage.addchild(bullet); } public function bulletremoved(e:event):void { e.currenttarget.removeeventlistener(event.removed_from_stage, bulletremoved); //remove event listener don't errors bulletlist.splice(bulletlist.indexof(e.currenttarget),1); //remove bullet bulletlist array } } }
as vesper said, you'll want checks in main
class. you've got game loop set up, can add check in there:
public function loop(e:event):void //create loop function { if(bulletlist.length > 0) //if there bullets in bullet list { for(var i:int = bulletlist.length-1; >= 0; i--) //for each 1 { bulletlist[i].loop(); //call loop() function // check see if enemy has been hit if(enemy.hittestobject(bulletlist[i])) { // enemy has been hit bullet @ index } } } }
since have single enemy
, you're testing each bullet against 1 enemy
. if had more enemies, you'd want keep array of references enemies , nested loop, checking see if of enemies hit of bullets.
Comments
Post a Comment