actionscript 3 - Bullet fires perfectly in three directions, but not left -
i'm quite new actionscript, , have been scrapping little test game sort of used language, feet wet. premise behind game pretty average, have character can move in 8 directions, can pick several keys randomly placed, , open door when possesses key.
the problem comes in play bullet shooting. don't know trigonometry, when key pressed, use .rotate property turn player. pass .rotate integer bullet class , use tell direction bullet should travel. works up, down, , right movements, when character facing left bullet created has no velocity whatsoever, , life of me cannot figure out error in code is.
if on code , me out, appreciated. i'm sure it's simple i'm missing. know it's bit sloppy, if there's other tips want pass on novice please feel free!!
thank guys.
main class
package { import flash.display.movieclip; import flash.events.event; import flash.events.keyboardevent; import flash.ui.keyboard; import flash.text.textfield; public class main extends movieclip { var player:player; var inventory:inventory; var invarray:array = new array(); var key:_key; var vx:int; var maxspeed:int; var key_up:boolean; var key_down:boolean; var key_left:boolean; var key_right:boolean; var maxkey:int; var keycount:textfield; var keysup:int; var door:door; var dooropen:boolean; var wall1:wall; var wall2:wall; var wallcollide:boolean; var bullettime:int; var bulletlimit:int; var bulletshoot:boolean; static var playerrotation:int; public function main() { init(); } public function init():void { stage.addeventlistener(event.enter_frame, gameloop); stage.addeventlistener(keyboardevent.key_down, keydownhandler); stage.addeventlistener(keyboardevent.key_up, keyuphandler); initplayer(); initvariables(); initinventory(); inititems(); } public function gameloop(e:event):void { moveplayer(); collisiondetection(); bullet(); } public function keydownhandler(e:keyboardevent):void { switch (e.keycode) { case 37 : key_left = true; break; case 38 : key_up = true; break; case 39 : key_right = true; break; case 40 : key_down = true; break; case 32: if(bulletshoot) { bulletshoot = false; var newbullet:bullet = new bullet(player.rotation); newbullet.x = player.x; newbullet.y = player.y; addchild(newbullet); } } } public function keyuphandler(e:keyboardevent):void { switch (e.keycode) { case 37 : key_left = false; break; case 38 : key_up = false; break; case 39 : key_right = false; break; case 40 : key_down = false; break; } } public function moveplayer():void { if (key_left && !key_right) { player.x -= maxspeed; player.rotation = 270; } if (key_right && !key_left) { player.x += maxspeed; player.rotation = 90; } if (key_up && !key_down) { player.y -= maxspeed; player.rotation = 0; } if (key_down && !key_up) { player.y += maxspeed; player.rotation = 180; } /*if ( key_left && key_up && !key_right && !key_down ) { player.rotation = 315; } if ( key_right && key_up && !key_left && !key_down ) { player.rotation = 45; } if ( key_left && key_down && !key_right && !key_up ) { player.rotation = 225; } if ( key_right && key_down && !key_left && !key_up ) { player.rotation = 135; }*/ } public function initplayer():void { player = new player(); player.x = stage.stagewidth * .5; player.y = stage.stageheight * .5; stage.addchild(player); } public function initvariables():void { vx = 0; maxspeed = 4; key_up = false; key_down = false; key_left = false; key_right = false; maxkey = 3; keysup = 0; dooropen = false; wallcollide = false; bullettime = 0; bulletlimit = 12; bulletshoot = true ; } public function collisiondetection():void { (var i:int=0; < invarray.length; i++) { key = invarray[i]; if (player.hittestobject(key)) { stage.removechild(key); invarray.splice(i, 1); keysup++; //trace(keysup); i--; keycount.text = string(keysup); break; } } if (player.hittestpoint(door.x,door.y + 25,true) && (keysup > 0) && ! dooropen) { door.gotoandstop(2); keysup--; invarray.pop(); trace(keysup); keycount.text = string(keysup); dooropen = true; } if (player.hittestobject(door) && (keysup == 0) && ! dooropen) { wallcollide = true; } if (player.hittestobject(wall1)) { wallcollide = true; } if (player.hittestobject(wall2)) { wallcollide = true; } if (wallcollide == true) { player.y += 4; wallcollide = false; } } public function initinventory():void { inventory = new inventory(); inventory.x = stage.stagewidth * .15; inventory.y = stage.stageheight * .90; stage.addchild(inventory); keycount = new textfield(); stage.addchild(keycount); keycount.x = inventory.x - 8; keycount.y = inventory.y + 3; keycount.text = string(keysup); //keycount.border = true; keycount.width = 20; keycount.height = 20; } public function inititems():void { while (invarray.length < maxkey) { key = new _key ; key.x = math.random() * 550; key.y = math.random() * 300; stage.addchild(key); invarray.push(key); } door = new door(); door.x = 250; door.y = 25; wall1 = new wall(); stage.addchild(wall1); wall1.x = door.x - 175; wall1.y = door.y; wall2 = new wall(); stage.addchild(wall2); wall2.x = door.x + 175; wall2.y = door.y; stage.addchild(door); } public function bullet():void { if (bullettime < bulletlimit) { bullettime++; } else { bulletshoot = true; bullettime = 0; } } } } bullet class
package { import flash.display.movieclip; import flash.events.event; public class bullet extends movieclip { public var _root:object; public var speed:int = 10; public var bulletrotation:int; public function bullet(protation:int) { addeventlistener(event.added, beginclass); addeventlistener(event.enter_frame, eframe); bulletrotation = protation; } private function beginclass(e:event):void { _root = movieclip(root); } private function eframe(e:event):void { if (bulletrotation == 0) { this.y -= speed; } else if (bulletrotation == 90) { this.x += speed; } else if(bulletrotation == 270) { this.x -= speed; } else if(bulletrotation == 180) { this.y += speed; } if(this.y < -1 * this.height) { removeeventlistener(event.enter_frame, eframe); _root.removechild(this); } if(this.x < -1 * this.width) { removeeventlistener(event.enter_frame, eframe); _root.removechild(this); } } } }
in bullet class, change 270 -90, @ line 38:
else if(bulletrotation == -90) { this.x -= speed; }
Comments
Post a Comment