开发者

Multiple instances of a method?

The following code ren开发者_JS百科ders a zombie with interchangeable coordinates. The only issue is that I have to duplicate the code and create new coordinate variables myself for each zombie. Is there any way I can create copies of the variables so I only have to have one method for multiple zombies verses three methods and three separate variables for three zombies?

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

}

Edit:

The spawning is great and works perfectly, but I want the zombies to move properly. Before, the code was that the method was called upon the player moving. This no longer works because I have to call an integer in the method.

+(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];

    }
}

Player movement handling just involves rendering the player then [Entity entityZombie]; But as said above, no longer works.


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

Call it like this:

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

Further more there is a dedicated function for creating NSPoints:

NSPoint center = NSMakePoint(xCoord, yCoord);

which you might want to use in favor of:

NSPoint center = {xCoord,yCoord};

(see one of my answer's comments on why)


If performance is critical a cached approach like the following might bring performance gain:

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

This code is just out of my head though. Went thru no testing, whatsoever. Use with caution ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜