1

次のコードは、交換可能な座標を持つゾンビをレンダリングします。唯一の問題は、コードを複製し、ゾンビごとに新しい座標変数を自分で作成する必要があることです。変数のコピーを作成できる方法はありますか?複数のゾンビに対して1つのメソッドと、3つのゾンビに対して3つのメソッドと3つの別々の変数があれば十分です。

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

    }
}

プレーヤーの動きの処理には、プレーヤーをレンダリングしてから[EntityentityZombie]をレンダリングするだけです。しかし、上で述べたように、もはや機能しません。

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

さらに、NSPointを作成するための専用関数があります。

NSPoint center = NSMakePoint(xCoord, yCoord);

あなたが支持して使用したいかもしれません:

NSPoint center = {xCoord,yCoord};

(理由についての私の回答のコメントの1つを参照してください)


パフォーマンスが重要な場合は、次のようなキャッシュされたアプローチによってパフォーマンスが向上する可能性があります。

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 に答える