Hi all,
I'm looking for the most optimal way of inserting a time delay between game events using Cocos2d 0.7.3. Can anyone advise, thanks?
EG...
Display Sprite 1
Wait 5 seconds
Display Sprite 2
Wait 8 seconds
etc...
A fast, easy to use, free, and community supported 2D game engine
Hi all,
I'm looking for the most optimal way of inserting a time delay between game events using Cocos2d 0.7.3. Can anyone advise, thanks?
EG...
Display Sprite 1
Wait 5 seconds
Display Sprite 2
Wait 8 seconds
etc...
use a sequence of actions. Something like:
[Sequence ...[CallFunc ....], [DelayTime ....], [CallFunc...], ....].
I think something like this will work.
Hi Caxaria,
Is is possible for you to provide me with some example code for this technique, many thanks.
Sure. Check the cocos2d source code. In the SpritesTest.m, there's a function called onEnter in the SpriteDelayTime and the SpriteCallFunc implementation. They should give you a good start.
I've looked at that example code but I'm still really struggling with this.
It appears that 'DelayTime' isn't working as I'm expecting, I've proven this by changing the delay value from 1 to 10000, it doesn't make any difference and there is no delay whatever value you set it to?
Let me clarify my requirements...
In my init{} method I want to run an 'action sequence' on 3 different AtlasSprites.
I need the same sequence of actions to run 'one after the other', I've included some of my code below:
id action = [Sequence actions:
[FadeIn actionWithDuration:0.1],
[ScaleTo actionWithDuration:0.9 scaleX:0.5 scaleY:0.5],
[FadeTo actionWithDuration:0.3 opacity:0],
[DelayTime actionWithDuration:5],
nil];
[sprite1 runAction:action];
[sprite2 runAction:action];
[sprite3 runAction:action];
When this code runs all 3 actions start 'simultaneously' which is not what I want.
I need to have a 5 second delay between each of these actions. The 'DelayTime' event has no effect in this code, i.e. there is no delay.
Has anyone hit this problem or have I misunderstood how 'DelayTime' works?
You're saying all 3 sprites run the actions at the same time right? That's no surprise. You're telling the 3 sprites to run the action immediately. If you want them to do it one after another, you have to create 3 different actions (with the Sequence you've added), and account for the time delay for one after another.
I am also trying to write a sequence to control timing of a slideshow.
I have:
id action1= [Sequence actions: [FadeIn actionWithDuration:5.0],[ScaleBy actionWithDuration:5.0 scaleX:1.2 scaleY:1.2],[DelayTime actionWithDuration:20.0f],nil];
The FadeIn and the ScaleBy work as expected, but the next slide starts immediately. There is no DelayTime.
I have read every piece of documentation I can find and that I understand. I (of course) am very new at this.
I would appreciate a step in the right direction?
Your not actually delaying anything with that DelayTime, as your not running anything after it.
It looks like you need a CallFunc to run after it (as in Joao's pseudo example above) which should then schedule the next image to load, and probably start a new action (or a copy of that one) to trigger the cycle again.
I have found sketchy documentation in regards to actions and sequences, but nothing very elaborate. Is there a good source that lays out all the option in a manner a novice can follow?
Thanks
searching for "CallFunc" returns 1 reference on this entire site.
If you do a 'Edit > Find > Find in Project' & search for 'CallFunc' within xcode with the default cocos2d project open, you'll find various examples showing its usage.
You can also use the api reference at: http://www.cocos2d-iphone.org/api-ref/0.8-rc/ (for the latest 0.8 rc rls).
Hope that helps.
I only learned how to startup Xcode a week ago. Please forgive.
I remember the days of Turbo C and Turbo Pascal with fondness of the 3 cubic feet of manuals you received.
@Osiris: there is another good database for Cocos2d (the old google group).
You'll find a few threads about CallFunc there:
http://groups.google.com/group/cocos2d-iphone-discuss
As PhilM suggests, you should try and check for examples within the examples shipped with the framework/Xcode project.
Welcome!
I am looking at Transitions.m. from the TouchesTest project.
How does my example compare with:
....
IntervalAction *rotozoom = [Sequence actions: [Spawn actions:
[ScaleBy actionWithDuration:duration/2 scale:0.001f],
[RotateBy actionWithDuration:duration/2 angle:360 *2],nil],
[DelayTime actionWithDuration:duration/2],nil];
....
A CallFunc is being used a couple of lines further down, but its my understanding that CallFunc is used for callouts. I don't know how to use a CallFunc in my context?
I see from the snippet above that the sequence is divided into two distinct groups. a "Spawn action:" and a "DelayTime", but again, I don't know how to use the construct in the context of my project.
Hmmm my xCode is kinda messed up and I can't really compile anything; Something that came up my mind (I'm very unsure) maybe it's possible to run a delay on your layer, e.g.
Sprite *spr1 = [Sprite spriteWithFile:@"whatever.png"];
spr.poistion = ccp(160.0, 240.0);
[self addChild:spr1];
[self runAction:[DelayTime actionWithDuration:5.0]];
Sprite *spr2 = [Sprite spriteWithFile:@"whatever2.png"];
spr2.position = ccp(120.0, 58.0);
[self addChild:spr2];
Just guessing, and I can't compile anything for now as my xCode got screwed somehow so update us if this worked...
I'll look at it, but at first glance its very similiar to what i'm trying to do. I have other actions working as I said up above. Its just the DelayTime portion of the sequence that isnt working.
@Osiris: No worries, I know its a steep learning curve at first, the documentation is improving as people currently get time to update it. The included examples are an excellent resource, as are the forums, wiki and the old google mailing list.
You are correct, Transitions.m does show a DelayTime at the end of a Sequence, although that file is actually part of the main cocos2d framework, not just the TouchTest. I'm not entirely sure what the delay is doing there, its my understanding DelayTime is to be used to delay the next action, but I maybe wrong, and it possibly could have an effect on actions scheduled separately straight after. Although, maybe it was added to delay the 'reverse' action that is run on it a few lines below it.
Joao's original tip should work. How are you currently triggering the next image to appear?
Update....
I discovered something I forgot about from earlier in the day,..... it comes from following examples and cutting and pasting and not entirely knowing what is going on...
I forgot my entire slide show was being run by...
[self schedule:@selector(slideShow:) interval:10.0];
but, it was set to 3 seconds, not 10 as it is now.... Since it was working properly, no amount of delays further on were going to have any effect at all.
We learn....
However, I have another issue now.
The slideshow looks beautiful. Im proud that I even figured out the CDAudioManager and have nice background music for it. Its doing a kinda "Ken Burns Effect" between sprites that are the size of the screen and fading/out and zooming but looking at it in instruments, there are no leaks, but my retained allocations are going through the roof and the FPS quickly drops from 60 to 5.
If I remove the transitions, it stays a rock solid 60FPS.
You must log in to post.