首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >目标C: SetNeedsDisplay

目标C: SetNeedsDisplay
EN

Stack Overflow用户
提问于 2011-01-22 09:56:45
回答 1查看 1.8K关注 0票数 0

我一直在读这本关于游戏编程的书,但例子并不是很棒。我正在开发一个名为[self setNeedsDisplay]的应用程序,但它会导致应用程序崩溃。

这是我所有的代码,从书中逐字复制:

代码语言:javascript
复制
-(void)awakeFromNib {
//start timer that will fire every second
[car setAlpha:0];
[road setAlpha:0];
currentImage = [UIImage imageNamed:@"road.png"];

//start timer that fires every second
[NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

//start timer that fires every hundreth of a second
[NSTimer scheduledTimerWithTimeInterval:(0.01) target:self selector:@selector(onTimerRoad) userInfo:nil repeats:YES];
}

-(void)onTimerRoad
{   
    int tileIndex;
tileIndex += 1;
[self setNeedsDisplay];
}

-(void)onTimer {
[self update];
[self draw];
}

-(void)update {
[self updateRoad];
}

-(void)updateRoad {
[self randomRoadUpdate];
}

-(void)randomRoadUpdate {
int distance = (random() % 11) -5;
CGPoint oldPosition = road.center;
if (oldPosition.x + distance < 96 || oldPosition.x + distance > 224)
    return;
road.center = CGPointMake(oldPosition.x + distance, oldPosition.y);
}

-(void)draw {

}

- (id) initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
    //init code
}
return self;
}

-(void)drawRect:(CGRect)rect {
CGImageRef image = CGImageRetain(currentImage.CGImage);
CGRect imageRect;
imageRect.origin = CGPointMake(160, 240);
imageRect.size = CGSizeMake(320.0, 480.0);
CGContextRef uiContext = UIGraphicsGetCurrentContext();
CGContextClipToRect(uiContext, CGRectMake(0.0, 0.0, rect.size.width, rect.size.height));
CGContextDrawTiledImage(uiContext, imageRect, image);

}
EN

回答 1

Stack Overflow用户

发布于 2011-01-22 10:41:08

原来我需要做的是[self.view setNeedsDisplay]而不是[self setNeedsDisplay]

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4765657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档