2013-05-08 146 views
0

我正在创建一个游戏,如果您遇到僵尸,僵尸就会死亡,并且他的头部会向您发出的方向发回。这就像打僵尸头与棒球一样,头是球。我有两个movieclip僵尸和僵尸头。一旦僵尸被击中,它将发挥其垂死的动画并将其自身移除,同时僵尸头部将被添加到垂死僵尸的x和y坐标中,并像打棒球一样被吹倒。我已经完成了关于命中和僵尸死亡和重生的代码,但是当它被击中时,我似乎无法定位并将头部添加到垂死的僵尸中。我已经完成了僵尸头部的功能,但我怎么能把它添加到僵尸。我认为这会是这样,我加入这个在僵尸类的PlayDeathAnimation功能,但没有奏效:将动画片段定位到另一个动画片段2

for (var i=0; i < 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); 

} 

我甚至尝试了这一点。头部产卵,但只是保持静止,不会飞回来,当僵尸移除自己时,它会自行移除,我不想这样做。我已经告诉ZombieHead时本身删除自己的类

var zh = new ZombieHead();    
addChild(zh); 

这是我到目前为止 球员

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); 
      // The lines below draw a preview of your 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 is 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 is true then key moves are false; 
      if ((attacking == true)) 
      { 
       walkRight = false; 
       walkLeft = false; 
       walkUp = false; 
       walkDown = false; 
       // see if the zombie is in the cone 

       for (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 is false then move and rotate to 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 the distance between the two 
     public function distanceBetween(player:MovieClip,zombies:MovieClip):Number 
     { 
      for (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 for the 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); 
      } 
     } 
    } 
} 

做僵尸

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 at its 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 the direction the zombie is facing 
       x = x+Math.cos(rotation/180*Math.PI)*walkSpeed; 
       y = y+Math.sin(rotation/180*Math.PI)*walkSpeed; 
       //Calculate the distance to 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() 
     { 
      for (var i=0; i < MovieClip(parent).zombies.length; ++i) 
      { 
       if (MovieClip(parent).zombies[i] == this) 
       { 
        MovieClip(parent).zombies.splice(i, 1); 
        break; 
       } 
      } 
      MovieClip(parent).removeChild(this); 
     } 
    } 

} 

ZombieHead

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); 
       } 
     } 

    } 

} 

文档类

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 a Zombie if there are less than number 
      if (numChildren < 4) 
      { 
       //Make a new instance of the Zombie. 
       var Z = new Zombie(); 
       addChild(Z); 

       //Position and rotate the zombie 
       Z.x = Math.random() * stage.stageWidth; 
       Z.y = Math.random() * stage.stageHeight; 
       Z.rotation = Math.random() * 360; 

       zombies.push(Z); 
      } 
      for (var count = 0; count<zombies.length; count ++) 
      { 
       zombies[count].loop(); 

      } 
     } 
    } 
} 

回答

0

您需要跟踪的x,y值的头跟踪身体,一个enterFrame事件函数会做: 例如:

 addEventListner(event.ENTER_FRAME,follow head); 

     function (event.ENTER_FRAME){ 
       myhead.x == mybody.x 
       myhead.y == mybody.y 
       } 

基本上thehead将跟随身体x,y值每帧或每帧周期内。

0

你显然是将头部添加到僵尸,而不是持有者类,然后,当你移除僵尸时,你也删除它的头部,因为头部是僵尸的孩子。不要将其添加到僵尸母体:

parent.addChild(zh); 

此外,为了搬完头,父类应该知道到达的头。所以,当你添加一个头部,并且你有一个僵尸阵列时,制作一个头部阵列,这些头部会在头部生成时填充,当你希望头部消失时将会消失,然后像你一样遍历它已经与僵尸做。

+0

它几乎奏效,这就是我所做的。如果(inAttackCone(MovieClip(parent).zombies [i])在播放器类 中(var i:int = MovieClip(parent).zombies.length-1; i> = 0; i - ){ \t ){ \t \t如果(hitTestObject(影片剪辑(父).zombies [I])){ \t \t \t的MovieClip(父).zombies [I] .zombieDead = TRUE; \t \t var zh = new ZombieHead; \t \t \t zh.x = MovieClip(parent).zombies [i]。X; \t \t \t zh.y = MovieClip(parent).zombies [i] .y; \t \t \t zh.rotation = MovieClip(parent).zombies [i] .rotation; \t \t \t parent.addChild(zh); \t \t \t} \t \t} \t}} 但 – user2350724 2013-05-08 23:14:26

+0

的问题是ZombieHead将重新本身多次,直到僵尸已经死了。头部的功能就像它假设的那样起作用并摆脱它自身的方式,但我认为只要僵尸精灵= true,它就会不断地重建自己,直到僵尸精灵失效。我怎么能想出一个解决方案,只有一个头将被添加时,僵尸程序是真实的。 – user2350724 2013-05-08 23:17:44

+0

没关系,我得到它的工作,谢谢sooooo很多我被卡住了一个星期。我只需要做parent.addChild并改变一些布尔值 – user2350724 2013-05-08 23:24:02

相关问题