2014-01-14 20 views
0

好奇地知道我能否以某種方式在我的Flash遊戲中製作敵人影片剪輯後面跟隨播放器控制的影片剪輯,在這種情況下稱爲「播放器」。在這一刻,它只是步行-3在X和1.5在Y.製作影片剪輯'Enemy'關注'player',而不是在set xy軸上行走? Flash,AS3

不確定是否需要在這裏發佈任何代碼,以防萬一,這裏是我的代碼在敵人的電影剪輯。 在此先感謝!

import flash.events.MouseEvent; 

var catxSpeed:Number = -3; 
var catySpeed:Number = 1.5; 
var myParent:*; 
var ground:Number; 
var jumping:Boolean = false; 
var health:Number; 

this.addEventListener(Event.ENTER_FRAME, updatecat); 

function updatecat(event:Event):void 
{ 
this.x += catxSpeed; 
if(myParent.player.hitTestObject(this)) 
{ 
    myParent.hit(); 
} 
this.y += catySpeed; 
if(jumping == true) 
{ 
    catySpeed +=1; 
    if(this.y >= ground) 
    { 
     catxSpeed = -3; 
     catySpeed = 0; 
     this.y = ground; 
     jumping = false; 
     if(health < 1) 
     { 
      shutDown(); 
     } 
    } 
} 
} 

function activate(passParent:*):void 
{ 
ground = this.y; 
this.addEventListener(Event.ENTER_FRAME, updatecat); 
this.addEventListener(MouseEvent.MOUSE_DOWN, hit); 
myParent = passParent; 
health = Math.round(myParent.randomise(2,4)); 
} 

function shutDown():void 
{ 
this.removeEventListener(Event.ENTER_FRAME, updatecat); 
this.parent.removeChild(this); 
} 

function hit(event:MouseEvent):void 
{ 
catxSpeed = 30; 
catySpeed = -15; 
jumping = true; 
myParent.addToScore(1); 
health--; 
} 

回答

0
if (enemy.x > this.x) this.x += catxSpeed; 
else this.x -= catxSpeed; 


if (enemy.y > this.y) this.y += catySpeed; 
else this.y -= catySpeed; 
+0

我應將了 '玩家' 裏面? 對不起,如何實現它/它如何跟隨'玩家'實例?對不起,我對此比較陌生!你介意穿過它嗎? – Ryo123987

+0

我建議有一個控制器/主/調解員類,它可以讓敵人和貓實例化。在控制器中最好只有一個偵聽器stage.ENTER_FRAME。控制器告訴貓/敵人如何移動,並檢查命中。保持貓和敵人的類別愚蠢,並將所有邏輯移至控制器。 –