cocos2d 课外日程选择器
我想安排一个@selector(count)间隔:1.0f来计算剩余时间。 这是我的代码:(在 GameManager.m 文件中)
-(void) count {
duration++;
[[[GameScene sharedScene] gadgetLayer] updateTimerLabel];
if (timeLimit - duration <= 5 && ticking == NO) {
ticking = YES;
[self schedule:@selector(untick) interval:5];
[[SimpleAudioEngine sharedEngine] playEffect:@"tick.caf"];
}
if (duration >= timeLimit) {
[self lose];
}
}
gadgetLayer 是我放置timerLayer 和scoreLayer 内容的地方。 计数未在 GameManager.m 中安排,而是将其放入 GameScene.m 文件中:
-(void) onEnter {
[[GameManager sharedManager] schedule:@selector(count) interval:1.0];
[super onEnter
}
- (void)onExit {
[[GameManager sharedManager] unschedule:@selector(count)];
[super onExit];
}
但计时器标签不会更改。 count 方法位于 GameManager.m 文件中,是否必须位于 GameScene.m 文件中?有什么问题吗?
+(GameManager*) sharedManager {
if (instanceOfGameManager == nil) {
return [[GameManager alloc] init];
}
else return instanceOfGameManager;
}
-(id) init {
if ((self = [super init])) {
instanceOfGameManager = self;
[self scheduleUpdate];
}
return self;
}`
-(void) update: (ccTime) delta {
int a = 2;
}
`
我在 'int a = 2' 行设置了断点,但无法到达。 [GameManager sharedManager] 在 appDidFinishLaunching 方法中调用,所以我猜它不会被再次分配和初始化。
I want to schedule a @selector(count) interval: 1.0f to count the time left.
here is my code: (In GameManager.m file)
-(void) count {
duration++;
[[[GameScene sharedScene] gadgetLayer] updateTimerLabel];
if (timeLimit - duration <= 5 && ticking == NO) {
ticking = YES;
[self schedule:@selector(untick) interval:5];
[[SimpleAudioEngine sharedEngine] playEffect:@"tick.caf"];
}
if (duration >= timeLimit) {
[self lose];
}
}
gadgetLayer is where i put timerLayer and scoreLayer stuff.
the the count is not scheduled in GameManager.m, instead, i put it in my GameScene.m file:
-(void) onEnter {
[[GameManager sharedManager] schedule:@selector(count) interval:1.0];
[super onEnter
}
- (void)onExit {
[[GameManager sharedManager] unschedule:@selector(count)];
[super onExit];
}
But the timerLabel won't change. The count method is in GameManager.m file, does it have to be inside GameScene.m file? Anything wrong with it?
+(GameManager*) sharedManager {
if (instanceOfGameManager == nil) {
return [[GameManager alloc] init];
}
else return instanceOfGameManager;
}
-(id) init {
if ((self = [super init])) {
instanceOfGameManager = self;
[self scheduleUpdate];
}
return self;
}`
-(void) update: (ccTime) delta {
int a = 2;
}
`
i set a breakpoint in 'int a = 2' line, but can not be reached.
[GameManager sharedManager] is called in appDidFinishLaunching method, so it won't be alloc'ed and init'ed again i guess.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上我不知道为什么,但它有效:
回答下一个问题:
使用sharedScheduler取消调度效果非常好。您的问题是您没有收到触摸事件,因为您忘记了 HelloWorld.m 和
中的
是CCLayer向touchDispatcher进行自我注册的地方。如果你添加这个,一切都会正常。[super onEnter]
(顺便说一下super onExit
) super onEnterActually I don't know why, but it work:
Answering next question:
Unscheduling using sharedScheduler works perfect. Your problem is that you don't receive touch events because you've forgot
[super onEnter]
(and by the waysuper onExit
) in HelloWorld.m andsuper onEnter
is the place where CCLayer makes self registration with touchDispatcher. If you will add this everything will work.