2011-05-22 62 views
1

以下代碼呈現一個可互換座標的殭屍。唯一的問題是我必須複製代碼併爲每個殭屍自己創建新的座標變量。有什麼辦法可以創建變量的副本,所以我只需要一個方法爲多個殭屍對三個殭屍的三個方法和三個單獨的變量?方法的多個實例?

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]; 但如上所述,不再有效。

+1

我是否正確地認爲'{xcord,ycord}'是你的玩家的座標,並且你希望殭屍在每次迭代時向玩家移動一步,然後在新的位置渲染? – Regexident 2011-05-23 00:19:14

+0

是的,確實如此。 :-) – evdude100 2011-05-23 00:21:34

+0

@Regexident所以我該怎麼做? – evdude100 2011-05-23 00:41:16

回答

5
-(void)renderZombieWithX:(int)xCoord andY:(int)yCoord { 
    NSBezierPath * path = [NSBezierPath bezierPath]; 
    [path setLineWidth:4]; 
    NSPoint center = {xCoord,yCoord}; 
    [path moveToPoint: center]; 
    [path appendBezierPathWithArcWithCenter:center 
            radius:5 
           startAngle:0 
            endAngle:360]; 
    [[NSColor greenColor] set]; 
    [path fill]; 
    [[NSColor greenColor] set]; 
    [path stroke]; 
} 

這樣稱呼它:

[self renderZombieWithX:10 andY:20]; 
[self renderZombieWithX:13 andY:37]; 
//... 

而且更沒有用於創建NSPoints一個dedicated function

NSPoint center = NSMakePoint(xCoord, yCoord); 

,你可能想贊成使用:

NSPoint center = {xCoord,yCoord}; 

(見爲什麼我的答案的評論之一)


如果性能是至關重要的一個緩存的方法類似下面可能帶來的性能增益:

static NSImage *sharedZombieStamp; 
- (NSImage *)sharedZombie { 
    @synchronized(sharedZombieStamp) { 
     if (!sharedZombieStamp) { 
      CGFloat lineWidth = 4.0; 
      CGFloat radius = 5.0; 
      sharedZombieStamp = [[NSImage alloc] initWithSize:NSMakeSize((lineWidth + radius) * 2, (lineWidth + radius) * 2]; 

      [zombieImage lockFocus]; 
      NSBezierPath *path = [NSBezierPath bezierPath]; 
      [path setLineWidth:4]; 
      NSPoint center = NSMakePoint(lineWidth + radius, lineWidth + radius); 
      [path moveToPoint: center]; 
      [path appendBezierPathWithArcWithCenter:center 
              radius:radius 
             startAngle:0 
              endAngle:360]; 
      [[NSColor greenColor] set]; 
      [path fill]; 
      [[NSColor greenColor] set]; 
      [path stroke]; 
      [zombieImage unlockFocus]; 
     } 
    } 
    return sharedZombieStamp; 
} 

-(void)renderZombieWithX:(int)xCoord andY:(int)yCoord { 
    NSImage *zombie = [self sharedZombie]; 
    NSSize zombieSize = [zombie size]; 
    [zombie drawAtPoint:NSMakePoint(xCoord - zombieSize.width/2, yCoord - zombieSize.height/2) 
       fromRect:NSMakeRect(0.0, 0.0, zombieSize.width, zombieSize.height) 
       operation:NSCompositeSourceOver 
       fraction:1.0]; 
} 

此代碼是剛剛走出我的頭,雖然。無論如何,我都沒有通過測試。謹慎使用;)

+0

非常感謝! – evdude100 2011-05-22 20:36:39

+0

不,你這樣稱呼:'[self renderZombieWithX:10 andY:20];',正如在我的答案中所述;) – Regexident 2011-05-22 20:38:13

+0

對不起,您發佈後立即編輯:-) – evdude100 2011-05-22 20:39:02