3

2 questions I can not make a CCTimer work, and I ve looked plenty through questions. I did however make the NSTimer work just fine and let's say I used this:

[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(bombExplodes:) userInfo:0 repeats:1];

[CCTimer timerWithTarget:self selector@selector(bombExplodes:) interval:3]

I understand that the CCTimer should fire up the bombExplodes methood in 3 seconds.. however it is just not doing it ! At the same time, the NSTimer works just as it is supposed to ! I'm not quite sure what I am doing wrong..

Also, I have tried using selfSchedule before, and even though it works fine, I have a small q about it.. say a selfSched call is called and it s supposed to fire up a method in 5 seconds. if this same selfSched call gets called again before it fires up that method, I noticed it pretty much cancels the first call (thus overriding it). Is this supposed to happen ?

Thanks !

3 Answers 3

2

This may help you

[self performSelector:@selector(bombExplodes:)withObject:nil afterDelay:3.0];
1
  • 1
    I solved my problem in a different way but I would still like to know the faults of CCTimer. Unlike self schedule.. delay.., I see that self performSelector does not get overriden by itself, so this will prove useful in the future, thanks ! Nov 1, 2012 at 6:31
1

Try This

    CCTimer *myTimer = [[CCTimer alloc] initWithTarget:self 
                         selector:@selector(methodName:) interval:delay]]
3
  • I did before and I did again. It does not work. If you are indeed running the same version of cocos2d that I am, I don't see how this would work. There should also be no difference as this is just manually allocated but essentially doing the same thing. Nov 1, 2012 at 5:46
  • it doing the same thing, but in the your case you have autoreleasede object that will be released at the end of the current tick.
    – Morion
    Nov 1, 2012 at 7:02
  • Correct ! That is what I stated in my previous comment (manual allocation..) ! Nov 4, 2012 at 8:22
1

I think if you are using cocos2d then there is no need to use NSTimer as scheduler is always a better option in cocos2d.You can achieve what you are doing by simply using

[self schedule:@selector(bombExplodes:)];  

and for custom interval you can use

[self schedule:@selector(bombExplodes:) interval:3.0f];  
6
  • if you call [self schedule:@selector(bombExplodes:) interval:3.0f] once and BEFORE the 3 seconds are over, you call it 2 more times (or 3 ?), then the scheduler fails to make all the calls in time (it actually gives a warning in output that the scheduler is BUSY). so a function will not get called because of this. In my case, not all 'bombs' would explode as the function would not get called. [self performSelector] fortunately does not get canceled, and it works better. Nov 1, 2012 at 22:29
  • I don't understand that why you will call it again when a scheduler has been already scheduled which will fire itself after each 3 seconds then what is the need of calling it again.And if you want to cancel the previous scheduler and start a new one just unschedule the previous one and fire a new scheduler. Will you elaborate what exactly want to do?
    – Abhinav
    Nov 2, 2012 at 4:49
  • After a bomb object is placed, it should 'explode' after 3 seconds. if you place 2 more bombs before those 3 seconds are over, then the scheduler will be called again twice. Nov 2, 2012 at 18:52
  • I think bomb object once placed will explode only once so why to schedule this.And yes for this performSelector is perfect option.As it will call the method just once at given interval for each instance.
    – Abhinav
    Nov 3, 2012 at 8:27
  • because its not only about one bomb object, its about 3 or more bomb objects, so the selector is called at least 3 times, scheduling 3 or more calls.. I can't explain it any further. I got what I needed to know, if you need clarification, please ask but I don't need any more! I also mentioned the method that you suggested in my question already as not being the appropriate action to take and the reason why.. Nov 4, 2012 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.