evd*_*100 1 methods objective-c 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];
}
Run Code Online (Sandbox Code Playgroud)
编辑:
产卵很棒,效果很好,但我希望僵尸能够正常运动.之前,代码是该方法是在玩家移动时调用的.这不再有效,因为我必须在方法中调用一个整数.
+(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];
}
}
Run Code Online (Sandbox Code Playgroud)
玩家移动处理只涉及渲染玩家然后[Entity entityZombie]; 但如上所述,不再有效.
-(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];
}
Run Code Online (Sandbox Code Playgroud)
像这样称呼它:
[self renderZombieWithX:10 andY:20];
[self renderZombieWithX:13 andY:37];
//...
Run Code Online (Sandbox Code Playgroud)
此外,还有一个用于创建NSPoints 的专用函数:
NSPoint center = NSMakePoint(xCoord, yCoord);
Run Code Online (Sandbox Code Playgroud)
您可能希望使用以支持:
NSPoint center = {xCoord,yCoord};
Run Code Online (Sandbox Code Playgroud)
(请参阅我的回答中有关原因的评论之一)
如果性能至关重要,则以下缓存方法可能会带来性能提升:
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];
}
Run Code Online (Sandbox Code Playgroud)
这段代码虽然不在我的脑海里.无论如何都没有通过任何测试.谨慎使用;)