Skip to content
Newer
Older
100644 683 lines (533 sloc) 12.5 KB
3b6763f @ricardoquesada Fixing issue #439
ricardoquesada authored May 6, 2010
1 //
2 // Scheduler Test
3 // http://www.cocos2d-iphone.org
4 //
5
6 // local import
7 #import "cocos2d.h"
8 #import "SchedulerTest.h"
9
10 enum {
11 kTagAnimationDance = 1,
12 };
13
14 static int sceneIdx=-1;
15 static NSString *transitions[] = {
16 @"SchedulerAutoremove",
17 @"SchedulerPauseResume",
18 @"SchedulerUnscheduleAll",
19 @"SchedulerUnscheduleAllHard",
20 @"SchedulerSchedulesAndRemove",
27433ab @ricardoquesada serveral fixes in selector update
ricardoquesada authored May 7, 2010
21 @"SchedulerUpdate",
d44ce31 @ricardoquesada cocos2d nodes uses scheduleUpdate
ricardoquesada authored May 7, 2010
22 @"SchedulerUpdateAndCustom",
23 @"SchedulerUpdateFromCustom",
ed84397 @ricardoquesada Scheduler: doesn't reschedule selector.
ricardoquesada authored Jul 7, 2010
24 @"RescheduleSelector",
3b6763f @ricardoquesada Fixing issue #439
ricardoquesada authored May 6, 2010
25 };
26
c8fee14 @ricardoquesada includes missing prototypes.
ricardoquesada authored Jun 7, 2011
27 Class nextTest(void);
28 Class prevTest(void);
29 Class restartTest(void);
30
3b6763f @ricardoquesada Fixing issue #439
ricardoquesada authored May 6, 2010
31 Class nextTest()
32 {
33
34 sceneIdx++;
35 sceneIdx = sceneIdx % ( sizeof(transitions) / sizeof(transitions[0]) );
36 NSString *r = transitions[sceneIdx];
37 Class c = NSClassFromString(r);
38 return c;
39 }
40
41 Class prevTest()
42 {
43 sceneIdx--;
44 if( sceneIdx < 0 )
45 sceneIdx = sizeof(transitions) / sizeof(transitions[0]) -1;
46 NSString *r = transitions[sceneIdx];
47 Class c = NSClassFromString(r);
48 return c;
49 }
50
51 Class restartTest()
52 {
53 NSString *r = transitions[sceneIdx];
54 Class c = NSClassFromString(r);
55 return c;
56 }
57
58
59 #pragma mark SchedulerTest
60 @implementation SchedulerTest
61 -(id) init
62 {
63 if( (self=[super init])) {
64
65
66 CGSize s = [[CCDirector sharedDirector] winSize];
67
97fcf24 @ricardoquesada Particle and CCLabel new naming convention
ricardoquesada authored Aug 21, 2010
68 CCLabelTTF *label = [CCLabelTTF labelWithString:[self title] fontName:@"Arial" fontSize:32];
3b6763f @ricardoquesada Fixing issue #439
ricardoquesada authored May 6, 2010
69 [self addChild: label];
70 [label setPosition: ccp(s.width/2, s.height-50)];
71
72 NSString *subtitle = [self subtitle];
73 if( subtitle ) {
97fcf24 @ricardoquesada Particle and CCLabel new naming convention
ricardoquesada authored Aug 21, 2010
74 CCLabelTTF *l = [CCLabelTTF labelWithString:subtitle fontName:@"Thonburi" fontSize:16];
3b6763f @ricardoquesada Fixing issue #439
ricardoquesada authored May 6, 2010
75 [self addChild:l z:1];
76 [l setPosition:ccp(s.width/2, s.height-80)];
77 }
78
79
80 CCMenuItemImage *item1 = [CCMenuItemImage itemFromNormalImage:@"b1.png" selectedImage:@"b2.png" target:self selector:@selector(backCallback:)];
81 CCMenuItemImage *item2 = [CCMenuItemImage itemFromNormalImage:@"r1.png" selectedImage:@"r2.png" target:self selector:@selector(restartCallback:)];
82 CCMenuItemImage *item3 = [CCMenuItemImage itemFromNormalImage:@"f1.png" selectedImage:@"f2.png" target:self selector:@selector(nextCallback:)];
83
84 CCMenu *menu = [CCMenu menuWithItems:item1, item2, item3, nil];
85 menu.position = CGPointZero;
86 item1.position = ccp( s.width/2 - 100,30);
87 item2.position = ccp( s.width/2, 30);
88 item3.position = ccp( s.width/2 + 100,30);
89 [self addChild: menu z:1];
90 }
91
92 return self;
93 }
94
95 -(void) dealloc
96 {
97 [super dealloc];
98 }
99
100
101 -(void) restartCallback: (id) sender
102 {
103 CCScene *s = [CCScene node];
104 [s addChild: [restartTest() node]];
105 [[CCDirector sharedDirector] replaceScene: s];
106 }
107
108 -(void) nextCallback: (id) sender
109 {
110 CCScene *s = [CCScene node];
111 [s addChild: [nextTest() node]];
112 [[CCDirector sharedDirector] replaceScene: s];
113 }
114
115 -(void) backCallback: (id) sender
116 {
117 CCScene *s = [CCScene node];
118 [s addChild: [prevTest() node]];
119 [[CCDirector sharedDirector] replaceScene: s];
120 }
121
122
123 -(NSString*) title
124 {
125 return @"No title";
126 }
127
128 -(NSString*) subtitle
129 {
130 return nil;
131 }
132 @end
133
134 #pragma mark SchedulerAutoremove
135 @implementation SchedulerAutoremove
136 -(id) init
137 {
138 if( (self=[super init]) ) {
139
140 [self schedule:@selector(autoremove:) interval:0.5f];
141 [self schedule:@selector(tick:) interval:0.5f];
142 accum = 0;
143 }
144
145 return self;
146 }
147
148 -(NSString *) title
149 {
150 return @"Self-remove an scheduler";
151 }
152
153 -(NSString *) subtitle
154 {
155 return @"1 scheduler will be autoremoved in 3 seconds. See console";
156 }
157
158 -(void) tick:(ccTime)dt
159 {
160 NSLog(@"This scheduler should not be removed");
161 }
162 -(void) autoremove:(ccTime)dt
163 {
164 accum += dt;
165 NSLog(@"Time: %f", accum);
166
167 if( accum > 3 ) {
168 [self unschedule:_cmd];
169 NSLog(@"scheduler removed");
170 }
171 }
172 @end
173
174 #pragma mark SchedulerPauseResume
175 @implementation SchedulerPauseResume
176 -(id) init
177 {
178 if( (self=[super init]) ) {
179
180 [self schedule:@selector(tick1:) interval:0.5f];
181 [self schedule:@selector(tick2:) interval:1];
182 [self schedule:@selector(pause:) interval:3];
183 }
184
185 return self;
186 }
187
188 -(NSString *) title
189 {
190 return @"Pause / Resume";
191 }
192
193 -(NSString *) subtitle
194 {
195 return @"Scheduler should be paused after 3 seconds. See console";
196 }
197
198 -(void) tick1:(ccTime)dt
199 {
200 NSLog(@"tick1");
201 }
202
203 -(void) tick2:(ccTime)dt
204 {
205 NSLog(@"tick2");
206 }
207
208 -(void) pause:(ccTime)dt
209 {
2a75647 @ricardoquesada Fixed issue #835
ricardoquesada authored May 21, 2010
210 [[CCScheduler sharedScheduler] pauseTarget:self];
3b6763f @ricardoquesada Fixing issue #439
ricardoquesada authored May 6, 2010
211 }
212 @end
213
214 #pragma mark SchedulerUnscheduleAll
215 @implementation SchedulerUnscheduleAll
216 -(id) init
217 {
218 if( (self=[super init]) ) {
219
220 [self schedule:@selector(tick1:) interval:0.5f];
221 [self schedule:@selector(tick2:) interval:1];
222 [self schedule:@selector(tick3:) interval:1.5f];
223 [self schedule:@selector(tick4:) interval:1.5f];
224 [self schedule:@selector(unscheduleAll:) interval:4];
225 }
226
227 return self;
228 }
229
230 -(NSString *) title
231 {
232 return @"Unschedule All selectors";
233 }
234
235 -(NSString *) subtitle
236 {
237 return @"All scheduled selectors will be unscheduled in 4 seconds. See console";
238 }
239
240 -(void) tick1:(ccTime)dt
241 {
242 NSLog(@"tick1");
243 }
244
245 -(void) tick2:(ccTime)dt
246 {
247 NSLog(@"tick2");
248 }
249
250 -(void) tick3:(ccTime)dt
251 {
252 NSLog(@"tick3");
253 }
254
255 -(void) tick4:(ccTime)dt
256 {
257 NSLog(@"tick4");
258 }
259
260 -(void) unscheduleAll:(ccTime)dt
261 {
262 [self unscheduleAllSelectors];
263 }
264 @end
265
266 #pragma mark SchedulerUnscheduleAllHard
267 @implementation SchedulerUnscheduleAllHard
268 -(id) init
269 {
270 if( (self=[super init]) ) {
271
272 [self schedule:@selector(tick1:) interval:0.5f];
273 [self schedule:@selector(tick2:) interval:1];
274 [self schedule:@selector(tick3:) interval:1.5f];
275 [self schedule:@selector(tick4:) interval:1.5f];
276 [self schedule:@selector(unscheduleAll:) interval:4];
277 }
278
279 return self;
280 }
281
282 -(NSString *) title
283 {
284 return @"Unschedule All selectors #2";
285 }
286
287 -(NSString *) subtitle
288 {
289 return @"Unschedules all selectors after 4s. Uses CCScheduler. See console";
290 }
291
292 -(void) tick1:(ccTime)dt
293 {
294 NSLog(@"tick1");
295 }
296
297 -(void) tick2:(ccTime)dt
298 {
299 NSLog(@"tick2");
300 }
301
302 -(void) tick3:(ccTime)dt
303 {
304 NSLog(@"tick3");
305 }
306
307 -(void) tick4:(ccTime)dt
308 {
309 NSLog(@"tick4");
310 }
311
312 -(void) unscheduleAll:(ccTime)dt
313 {
314 [[CCScheduler sharedScheduler] unscheduleAllSelectors];
315 }
316 @end
317
318
319 #pragma mark SchedulerSchedulesAndRemove
320 @implementation SchedulerSchedulesAndRemove
321 -(id) init
322 {
323 if( (self=[super init]) ) {
324
325 [self schedule:@selector(tick1:) interval:0.5f];
326 [self schedule:@selector(tick2:) interval:1];
327 [self schedule:@selector(scheduleAndUnschedule:) interval:4];
328 }
329
330 return self;
331 }
332
333 -(NSString *) title
334 {
335 return @"Schedule from Schedule";
336 }
337
338 -(NSString *) subtitle
339 {
340 return @"Will unschedule and schedule selectors in 4s. See console";
341 }
342
343 -(void) tick1:(ccTime)dt
344 {
345 NSLog(@"tick1");
346 }
347
348 -(void) tick2:(ccTime)dt
349 {
350 NSLog(@"tick2");
351 }
352
353 -(void) tick3:(ccTime)dt
354 {
355 NSLog(@"tick3");
356 }
357
358 -(void) tick4:(ccTime)dt
359 {
360 NSLog(@"tick4");
361 }
362
363 -(void) scheduleAndUnschedule:(ccTime)dt
364 {
365 [self unschedule:_cmd];
366 [self unschedule:@selector(tick1:)];
367 [self unschedule:@selector(tick2:)];
368 [self schedule:@selector(tick3:) interval:1];
369 [self schedule:@selector(tick4:) interval:1];
370 }
371 @end
372
373
27433ab @ricardoquesada serveral fixes in selector update
ricardoquesada authored May 7, 2010
374 @interface TestNode : CCNode
375 {
376 NSString *string_;
377 }
378 @end
379
d44ce31 @ricardoquesada cocos2d nodes uses scheduleUpdate
ricardoquesada authored May 7, 2010
380 #pragma mark SchedulerUpdate
381
27433ab @ricardoquesada serveral fixes in selector update
ricardoquesada authored May 7, 2010
382 @implementation TestNode
383 -(id) initWithString:(NSString*)string priority:(int)priority
384 {
385 if( (self = [super init] ) ) {
386
387 string_ = [string retain];
388
389 [self scheduleUpdateWithPriority:priority];
390
391 }
392
393 return self;
394 }
395
396 - (void) dealloc
397 {
398 [string_ release];
399 [super dealloc];
400 }
401
402 -(void) update:(ccTime)dt
403 {
404 NSLog(@"%@", string_ );
405 }
406
407 @end
408
409 @implementation SchedulerUpdate
410 -(id) init
411 {
412 if( (self=[super init]) ) {
413
414 // schedule in different order... just another test
415 TestNode *d = [[TestNode alloc] initWithString:@"---" priority:50];
416 [self addChild:d];
417 [d release];
418
d44ce31 @ricardoquesada cocos2d nodes uses scheduleUpdate
ricardoquesada authored May 7, 2010
419 TestNode *b = [[TestNode alloc] initWithString:@"3rd" priority:0];
27433ab @ricardoquesada serveral fixes in selector update
ricardoquesada authored May 7, 2010
420 [self addChild:b];
421 [b release];
422
423 TestNode *a = [[TestNode alloc] initWithString:@"1st" priority:-10];
424 [self addChild:a];
425 [a release];
426
d44ce31 @ricardoquesada cocos2d nodes uses scheduleUpdate
ricardoquesada authored May 7, 2010
427 TestNode *c = [[TestNode alloc] initWithString:@"4th" priority:10];
27433ab @ricardoquesada serveral fixes in selector update
ricardoquesada authored May 7, 2010
428 [self addChild:c];
429 [c release];
430
d44ce31 @ricardoquesada cocos2d nodes uses scheduleUpdate
ricardoquesada authored May 7, 2010
431 TestNode *e = [[TestNode alloc] initWithString:@"5th" priority:20];
27433ab @ricardoquesada serveral fixes in selector update
ricardoquesada authored May 7, 2010
432 [self addChild:e];
433 [e release];
d44ce31 @ricardoquesada cocos2d nodes uses scheduleUpdate
ricardoquesada authored May 7, 2010
434
435 TestNode *f = [[TestNode alloc] initWithString:@"2nd" priority:-5];
436 [self addChild:f];
437 [f release];
27433ab @ricardoquesada serveral fixes in selector update
ricardoquesada authored May 7, 2010
438
439
440 [self schedule:@selector(removeUpdates:) interval:4];
441 }
442
443 return self;
444 }
445
446 -(NSString *) title
447 {
448 return @"Schedule update with priority";
449 }
450
451 -(NSString *) subtitle
452 {
453 return @"3 scheduled updates. Priority should work. Stops in 4s. See console";
454 }
455
456 -(void) removeUpdates:(ccTime)dt
457 {
458 for( CCNode *node in children_)
459 [node unscheduleAllSelectors];
460 }
461 @end
462
463
d44ce31 @ricardoquesada cocos2d nodes uses scheduleUpdate
ricardoquesada authored May 7, 2010
464 #pragma mark SchedulerUpdateAndCustom
465 @implementation SchedulerUpdateAndCustom
466 -(id) init
467 {
468 if( (self=[super init]) ) {
469
470 [self scheduleUpdate];
471 [self schedule:@selector(tick:)];
472 [self schedule:@selector(stopSelectors:) interval:4];
473
474 }
475
476 return self;
477 }
478
479 -(NSString *) title
480 {
481 return @"Schedule Update + custom selector";
482 }
483
484 -(NSString *) subtitle
485 {
486 return @"Update + custom selector at the same time. Stops in 4s. See console";
487 }
488
489 -(void) update:(ccTime)dt
490 {
491 NSLog(@"update called:%f", dt);
492 }
493
494 -(void) tick:(ccTime)dt
495 {
496 NSLog(@"custom selector called:%f",dt);
497 }
498
499 -(void) stopSelectors:(ccTime)dt
500 {
501 [self unscheduleAllSelectors];
502 }
503 @end
504
505 #pragma mark SchedulerUpdateFromCustomcle
506 @implementation SchedulerUpdateFromCustom
507 -(id) init
508 {
509 if( (self=[super init]) ) {
510
511 [self schedule:@selector(schedUpdate:) interval:2];
512
513 }
514
515 return self;
516 }
517
518 -(NSString *) title
519 {
520 return @"Schedule Update in 2 sec";
521 }
522
523 -(NSString *) subtitle
524 {
525 return @"Update schedules in 2 secs. Stops 2 sec later. See console";
526 }
527
528 -(void) update:(ccTime)dt
529 {
530 NSLog(@"update called:%f", dt);
531 }
532
533 -(void) stopUpdate:(ccTime)dt
534 {
535 [self unscheduleUpdate];
536 [self unschedule:_cmd];
537 }
538
539 -(void) schedUpdate:(ccTime)dt
540 {
541 [self unschedule:_cmd];
542 [self scheduleUpdate];
543 [self schedule:@selector(stopUpdate:) interval:2];
544 }
545
546 @end
547
ed84397 @ricardoquesada Scheduler: doesn't reschedule selector.
ricardoquesada authored Jul 7, 2010
548 #pragma mark RescheduleSelector
549 @implementation RescheduleSelector
550
551 -(id) init
552 {
553 if( (self=[super init]) ) {
554
555 interval = 1;
556 ticks = 0;
557 [self schedule:@selector(schedUpdate:) interval:interval];
558
559 }
560
561 return self;
562 }
563
564 -(NSString *) title
565 {
566 return @"Reschedule Selector";
567 }
568
569 -(NSString *) subtitle
570 {
571 return @"Interval is 1 second, then 2, then 3...";
572 }
573
574
575 -(void) schedUpdate:(ccTime)dt
576 {
577 ticks++;
578
b47eab8 @ricardoquesada SchedulerTest: CCLog() -> NSLog()
ricardoquesada authored Jul 30, 2010
579 NSLog(@"schedUpdate: %.2f", dt);
ed84397 @ricardoquesada Scheduler: doesn't reschedule selector.
ricardoquesada authored Jul 7, 2010
580 if( ticks > 3 ) {
581 [self schedule:_cmd interval:++interval];
582 ticks = 0;
583 }
584
585 }
586
587 @end
588
d44ce31 @ricardoquesada cocos2d nodes uses scheduleUpdate
ricardoquesada authored May 7, 2010
589
3b6763f @ricardoquesada Fixing issue #439
ricardoquesada authored May 6, 2010
590
591 // CLASS IMPLEMENTATIONS
592 @implementation AppController
593
594 - (void) applicationDidFinishLaunching:(UIApplication*)application
595 {
97726bd @ricardoquesada CCDirector: easier way to setup a director.
ricardoquesada authored Jun 14, 2010
596 // CC_DIRECTOR_INIT()
597 //
598 // 1. Initializes an EAGLView with 0-bit depth format, and RGB565 render buffer
a4a80cb @ricardoquesada CC_DIRECTOR_INIT(): improved description
ricardoquesada authored Jun 14, 2010
599 // 2. EAGLView multiple touches: disabled
53a242c @ricardoquesada Fixed memory leak in CC_DIRECTOR_INIT()
ricardoquesada authored Jun 14, 2010
600 // 3. creates a UIWindow, and assign it to the "window" var (it must already be declared)
601 // 4. Parents EAGLView to the newly created window
602 // 5. Creates Display Link Director
603 // 5a. If it fails, it will use an NSTimer director
604 // 6. It will try to run at 60 FPS
605 // 7. Display FPS: NO
606 // 8. Device orientation: Portrait
607 // 9. Connects the director to the EAGLView
97726bd @ricardoquesada CCDirector: easier way to setup a director.
ricardoquesada authored Jun 14, 2010
608 //
609 CC_DIRECTOR_INIT();
3b6763f @ricardoquesada Fixing issue #439
ricardoquesada authored May 6, 2010
610
97726bd @ricardoquesada CCDirector: easier way to setup a director.
ricardoquesada authored Jun 14, 2010
611 // Obtain the shared director in order to...
7289b2a @ricardoquesada Simplified CCDirector.
ricardoquesada authored Jun 14, 2010
612 CCDirector *director = [CCDirector sharedDirector];
3b6763f @ricardoquesada Fixing issue #439
ricardoquesada authored May 6, 2010
613
97726bd @ricardoquesada CCDirector: easier way to setup a director.
ricardoquesada authored Jun 14, 2010
614 // Sets landscape mode
7289b2a @ricardoquesada Simplified CCDirector.
ricardoquesada authored Jun 13, 2010
615 [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
3b6763f @ricardoquesada Fixing issue #439
ricardoquesada authored May 6, 2010
616
95e67f2 @ricardoquesada Updates Donors. Use HighRes only on iPHone4
ricardoquesada authored Sep 10, 2010
617 // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
7dff782 @ricardoquesada Director: Added enableRetinaDisplay:(BOOL)enabled
ricardoquesada authored Oct 18, 2010
618 if( ! [director enableRetinaDisplay:YES] )
619 CCLOG(@"Retina Display Not supported");
95e67f2 @ricardoquesada Updates Donors. Use HighRes only on iPHone4
ricardoquesada authored Sep 10, 2010
620
97726bd @ricardoquesada CCDirector: easier way to setup a director.
ricardoquesada authored Jun 14, 2010
621 // Turn on display FPS
7289b2a @ricardoquesada Simplified CCDirector.
ricardoquesada authored Jun 13, 2010
622 [director setDisplayFPS:YES];
3b6763f @ricardoquesada Fixing issue #439
ricardoquesada authored May 6, 2010
623
624 // Default texture format for PNG/BMP/TIFF/JPEG/GIF images
625 // It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
626 // You can change anytime.
627 [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
628
629 CCScene *scene = [CCScene node];
630 [scene addChild: [nextTest() node]];
631
7289b2a @ricardoquesada Simplified CCDirector.
ricardoquesada authored Jun 13, 2010
632 [director runWithScene: scene];
3b6763f @ricardoquesada Fixing issue #439
ricardoquesada authored May 6, 2010
633
634 }
635
636 // getting a call, pause the game
637 -(void) applicationWillResignActive:(UIApplication *)application
638 {
639 [[CCDirector sharedDirector] pause];
640 }
641
642 // call got rejected
643 -(void) applicationDidBecomeActive:(UIApplication *)application
644 {
645 [[CCDirector sharedDirector] resume];
646 }
647
454d175 @ricardoquesada Added background & foreground in AppDelegate
ricardoquesada authored Jul 6, 2010
648 -(void) applicationDidEnterBackground:(UIApplication*)application
649 {
650 [[CCDirector sharedDirector] stopAnimation];
651 }
652
653 -(void) applicationWillEnterForeground:(UIApplication*)application
654 {
655 [[CCDirector sharedDirector] startAnimation];
656 }
657
280c65f @ricardoquesada Tempates & Test: CC_DIRECTOR_END()
ricardoquesada authored Jul 15, 2010
658 // application will be killed
454d175 @ricardoquesada Added background & foreground in AppDelegate
ricardoquesada authored Jul 7, 2010
659 - (void)applicationWillTerminate:(UIApplication *)application
660 {
280c65f @ricardoquesada Tempates & Test: CC_DIRECTOR_END()
ricardoquesada authored Jul 16, 2010
661 CC_DIRECTOR_END();
454d175 @ricardoquesada Added background & foreground in AppDelegate
ricardoquesada authored Jul 7, 2010
662 }
663
32ed665 @ricardoquesada BitmapFontAtlas: fixed issue #833. Director improvements
ricardoquesada authored May 24, 2010
664 // purge memory
665 - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
666 {
667 [[CCDirector sharedDirector] purgeCachedData];
3b6763f @ricardoquesada Fixing issue #439
ricardoquesada authored May 6, 2010
668 }
669
670 - (void) dealloc
671 {
672 [window release];
673 [super dealloc];
674 }
675
676 // next delta time will be zero
677 -(void) applicationSignificantTimeChange:(UIApplication *)application
678 {
679 [[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
680 }
681
682 @end
Something went wrong with that request. Please try again.