方法的多个实例?

时间:2011-05-22 20:32:07

标签: objective-c methods multiple-instances

以下代码呈现具有可互换坐标的僵尸。唯一的问题是我必须自己复制代码并为每个僵尸创建新的坐标变量。有什么方法可以创建变量的副本,所以我只需要有一个方法可以为多个僵尸提供三个方法和三个独立变量三个僵尸吗?

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 个答案:

答案 0 :(得分: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];
//...

还有一个dedicated function用于创建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];
}

这段代码虽然不在我的脑海里。无论如何都没有通过任何测试。谨慎使用;)