arrays - Positioning a movieclip onto another movieclip -
i'm creating game if hit zombie zombie dies , head sent in direction hit. have 2 movieclips zombie , zombie head. once zombie hit play dying animation , remove , @ same time zombie head added dying zombie , blown back. have done code hittest , zombie dying , respawning can't seem position , add head dying zombie when hit. how can this. thought this, added in playdeathanimation function in zombie class did not work:
for (var i=0; < movieclip(parent).zombies.length; ++i) { var zh = new zombiehead(); zh.x = movieclip(parent).zombies[i].x; zh.y = movieclip(parent).zombies[i].y; zh.rotation = movieclip(parent).zombies[i].rotation; addchild(zh); }
i have 4 classes
player
package { import flash.display.movieclip; import flash.events.event; import flash.events.keyboardevent; import flash.events.mouseevent; import flash.ui.keyboard; import flash.display.graphics; import flash.utils.settimeout; public class player extends movieclip { //player setting var walkspeed:number = 4; var walkright:boolean = false; var walkleft:boolean = false; var walkup:boolean = false; var walkdown:boolean = false; var attacking:boolean = false; var attackrange:int = 100; var attackangle:int = 30; public function player() { stage.addeventlistener(keyboardevent.key_down,walk); addeventlistener(event.enter_frame,update); stage.addeventlistener(keyboardevent.key_up,stopwalk); stage.addeventlistener(mouseevent.click,attack); // lines below draw preview of attack area graphics.beginfill(0x00ff00, 0.2); graphics.lineto(attackrange*math.cos((rotation-attackangle)/180*math.pi),attackrange*math.sin((rotation-attackangle)/180*math.pi)); graphics.lineto(attackrange*math.cos((rotation+attackangle)/180*math.pi),attackrange*math.sin((rotation+attackangle)/180*math.pi)); graphics.endfill(); } function walk(event:keyboardevent) { //when key down if (event.keycode == 68) { walkright = true; } if (event.keycode == 87) { walkup = true; } if (event.keycode == 65) { walkleft = true; } if (event.keycode == 83) { walkdown = true; } } function update(event:event) { //if attacking true key moves false; if ((attacking == true)) { walkright = false; walkleft = false; walkup = false; walkdown = false; // see if zombie in cone (var i:int=movieclip(parent).zombies.length-1; i>=0; i--) { if (inattackcone(movieclip(parent).zombies[i])) { if (hittestobject(movieclip(parent).zombies[i])) { //attacking = true; movieclip(parent).zombies[i].zombiedead = true; } } } } else if ((attacking == false)) { //else if attacking false move , rotate mouse; var dx = parent.mousex - x; var dy = parent.mousey - y; var angle = math.atan2(dy,dx) / math.pi * 180; rotation = angle; if ((walkright == true)) { x += walkspeed; gotoandstop(2); } if ((walkup == true)) { y -= walkspeed; gotoandstop(2); } if ((walkleft == true)) { x -= walkspeed; gotoandstop(2); } if ((walkdown == true)) { y += walkspeed; gotoandstop(2); } } } //calculate distance between 2 public function distancebetween(player:movieclip,zombies:movieclip):number { (var i:int=movieclip(parent).zombies.length-1; i>=0; i--) { var dx:number = x - movieclip(parent).zombies[i].x; var dy:number = y - movieclip(parent).zombies[i].y; } return math.sqrt(((dx * dx) + dy * dy)); } public function angledifference(a:object, b:object):number { var dx:number = b.x - a.x; var dy:number = b.y - a.y; var ang:number = (a.rotation/180*math.pi)-math.atan2(dy, dx); while (ang>math.pi) { ang -= 2 * math.pi; } while (ang<-math.pi) { ang += 2 * math.pi; } return math.abs(ang*180/math.pi); } function inattackcone(enemy:movieclip):boolean { var distance:number = distancebetween(this,enemy); distance -= enemy.width / 2;// account enemy's size if (distance < attackrange) { // in range, check angle if (angledifference(this,enemy)<attackangle) { return true; } } return false; } function stopwalk(event:keyboardevent) { if ((attacking == false)) { if (event.keycode == 68) { event.keycode = 0; walkright = false; gotoandstop(1); } if (event.keycode == 87) { event.keycode = 0; walkup = false; gotoandstop(1); } if (event.keycode == 65) { event.keycode = 0; walkleft = false; gotoandstop(1); } if (event.keycode == 83) { event.keycode = 0; walkdown = false; gotoandstop(1); } } } function attack(event:mouseevent) { if ((attacking == false)) { attacking = true; gotoandstop(3); } } } }
zombies
package { import flash.display.movieclip; import flash.events.event; import flash.geom.point; public class zombie extends movieclip { var walkspeed:number = 2; var target:point; public var zombiedead:boolean = false; public function zombie() { //set target location of zombie target = new point(math.random() * 500,math.random() * 500); } function loop() { if (zombiedead == true) { playdeathanimation(); } else if (zombiedead == false) { gotoandstop(1); //point zombie @ target var dx = movieclip(root).player01.x - x; var dy = movieclip(root).player01.y - y; var angle = math.atan2(dy,dx) / math.pi * 180; rotation = angle; //move in direction zombie facing x = x+math.cos(rotation/180*math.pi)*walkspeed; y = y+math.sin(rotation/180*math.pi)*walkspeed; //calculate distance target var hyp = math.sqrt((dx*dx)+(dy*dy)); if (hyp<5) { target.x = math.random() * 500; target.y = math.random() * 500; } } } public function playdeathanimation() { gotoandstop(2); } public function deathanimationfinishedcallback() { (var i=0; < movieclip(parent).zombies.length; ++i) { if (movieclip(parent).zombies[i] == this) { movieclip(parent).zombies.splice(i, 1); break; } } movieclip(parent).removechild(this); } } }
document class
package { import flash.display.movieclip; import flash.events.event; public class game extends movieclip { public var zombies:array; public function game() { addeventlistener(event.enter_frame, update); zombies = new array(); } public function update(e:event) { //only spawn zombie if there less number if (numchildren < 4) { //make new instance of zombie. var z = new zombie(); addchild(z); //position , rotate zombie z.x = math.random() * stage.stagewidth; z.y = math.random() * stage.stageheight; z.rotation = math.random() * 360; zombies.push(z); } (var count = 0; count<zombies.length; count ++) { zombies[count].loop(); } } } }
zombie head
package { import flash.display.movieclip; import flash.events.event; public class zombiehead extends movieclip { var headspeed:number = -30; var friction:number = 0.9; public function zombiehead() { // constructor code addeventlistener(event.enter_frame,update); } function update(event:event) { x = x+math.cos(rotation/180*math.pi)*headspeed; y = y+math.sin(rotation/180*math.pi)*headspeed; headspeed *= friction; if (headspeed > -0.00001) { removeeventlistener(event.enter_frame,update); parent.removechild(this); } else if (x<0 || x > 550 || y < 0 || y > 400) { removeeventlistener(event.enter_frame,update); parent.removechild(this); } } } }
Comments
Post a Comment