/tests/EaseActionsTest.m

https://github.com/epictiles/cocos2d-iphone · Objective C · 893 lines · 628 code · 224 blank · 41 comment · 8 complexity · 3889648bc850bbe0edcdd3dc3f82c003 MD5 · raw file

  1. //
  2. // Ease Demo
  3. // a cocos2d example
  4. //
  5. // local import
  6. #import "cocos2d.h"
  7. #import "EaseActionsTest.h"
  8. static int sceneIdx=-1;
  9. static NSString *transitions[] = {
  10. @"SpriteEase",
  11. @"SpriteEaseInOut",
  12. @"SpriteEaseExponential",
  13. @"SpriteEaseExponentialInOut",
  14. @"SpriteEaseSine",
  15. @"SpriteEaseSineInOut",
  16. @"SpriteEaseElastic",
  17. @"SpriteEaseElasticInOut",
  18. @"SpriteEaseBounce",
  19. @"SpriteEaseBounceInOut",
  20. @"SpriteEaseBack",
  21. @"SpriteEaseBackInOut",
  22. @"SpeedTest",
  23. @"SchedulerTest",
  24. };
  25. enum {
  26. kTagAction1 = 1,
  27. kTagAction2 = 2,
  28. kTagSlider = 1,
  29. };
  30. Class nextAction(void);
  31. Class backAction(void);
  32. Class restartAction(void);
  33. Class nextAction()
  34. {
  35. sceneIdx++;
  36. sceneIdx = sceneIdx % ( sizeof(transitions) / sizeof(transitions[0]) );
  37. NSString *r = transitions[sceneIdx];
  38. Class c = NSClassFromString(r);
  39. return c;
  40. }
  41. Class backAction()
  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. Class restartAction()
  51. {
  52. NSString *r = transitions[sceneIdx];
  53. Class c = NSClassFromString(r);
  54. return c;
  55. }
  56. @implementation SpriteDemo
  57. -(id) init
  58. {
  59. if( (self=[super init])) {
  60. grossini = [[CCSprite alloc] initWithFile:@"grossini.png"];
  61. tamara = [[CCSprite alloc] initWithFile:@"grossinis_sister1.png"];
  62. kathia = [[CCSprite alloc] initWithFile:@"grossinis_sister2.png"];
  63. [self addChild: grossini z:3];
  64. [self addChild: kathia z:2];
  65. [self addChild: tamara z:1];
  66. CGSize s = [[CCDirector sharedDirector] winSize];
  67. [grossini setPosition: ccp(60, 50)];
  68. [kathia setPosition: ccp(60, 150)];
  69. [tamara setPosition: ccp(60, 250)];
  70. CCLabelTTF *label = [CCLabelTTF labelWithString:[self title] fontName:@"Arial" fontSize:32];
  71. [self addChild: label];
  72. [label setPosition: ccp(s.width/2, s.height-50)];
  73. CCMenuItemImage *item1 = [CCMenuItemImage itemFromNormalImage:@"b1.png" selectedImage:@"b2.png" target:self selector:@selector(backCallback:)];
  74. CCMenuItemImage *item2 = [CCMenuItemImage itemFromNormalImage:@"r1.png" selectedImage:@"r2.png" target:self selector:@selector(restartCallback:)];
  75. CCMenuItemImage *item3 = [CCMenuItemImage itemFromNormalImage:@"f1.png" selectedImage:@"f2.png" target:self selector:@selector(nextCallback:)];
  76. CCMenu *menu = [CCMenu menuWithItems:item1, item2, item3, nil];
  77. menu.position = CGPointZero;
  78. item1.position = ccp(s.width/2-100,30);
  79. item2.position = ccp(s.width/2, 30);
  80. item3.position = ccp(s.width/2+100,30);
  81. [self addChild: menu z:1];
  82. }
  83. return self;
  84. }
  85. -(void) dealloc
  86. {
  87. [grossini release];
  88. [tamara release];
  89. [kathia release];
  90. [super dealloc];
  91. }
  92. -(void) restartCallback: (id) sender
  93. {
  94. CCScene *s = [CCScene node];
  95. [s addChild: [restartAction() node]];
  96. [[CCDirector sharedDirector] replaceScene: s];
  97. }
  98. -(void) nextCallback: (id) sender
  99. {
  100. CCScene *s = [CCScene node];
  101. [s addChild: [nextAction() node]];
  102. [[CCDirector sharedDirector] replaceScene: s];
  103. }
  104. -(void) backCallback: (id) sender
  105. {
  106. CCScene *s = [CCScene node];
  107. [s addChild: [backAction() node]];
  108. [[CCDirector sharedDirector] replaceScene: s];
  109. }
  110. -(void) positionForTwo
  111. {
  112. grossini.position = ccp( 60, 120 );
  113. tamara.position = ccp( 60, 220);
  114. kathia.visible = NO;
  115. }
  116. -(NSString*) title
  117. {
  118. return @"No title";
  119. }
  120. @end
  121. #pragma mark -
  122. #pragma mark Ease Actions
  123. #define CCCA(x) [[x copy] autorelease]
  124. @implementation SpriteEase
  125. -(void) onEnter
  126. {
  127. [super onEnter];
  128. CGSize s = [[CCDirector sharedDirector] winSize];
  129. id move = [CCMoveBy actionWithDuration:3 position:ccp(s.width-130,0)];
  130. id move_back = [move reverse];
  131. id move_ease_in = [CCEaseIn actionWithAction:[[move copy] autorelease] rate:3.0f];
  132. id move_ease_in_back = [move_ease_in reverse];
  133. id move_ease_out = [CCEaseOut actionWithAction:[[move copy] autorelease] rate:3.0f];
  134. id move_ease_out_back = [move_ease_out reverse];
  135. id delay = [CCDelayTime actionWithDuration:0.25f];
  136. id seq1 = [CCSequence actions: move, delay, move_back, CCCA(delay), nil];
  137. id seq2 = [CCSequence actions: move_ease_in, CCCA(delay), move_ease_in_back, CCCA(delay), nil];
  138. id seq3 = [CCSequence actions: move_ease_out, CCCA(delay), move_ease_out_back, CCCA(delay), nil];
  139. CCAction *a2 = [grossini runAction: [CCRepeatForever actionWithAction:seq1]];
  140. [a2 setTag:1];
  141. CCAction *a1 =[tamara runAction: [CCRepeatForever actionWithAction:seq2]];
  142. [a1 setTag:1];
  143. CCAction *a = [kathia runAction: [CCRepeatForever actionWithAction:seq3]];
  144. [a setTag:1];
  145. [self schedule:@selector(testStopAction:) interval:6.25f];
  146. }
  147. -(void) testStopAction:(ccTime)dt
  148. {
  149. [self unschedule:_cmd];
  150. [tamara stopActionByTag:1];
  151. [kathia stopActionByTag:1];
  152. [grossini stopActionByTag:1];
  153. }
  154. -(NSString *) title
  155. {
  156. return @"EaseIn - EaseOut - Stop";
  157. }
  158. @end
  159. @implementation SpriteEaseInOut
  160. -(void) onEnter
  161. {
  162. [super onEnter];
  163. CGSize s = [[CCDirector sharedDirector] winSize];
  164. id move = [CCMoveBy actionWithDuration:3 position:ccp(s.width-130,0)];
  165. // id move_back = [move reverse];
  166. id move_ease_inout1 = [CCEaseInOut actionWithAction:[[move copy] autorelease] rate:2.0f];
  167. id move_ease_inout_back1 = [move_ease_inout1 reverse];
  168. id move_ease_inout2 = [CCEaseInOut actionWithAction:[[move copy] autorelease] rate:3.0f];
  169. id move_ease_inout_back2 = [move_ease_inout2 reverse];
  170. id move_ease_inout3 = [CCEaseInOut actionWithAction:[[move copy] autorelease] rate:4.0f];
  171. id move_ease_inout_back3 = [move_ease_inout3 reverse];
  172. id delay = [CCDelayTime actionWithDuration:0.25f];
  173. id seq1 = [CCSequence actions: move_ease_inout1, delay, move_ease_inout_back1, CCCA(delay), nil];
  174. id seq2 = [CCSequence actions: move_ease_inout2, CCCA(delay), move_ease_inout_back2, CCCA(delay), nil];
  175. id seq3 = [CCSequence actions: move_ease_inout3, CCCA(delay), move_ease_inout_back3, CCCA(delay), nil];
  176. [tamara runAction: [CCRepeatForever actionWithAction:seq1]];
  177. [kathia runAction: [CCRepeatForever actionWithAction:seq2]];
  178. [grossini runAction: [CCRepeatForever actionWithAction:seq3]];
  179. }
  180. -(NSString *) title
  181. {
  182. return @"EaseInOut and rates";
  183. }
  184. @end
  185. @implementation SpriteEaseSine
  186. -(void) onEnter
  187. {
  188. [super onEnter];
  189. CGSize s = [[CCDirector sharedDirector] winSize];
  190. id move = [CCMoveBy actionWithDuration:3 position:ccp(s.width-130,0)];
  191. id move_back = [move reverse];
  192. id move_ease_in = [CCEaseSineIn actionWithAction:[[move copy] autorelease]];
  193. id move_ease_in_back = [move_ease_in reverse];
  194. id move_ease_out = [CCEaseSineOut actionWithAction:[[move copy] autorelease]];
  195. id move_ease_out_back = [move_ease_out reverse];
  196. id delay = [CCDelayTime actionWithDuration:0.25f];
  197. id seq1 = [CCSequence actions: move, delay, move_back, CCCA(delay), nil];
  198. id seq2 = [CCSequence actions: move_ease_in, CCCA(delay), move_ease_in_back, CCCA(delay), nil];
  199. id seq3 = [CCSequence actions: move_ease_out, CCCA(delay), move_ease_out_back, CCCA(delay), nil];
  200. [grossini runAction: [CCRepeatForever actionWithAction:seq1]];
  201. [tamara runAction: [CCRepeatForever actionWithAction:seq2]];
  202. [kathia runAction: [CCRepeatForever actionWithAction:seq3]];
  203. }
  204. -(NSString *) title
  205. {
  206. return @"EaseSineIn - EaseSineOut";
  207. }
  208. @end
  209. @implementation SpriteEaseSineInOut
  210. -(void) onEnter
  211. {
  212. [super onEnter];
  213. CGSize s = [[CCDirector sharedDirector] winSize];
  214. id move = [CCMoveBy actionWithDuration:3 position:ccp(s.width-130,0)];
  215. id move_back = [move reverse];
  216. id move_ease = [CCEaseSineInOut actionWithAction:[[move copy] autorelease]];
  217. id move_ease_back = [move_ease reverse];
  218. id delay = [CCDelayTime actionWithDuration:0.25f];
  219. id seq1 = [CCSequence actions: move, delay, move_back, CCCA(delay), nil];
  220. id seq2 = [CCSequence actions: move_ease, CCCA(delay), move_ease_back, CCCA(delay), nil];
  221. [self positionForTwo];
  222. [grossini runAction: [CCRepeatForever actionWithAction:seq1]];
  223. [tamara runAction: [CCRepeatForever actionWithAction:seq2]];
  224. }
  225. -(NSString *) title
  226. {
  227. return @"EaseSineInOut action";
  228. }
  229. @end
  230. #pragma mark SpriteEaseExponential
  231. @implementation SpriteEaseExponential
  232. -(void) onEnter
  233. {
  234. [super onEnter];
  235. CGSize s = [[CCDirector sharedDirector] winSize];
  236. id move = [CCMoveBy actionWithDuration:3 position:ccp(s.width-130,0)];
  237. id move_back = [move reverse];
  238. id move_ease_in = [CCEaseExponentialIn actionWithAction:[[move copy] autorelease]];
  239. id move_ease_in_back = [move_ease_in reverse];
  240. id move_ease_out = [CCEaseExponentialOut actionWithAction:[[move copy] autorelease]];
  241. id move_ease_out_back = [move_ease_out reverse];
  242. id delay = [CCDelayTime actionWithDuration:0.25f];
  243. id seq1 = [CCSequence actions: move, delay, move_back, CCCA(delay), nil];
  244. id seq2 = [CCSequence actions: move_ease_in, CCCA(delay), move_ease_in_back, CCCA(delay), nil];
  245. id seq3 = [CCSequence actions: move_ease_out, CCCA(delay), move_ease_out_back, CCCA(delay), nil];
  246. [grossini runAction: [CCRepeatForever actionWithAction:seq1]];
  247. [tamara runAction: [CCRepeatForever actionWithAction:seq2]];
  248. [kathia runAction: [CCRepeatForever actionWithAction:seq3]];
  249. }
  250. -(NSString *) title
  251. {
  252. return @"ExpIn - ExpOut actions";
  253. }
  254. @end
  255. #pragma mark SpriteEaseExponentialInOut
  256. @implementation SpriteEaseExponentialInOut
  257. -(void) onEnter
  258. {
  259. [super onEnter];
  260. CGSize s = [[CCDirector sharedDirector] winSize];
  261. id move = [CCMoveBy actionWithDuration:3 position:ccp(s.width-130,0)];
  262. id move_back = [move reverse];
  263. id move_ease = [CCEaseExponentialInOut actionWithAction:[[move copy] autorelease]];
  264. id move_ease_back = [move_ease reverse];
  265. id delay = [CCDelayTime actionWithDuration:0.25f];
  266. id seq1 = [CCSequence actions: move, delay, move_back, CCCA(delay), nil];
  267. id seq2 = [CCSequence actions: move_ease, CCCA(delay), move_ease_back, CCCA(delay), nil];
  268. [self positionForTwo];
  269. [grossini runAction: [CCRepeatForever actionWithAction:seq1]];
  270. [tamara runAction: [CCRepeatForever actionWithAction:seq2]];
  271. }
  272. -(NSString *) title
  273. {
  274. return @"EaseExponentialInOut action";
  275. }
  276. @end
  277. #pragma mark SpriteEaseElasticInOut
  278. @implementation SpriteEaseElasticInOut
  279. -(void) onEnter
  280. {
  281. [super onEnter];
  282. CGSize s = [[CCDirector sharedDirector] winSize];
  283. id move = [CCMoveBy actionWithDuration:3 position:ccp(s.width-130,0)];
  284. id move_ease_inout1 = [CCEaseElasticInOut actionWithAction:[[move copy] autorelease] period:0.3f];
  285. id move_ease_inout_back1 = [move_ease_inout1 reverse];
  286. id move_ease_inout2 = [CCEaseElasticInOut actionWithAction:[[move copy] autorelease] period:0.45f];
  287. id move_ease_inout_back2 = [move_ease_inout2 reverse];
  288. id move_ease_inout3 = [CCEaseElasticInOut actionWithAction:[[move copy] autorelease] period:0.6f];
  289. id move_ease_inout_back3 = [move_ease_inout3 reverse];
  290. id delay = [CCDelayTime actionWithDuration:0.25f];
  291. id seq1 = [CCSequence actions: move_ease_inout1, delay, move_ease_inout_back1, CCCA(delay), nil];
  292. id seq2 = [CCSequence actions: move_ease_inout2, CCCA(delay), move_ease_inout_back2, CCCA(delay), nil];
  293. id seq3 = [CCSequence actions: move_ease_inout3, CCCA(delay), move_ease_inout_back3, CCCA(delay), nil];
  294. [tamara runAction: [CCRepeatForever actionWithAction:seq1]];
  295. [kathia runAction: [CCRepeatForever actionWithAction:seq2]];
  296. [grossini runAction: [CCRepeatForever actionWithAction:seq3]];
  297. }
  298. -(NSString *) title
  299. {
  300. return @"EaseElasticInOut action";
  301. }
  302. @end
  303. #pragma mark SpriteEaseElastic
  304. @implementation SpriteEaseElastic
  305. -(void) onEnter
  306. {
  307. [super onEnter];
  308. CGSize s = [[CCDirector sharedDirector] winSize];
  309. id move = [CCMoveBy actionWithDuration:3 position:ccp(s.width-130,0)];
  310. id move_back = [move reverse];
  311. id move_ease_in = [CCEaseElasticIn actionWithAction:[[move copy] autorelease]];
  312. id move_ease_in_back = [move_ease_in reverse];
  313. id move_ease_out = [CCEaseElasticOut actionWithAction:[[move copy] autorelease]];
  314. id move_ease_out_back = [move_ease_out reverse];
  315. id delay = [CCDelayTime actionWithDuration:0.25f];
  316. id seq1 = [CCSequence actions: move, delay, move_back, CCCA(delay), nil];
  317. id seq2 = [CCSequence actions: move_ease_in, CCCA(delay), move_ease_in_back, CCCA(delay), nil];
  318. id seq3 = [CCSequence actions: move_ease_out, CCCA(delay), move_ease_out_back, CCCA(delay), nil];
  319. [grossini runAction: [CCRepeatForever actionWithAction:seq1]];
  320. [tamara runAction: [CCRepeatForever actionWithAction:seq2]];
  321. [kathia runAction: [CCRepeatForever actionWithAction:seq3]];
  322. }
  323. -(NSString *) title
  324. {
  325. return @"Elastic In - Out actions";
  326. }
  327. @end
  328. #pragma mark SpriteEaseBounce
  329. @implementation SpriteEaseBounce
  330. -(void) onEnter
  331. {
  332. [super onEnter];
  333. CGSize s = [[CCDirector sharedDirector] winSize];
  334. id move = [CCMoveBy actionWithDuration:3 position:ccp(s.width-130,0)];
  335. id move_back = [move reverse];
  336. id move_ease_in = [CCEaseBounceIn actionWithAction:[[move copy] autorelease]];
  337. id move_ease_in_back = [move_ease_in reverse];
  338. id move_ease_out = [CCEaseBounceOut actionWithAction:[[move copy] autorelease]];
  339. id move_ease_out_back = [move_ease_out reverse];
  340. id delay = [CCDelayTime actionWithDuration:0.25f];
  341. id seq1 = [CCSequence actions: move, delay, move_back, CCCA(delay), nil];
  342. id seq2 = [CCSequence actions: move_ease_in, CCCA(delay), move_ease_in_back, CCCA(delay), nil];
  343. id seq3 = [CCSequence actions: move_ease_out, CCCA(delay), move_ease_out_back, CCCA(delay), nil];
  344. [grossini runAction: [CCRepeatForever actionWithAction:seq1]];
  345. [tamara runAction: [CCRepeatForever actionWithAction:seq2]];
  346. [kathia runAction: [CCRepeatForever actionWithAction:seq3]];
  347. }
  348. -(NSString *) title
  349. {
  350. return @"Bounce In - Out actions";
  351. }
  352. @end
  353. @implementation SpriteEaseBounceInOut
  354. -(void) onEnter
  355. {
  356. [super onEnter];
  357. CGSize s = [[CCDirector sharedDirector] winSize];
  358. id move = [CCMoveBy actionWithDuration:3 position:ccp(s.width-130,0)];
  359. id move_back = [move reverse];
  360. id move_ease = [CCEaseBounceInOut actionWithAction:[[move copy] autorelease]];
  361. id move_ease_back = [move_ease reverse];
  362. id delay = [CCDelayTime actionWithDuration:0.25f];
  363. id seq1 = [CCSequence actions: move, delay, move_back, CCCA(delay), nil];
  364. id seq2 = [CCSequence actions: move_ease, CCCA(delay), move_ease_back, CCCA(delay), nil];
  365. [self positionForTwo];
  366. [grossini runAction: [CCRepeatForever actionWithAction:seq1]];
  367. [tamara runAction: [CCRepeatForever actionWithAction:seq2]];
  368. }
  369. -(NSString *) title
  370. {
  371. return @"EaseBounceInOut action";
  372. }
  373. @end
  374. #pragma mark SpriteEaseBack
  375. @implementation SpriteEaseBack
  376. -(void) onEnter
  377. {
  378. [super onEnter];
  379. CGSize s = [[CCDirector sharedDirector] winSize];
  380. id move = [CCMoveBy actionWithDuration:3 position:ccp(s.width-130,0)];
  381. id move_back = [move reverse];
  382. id move_ease_in = [CCEaseBackIn actionWithAction:[[move copy] autorelease]];
  383. id move_ease_in_back = [move_ease_in reverse];
  384. id move_ease_out = [CCEaseBackOut actionWithAction:[[move copy] autorelease]];
  385. id move_ease_out_back = [move_ease_out reverse];
  386. id delay = [CCDelayTime actionWithDuration:0.25f];
  387. id seq1 = [CCSequence actions: move, delay, move_back, CCCA(delay), nil];
  388. id seq2 = [CCSequence actions: move_ease_in, CCCA(delay), move_ease_in_back, CCCA(delay), nil];
  389. id seq3 = [CCSequence actions: move_ease_out, CCCA(delay), move_ease_out_back, CCCA(delay), nil];
  390. [grossini runAction: [CCRepeatForever actionWithAction:seq1]];
  391. [tamara runAction: [CCRepeatForever actionWithAction:seq2]];
  392. [kathia runAction: [CCRepeatForever actionWithAction:seq3]];
  393. }
  394. -(NSString *) title
  395. {
  396. return @"Back In - Out actions";
  397. }
  398. @end
  399. @implementation SpriteEaseBackInOut
  400. -(void) onEnter
  401. {
  402. [super onEnter];
  403. CGSize s = [[CCDirector sharedDirector] winSize];
  404. id move = [CCMoveBy actionWithDuration:3 position:ccp(s.width-130,0)];
  405. id move_back = [move reverse];
  406. id move_ease = [CCEaseBackInOut actionWithAction:[[move copy] autorelease]];
  407. id move_ease_back = [move_ease reverse];
  408. id delay = [CCDelayTime actionWithDuration:0.25f];
  409. id seq1 = [CCSequence actions: move, delay, move_back, CCCA(delay), nil];
  410. id seq2 = [CCSequence actions: move_ease, CCCA(delay), move_ease_back, CCCA(delay), nil];
  411. [self positionForTwo];
  412. [grossini runAction: [CCRepeatForever actionWithAction:seq1]];
  413. [tamara runAction: [CCRepeatForever actionWithAction:seq2]];
  414. }
  415. -(NSString *) title
  416. {
  417. return @"EaseBackInOut action";
  418. }
  419. @end
  420. #pragma mark SpeedTest
  421. @implementation SpeedTest
  422. -(void) onEnter
  423. {
  424. [super onEnter];
  425. CGSize s = [[CCDirector sharedDirector] winSize];
  426. // rotate and jump
  427. CCActionInterval *jump1 = [CCJumpBy actionWithDuration:4 position:ccp(-s.width+80,0) height:100 jumps:4];
  428. CCActionInterval *jump2 = [jump1 reverse];
  429. CCActionInterval *rot1 = [CCRotateBy actionWithDuration:4 angle:360*2];
  430. CCActionInterval *rot2 = [rot1 reverse];
  431. id seq3_1 = [CCSequence actions:jump2, jump1, nil];
  432. id seq3_2 = [CCSequence actions: rot1, rot2, nil];
  433. id spawn = [CCSpawn actions:seq3_1, seq3_2, nil];
  434. id action = [CCSpeed actionWithAction: [CCRepeatForever actionWithAction:spawn] speed:1.0f];
  435. [action setTag: kTagAction1];
  436. id action2 = [[action copy] autorelease];
  437. id action3 = [[action copy] autorelease];
  438. [action2 setTag:kTagAction1];
  439. [action3 setTag:kTagAction1];
  440. [grossini runAction: action2 ];
  441. [tamara runAction: action3];
  442. [kathia runAction:action];
  443. [self schedule:@selector(altertime:) interval:1.0f];
  444. }
  445. -(void) altertime:(ccTime)dt
  446. {
  447. id action1 = [grossini getActionByTag:kTagAction1];
  448. id action2 = [tamara getActionByTag:kTagAction1];
  449. id action3 = [kathia getActionByTag:kTagAction1];
  450. [action1 setSpeed: CCRANDOM_0_1() * 2];
  451. [action2 setSpeed: CCRANDOM_0_1() * 2];
  452. [action3 setSpeed: CCRANDOM_0_1() * 2];
  453. }
  454. -(NSString *) title
  455. {
  456. return @"Speed action";
  457. }
  458. @end
  459. @implementation SchedulerTest
  460. #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
  461. - (UISlider *)sliderCtl
  462. {
  463. if (sliderCtl == nil)
  464. {
  465. CGRect frame = CGRectMake(174.0f, 12.0f, 120.0f, 7.0f);
  466. sliderCtl = [[UISlider alloc] initWithFrame:frame];
  467. [sliderCtl addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
  468. // in case the parent view draws with a custom color or gradient, use a transparent color
  469. sliderCtl.backgroundColor = [UIColor clearColor];
  470. sliderCtl.minimumValue = 0.0f;
  471. sliderCtl.maximumValue = 2.0f;
  472. sliderCtl.continuous = YES;
  473. sliderCtl.value = 1.0f;
  474. sliderCtl.tag = kTagSlider; // tag this view for later so we can remove it from recycled table cells
  475. }
  476. return [sliderCtl autorelease];
  477. }
  478. #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
  479. -(NSSlider*) sliderCtl
  480. {
  481. if( sliderCtl == nil )
  482. {
  483. sliderCtl = [[NSSlider alloc] initWithFrame: NSMakeRect (0, 0, 200, 20)];
  484. [sliderCtl setMinValue: 0];
  485. [sliderCtl setMaxValue: 3];
  486. [sliderCtl setFloatValue: 1];
  487. [sliderCtl setAction: @selector (sliderAction:)];
  488. [sliderCtl setTarget: self];
  489. [sliderCtl setContinuous: YES];
  490. }
  491. return sliderCtl;
  492. }
  493. #endif // Mac
  494. -(void) sliderAction:(id) sender
  495. {
  496. #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
  497. [[CCScheduler sharedScheduler] setTimeScale: sliderCtl.value];
  498. #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
  499. [[CCScheduler sharedScheduler] setTimeScale: [sliderCtl floatValue]];
  500. #endif
  501. }
  502. -(void) onEnter
  503. {
  504. [super onEnter];
  505. CGSize s = [[CCDirector sharedDirector] winSize];
  506. // rotate and jump
  507. CCActionInterval *jump1 = [CCJumpBy actionWithDuration:4 position:ccp(-s.width+80,0) height:100 jumps:4];
  508. CCActionInterval *jump2 = [jump1 reverse];
  509. CCActionInterval *rot1 = [CCRotateBy actionWithDuration:4 angle:360*2];
  510. CCActionInterval *rot2 = [rot1 reverse];
  511. id seq3_1 = [CCSequence actions:jump2, jump1, nil];
  512. id seq3_2 = [CCSequence actions: rot1, rot2, nil];
  513. id spawn = [CCSpawn actions:seq3_1, seq3_2, nil];
  514. id action = [CCRepeatForever actionWithAction:spawn];
  515. id action2 = [[action copy] autorelease];
  516. id action3 = [[action copy] autorelease];
  517. [grossini runAction: [CCSpeed actionWithAction:action speed:0.5f]];
  518. [tamara runAction: [CCSpeed actionWithAction:action2 speed:1.5f]];
  519. [kathia runAction: [CCSpeed actionWithAction:action3 speed:1.0f]];
  520. CCParticleSystem *emitter = [CCParticleFireworks node];
  521. [self addChild:emitter];
  522. sliderCtl = [self sliderCtl];
  523. #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
  524. [[[[CCDirector sharedDirector] openGLView] window] addSubview: sliderCtl];
  525. #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
  526. MacGLView *view = [[CCDirector sharedDirector] openGLView];
  527. if( ! overlayWindow ) {
  528. overlayWindow = [[NSWindow alloc] initWithContentRect:[[view window] frame]
  529. styleMask:NSBorderlessWindowMask
  530. backing:NSBackingStoreBuffered
  531. defer:NO];
  532. [overlayWindow setFrame:[[view window] frame] display:NO];
  533. [[overlayWindow contentView] addSubview:sliderCtl];
  534. [overlayWindow setParentWindow:[view window]];
  535. [overlayWindow setOpaque:NO];
  536. [overlayWindow makeKeyAndOrderFront:nil];
  537. [overlayWindow setBackgroundColor:[NSColor clearColor]];
  538. [[overlayWindow contentView] display];
  539. }
  540. [[view window] addChildWindow:overlayWindow ordered:NSWindowAbove];
  541. #endif
  542. }
  543. -(void) onExit
  544. {
  545. [sliderCtl removeFromSuperview];
  546. #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
  547. #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
  548. MacGLView *view = [[CCDirector sharedDirector] openGLView];
  549. [[view window] removeChildWindow:overlayWindow];
  550. [overlayWindow release];
  551. overlayWindow = nil;
  552. #endif
  553. [super onExit];
  554. }
  555. -(NSString *) title
  556. {
  557. return @"Scheduler scaleTime Test";
  558. }
  559. @end
  560. #pragma mark -
  561. #pragma mark AppController
  562. // CLASS IMPLEMENTATIONS
  563. #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
  564. @implementation AppController
  565. - (void) applicationDidFinishLaunching:(UIApplication*)application
  566. {
  567. // CC_DIRECTOR_INIT()
  568. //
  569. // 1. Initializes an EAGLView with 0-bit depth format, and RGB565 render buffer
  570. // 2. EAGLView multiple touches: disabled
  571. // 3. creates a UIWindow, and assign it to the "window" var (it must already be declared)
  572. // 4. Parents EAGLView to the newly created window
  573. // 5. Creates Display Link Director
  574. // 5a. If it fails, it will use an NSTimer director
  575. // 6. It will try to run at 60 FPS
  576. // 7. Display FPS: NO
  577. // 8. Device orientation: Portrait
  578. // 9. Connects the director to the EAGLView
  579. //
  580. CC_DIRECTOR_INIT();
  581. // Obtain the shared director in order to...
  582. CCDirector *director = [CCDirector sharedDirector];
  583. // Sets landscape mode
  584. [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
  585. // Turn on display FPS
  586. [director setDisplayFPS:YES];
  587. // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
  588. if( ! [director enableRetinaDisplay:YES] )
  589. CCLOG(@"Retina Display Not supported");
  590. // Default texture format for PNG/BMP/TIFF/JPEG/GIF images
  591. // It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
  592. // You can change anytime.
  593. [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
  594. // When in iPad / RetinaDisplay mode, CCFileUtils will append the "-ipad" / "-hd" to all loaded files
  595. // If the -ipad / -hdfile is not found, it will load the non-suffixed version
  596. [CCFileUtils setiPadSuffix:@"-ipad"]; // Default on iPad is "" (empty string)
  597. [CCFileUtils setRetinaDisplaySuffix:@"-hd"]; // Default on RetinaDisplay is "-hd"
  598. CCScene *scene = [CCScene node];
  599. [scene addChild: [nextAction() node]];
  600. [director runWithScene: scene];
  601. }
  602. // getting a call, pause the game
  603. -(void) applicationWillResignActive:(UIApplication *)application
  604. {
  605. [[CCDirector sharedDirector] pause];
  606. }
  607. // call got rejected
  608. -(void) applicationDidBecomeActive:(UIApplication *)application
  609. {
  610. [[CCDirector sharedDirector] resume];
  611. }
  612. -(void) applicationDidEnterBackground:(UIApplication*)application
  613. {
  614. [[CCDirector sharedDirector] stopAnimation];
  615. }
  616. -(void) applicationWillEnterForeground:(UIApplication*)application
  617. {
  618. [[CCDirector sharedDirector] startAnimation];
  619. }
  620. // application will be killed
  621. - (void)applicationWillTerminate:(UIApplication *)application
  622. {
  623. CC_DIRECTOR_END();
  624. }
  625. // purge memroy
  626. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
  627. {
  628. [[CCDirector sharedDirector] purgeCachedData];
  629. }
  630. // next delta time will be zero
  631. -(void) applicationSignificantTimeChange:(UIApplication *)application
  632. {
  633. [[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
  634. }
  635. - (void) dealloc
  636. {
  637. [window release];
  638. [super dealloc];
  639. }
  640. @end
  641. #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
  642. @implementation cocos2dmacAppDelegate
  643. @synthesize window=window_, glView=glView_;
  644. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  645. {
  646. CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
  647. [director setDisplayFPS:YES];
  648. [director setOpenGLView:glView_];
  649. // [director setProjection:kCCDirectorProjection2D];
  650. // Enable "moving" mouse event. Default no.
  651. [window_ setAcceptsMouseMovedEvents:NO];
  652. // EXPERIMENTAL stuff.
  653. // 'Effects' don't work correctly when autoscale is turned on.
  654. [director setResizeMode:kCCDirectorResize_AutoScale];
  655. CCScene *scene = [CCScene node];
  656. [scene addChild: [nextAction() node]];
  657. [director runWithScene:scene];
  658. }
  659. - (BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication *) theApplication
  660. {
  661. return YES;
  662. }
  663. - (IBAction)toggleFullScreen: (id)sender
  664. {
  665. CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
  666. [director setFullScreen: ! [director isFullScreen] ];
  667. }
  668. @end
  669. #endif