以下代碼呈現一個可互換座標的殭屍。唯一的問題是我必須複製代碼併爲每個殭屍自己創建新的座標變量。有什麼辦法可以創建變量的副本,所以我只需要一個方法爲多個殭屍對三個殭屍的三個方法和三個單獨的變量?方法的多個實例?
int zombieyCord;
int zombiexCord;
-(void)renderZombie
{
NSBezierPath * path = [NSBezierPath bezierPath];
[path setLineWidth:4];
NSPoint center = {zombieyCord,zombiexCord};
[path moveToPoint: center];
[path appendBezierPathWithArcWithCenter:center
radius:5
startAngle:0
endAngle:360];
[[NSColor greenColor] set];
[path fill];
[[NSColor greenColor] set];
[path stroke];
}
編輯:
產卵是偉大的,完美的作品,但我想殭屍正確遷移。之前,代碼是該方法被調用,玩家移動。這不再有效,因爲我必須在方法中調用一個整數。
+(void)entityZombie:(int)zombiexCord andY:(int)zombieyCord
{
/* Handle Zombie Movement*/
if (xcord != zombiexCord || ycord != zombieyCord)
{
if (xcord > zombiexCord){zombiexCord=zombiexCord+1;}
if (xcord < zombiexCord){zombiexCord=zombiexCord-1;}
if (ycord < zombieyCord){zombieyCord=zombieyCord-1;}
if (ycord > zombieyCord){zombieyCord=zombieyCord+1;}
/* Handle Zombie Render*/
NSBezierPath * path = [NSBezierPath bezierPath];
[path setLineWidth:4];
NSPoint center = {zombieyCord,zombiexCord};
[path moveToPoint: center];
[path appendBezierPathWithArcWithCenter:center
radius:5
startAngle:0
endAngle:360];
[[NSColor greenColor] set];
[path fill];
[[NSColor greenColor] set];
[path stroke];
}
}
玩家移動處理只涉及渲染玩家然後[Entity entityZombie]; 但如上所述,不再有效。
我是否正確地認爲'{xcord,ycord}'是你的玩家的座標,並且你希望殭屍在每次迭代時向玩家移動一步,然後在新的位置渲染? – Regexident 2011-05-23 00:19:14
是的,確實如此。 :-) – evdude100 2011-05-23 00:21:34
@Regexident所以我該怎麼做? – evdude100 2011-05-23 00:41:16