cocos2d 课外日程选择器

发布于 2024-11-10 17:57:14 字数 1318 浏览 4 评论 0原文

我想安排一个@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 技术交流群。

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

发布评论

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

评论(1

叹梦 2024-11-17 17:57:15

实际上我不知道为什么,但它有效:

-(void) onEnter {
    GameManager *sharedManager = [GameManager sharedManager];
    [[CCScheduler sharedScheduler] scheduleSelector:@selector(count)
                                          forTarget:sharedManager
                                           interval:1.0
                                             paused:NO];

//    [self schedule:@selector(tick:) interval:0.5];
}

回答下一个问题:

使用sharedScheduler取消调度效果非常好。您的问题是您没有收到触摸事件,因为您忘记了 HelloWorld.m 和 中的 [super onEnter] (顺便说一下 super onExit) super onEnter是CCLayer向touchDispatcher进行自我注册的地方。如果你添加这个,一切都会正常。

Actually I don't know why, but it work:

-(void) onEnter {
    GameManager *sharedManager = [GameManager sharedManager];
    [[CCScheduler sharedScheduler] scheduleSelector:@selector(count)
                                          forTarget:sharedManager
                                           interval:1.0
                                             paused:NO];

//    [self schedule:@selector(tick:) interval:0.5];
}

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 way super onExit) in HelloWorld.m and super onEnter is the place where CCLayer makes self registration with touchDispatcher. If you will add this everything will work.

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