在不同的文件中绘制两个对象

发布于 2024-11-07 00:26:30 字数 3116 浏览 2 评论 0原文

Player.m

//
//  PlayerTestAppDelegate.m
//  PlayerTest
//
//  Created by Someguy on 5/13/11.
//  Copyright 2011 Enginering. All rights reserved.
//

#import "Player.h"

@implementation Player
@synthesize window;

//[NSThread sleepForTimeInterval:.1];

// On the program finished loading, do the following..
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {plyr_size = 10; xcord = 200; ycord = 215; [self DrawPlayer];}


- (void)Clearscreen{NSPoint origin = {0,0}; NSRect rect; rect.origin = origin; rect.size.width=1000; rect.size.height=1000; NSBezierPath * path; path = [NSBezierPath bezierPathWithRect:rect]; [path setLineWidth:4]; [[NSColor whiteColor] set]; [path fill]; [[NSColor whiteColor] set]; [path stroke];} 

- (void)DrawPlayer{[self Clearscreen]; NSBezierPath * path = [NSBezierPath bezierPath]; [path setLineWidth:4]; NSPoint center = {ycord,xcord}; [path moveToPoint: center]; [path appendBezierPathWithArcWithCenter: center radius: plyr_size startAngle: 0 endAngle: 360]; [[NSColor grayColor] set]; [path fill]; [[NSColor grayColor] set]; [path stroke];}



//Declare these actions

- (IBAction)PlayerMoveForward:(id)sender {[self PlayerMoveForward];}
- (IBAction)PlayerMoveBackwards:(id)sender {[self PlayerMoveBackwards];}
- (IBAction)PlayerMoveLeft:(id)sender {[self PlayerMoveLeft];}
- (IBAction)PlayerMoveRight:(id)sender {[self PlayerMoveRight];}

// Declare the Methods used for movement

- (void)PlayerMoveForward {xcord=xcord+10; [self DrawPlayer]; [self GetPlayerPostion];}

- (void)PlayerMoveBackwards {xcord=xcord-10; [self DrawPlayer]; [self GetPlayerPostion];}

- (void)PlayerMoveLeft {ycord=ycord-10; [self DrawPlayer]; [self GetPlayerPostion];}

- (void)PlayerMoveRight {ycord=ycord+10; [self DrawPlayer]; [self GetPlayerPostion];}

- (void)GetPlayerPostion {NSLog(@"Player cordinates (%i, %i)",xcord, ycord);}

@end

EntityZombie.m

//
//  EntityZombie.m
//  PlayerTest
//
//  Created by Someguy on 5/13/11.
//  Copyright 2011 Enginering. All rights reserved.
//

#import "EntityZombie.h"
#import "Player.h"

@implementation EntityZombie : Player

//- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {[self FollowPlayer];}
- (void)awakeFromNib{[self FollowPlayer];}

- (void)DrawZombie {if ([self CanSpawnZombie] == TRUE){NSBezierPath * path = [NSBezierPath bezierPath]; [path setLineWidth:4]; NSPoint center = {215,200}; [path moveToPoint: center]; [path appendBezierPathWithArcWithCenter: center radius: 18 startAngle: 0 endAngle: 360]; [[NSColor greenColor] set]; [path fill]; [[NSColor greenColor] set]; [path stroke];}}
- (void)FollowPlayer {
    NSLog(@"Zombie is following player..");
    //zombie_xcord == xcord-1;
    //zombie_ycord == ycord-1;
    [self DrawZombie];
}

- (BOOL)CanSpawnZombie{return TRUE;}

@end

僵尸不会渲染,我有日志

2011-05-13 20:59:22.677 PlayerTest[45207:a0f] Zombie is following player..
Fri May 13 20:59:22 Someguy-MacBook-Pro.local PlayerTest[45207] <Error>: CGContextSetFillColorWithColor: invalid context 0x0

Player.m

//
//  PlayerTestAppDelegate.m
//  PlayerTest
//
//  Created by Someguy on 5/13/11.
//  Copyright 2011 Enginering. All rights reserved.
//

#import "Player.h"

@implementation Player
@synthesize window;

//[NSThread sleepForTimeInterval:.1];

// On the program finished loading, do the following..
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {plyr_size = 10; xcord = 200; ycord = 215; [self DrawPlayer];}


- (void)Clearscreen{NSPoint origin = {0,0}; NSRect rect; rect.origin = origin; rect.size.width=1000; rect.size.height=1000; NSBezierPath * path; path = [NSBezierPath bezierPathWithRect:rect]; [path setLineWidth:4]; [[NSColor whiteColor] set]; [path fill]; [[NSColor whiteColor] set]; [path stroke];} 

- (void)DrawPlayer{[self Clearscreen]; NSBezierPath * path = [NSBezierPath bezierPath]; [path setLineWidth:4]; NSPoint center = {ycord,xcord}; [path moveToPoint: center]; [path appendBezierPathWithArcWithCenter: center radius: plyr_size startAngle: 0 endAngle: 360]; [[NSColor grayColor] set]; [path fill]; [[NSColor grayColor] set]; [path stroke];}



//Declare these actions

- (IBAction)PlayerMoveForward:(id)sender {[self PlayerMoveForward];}
- (IBAction)PlayerMoveBackwards:(id)sender {[self PlayerMoveBackwards];}
- (IBAction)PlayerMoveLeft:(id)sender {[self PlayerMoveLeft];}
- (IBAction)PlayerMoveRight:(id)sender {[self PlayerMoveRight];}

// Declare the Methods used for movement

- (void)PlayerMoveForward {xcord=xcord+10; [self DrawPlayer]; [self GetPlayerPostion];}

- (void)PlayerMoveBackwards {xcord=xcord-10; [self DrawPlayer]; [self GetPlayerPostion];}

- (void)PlayerMoveLeft {ycord=ycord-10; [self DrawPlayer]; [self GetPlayerPostion];}

- (void)PlayerMoveRight {ycord=ycord+10; [self DrawPlayer]; [self GetPlayerPostion];}

- (void)GetPlayerPostion {NSLog(@"Player cordinates (%i, %i)",xcord, ycord);}

@end

EntityZombie.m

//
//  EntityZombie.m
//  PlayerTest
//
//  Created by Someguy on 5/13/11.
//  Copyright 2011 Enginering. All rights reserved.
//

#import "EntityZombie.h"
#import "Player.h"

@implementation EntityZombie : Player

//- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {[self FollowPlayer];}
- (void)awakeFromNib{[self FollowPlayer];}

- (void)DrawZombie {if ([self CanSpawnZombie] == TRUE){NSBezierPath * path = [NSBezierPath bezierPath]; [path setLineWidth:4]; NSPoint center = {215,200}; [path moveToPoint: center]; [path appendBezierPathWithArcWithCenter: center radius: 18 startAngle: 0 endAngle: 360]; [[NSColor greenColor] set]; [path fill]; [[NSColor greenColor] set]; [path stroke];}}
- (void)FollowPlayer {
    NSLog(@"Zombie is following player..");
    //zombie_xcord == xcord-1;
    //zombie_ycord == ycord-1;
    [self DrawZombie];
}

- (BOOL)CanSpawnZombie{return TRUE;}

@end

The zombie will not render and I have the log of

2011-05-13 20:59:22.677 PlayerTest[45207:a0f] Zombie is following player..
Fri May 13 20:59:22 Someguy-MacBook-Pro.local PlayerTest[45207] <Error>: CGContextSetFillColorWithColor: invalid context 0x0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡的优雅 2024-11-14 00:26:30

没有当前绘制上下文。问题出在您调用绘制方法的位置。它们需要从 -(void) drawRect 方法调用,或者,如果您愿意,也可以绘制到图像中。不管怎样,都必须有一个当前的绘制上下文。

There is no current draw context. The issue is from where you are calling the draw methods. They need to be called from a -(void) drawRect method or, if you'd prefer, you can draw into an image. Either way, there has to be a current draw context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文