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

4

1 回答 1

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的专用函数:

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

不过,这段代码让我想不通。没有通过任何测试。谨慎使用;)

于 2011-05-22T20:34:04.050 回答