Guest User

Untitled

a guest
Dec 11th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.93 KB | None | 0 0
  1. //
  2. // MainMenuSceneS.m
  3. // Swipe-Switch Scenes
  4. //
  5. // Created by Christopher Whitman on 3/22/11.
  6. // Copyright __MyCompanyName__ 2011. All rights reserved.
  7. //
  8.  
  9. // Import the interfaces
  10. #import "MainMenuScene.h"
  11. #import "PreBookScene.h"
  12. #import "IntroScene.h"
  13. #import "PageOneScene.h"
  14. #import "PageTwoScene.h"
  15. #import "PageThreeScene.h"
  16. #import "CCTransition.h"
  17. #import "GlobalDataManager.h"
  18. #import "GameStateManager.h"
  19.  
  20.  
  21. // MainMenuScene implementation
  22. @implementation MainMenuLayer
  23.  
  24. +(CCScene *) scene
  25. {
  26. // 'scene' is an autorelease object.
  27. CCScene *scene = [CCScene node];
  28.  
  29. // 'layer' is an autorelease object.
  30. MainMenuLayer *layer = [MainMenuLayer node];
  31.  
  32. // add layer as a child to scene
  33. [scene addChild: layer];
  34.  
  35. // return the scene
  36. return scene;
  37. }
  38.  
  39. // set up our menus
  40. - (void) setUpMenus
  41. {
  42. // menu item to read the book
  43. CCMenuItemImage *menuItemReadToMe = [CCMenuItemImage
  44. itemFromNormalImage:@"readToMe-ipad.png"
  45. selectedImage:@"readToMe-hover-ipad.png"
  46. target:self
  47. selector:@selector(setReadToMeFlagAndGoToBook:)];
  48.  
  49.  
  50. // menu item to read the book
  51. CCMenuItemImage *menuItemReadItMyself = [CCMenuItemImage
  52. itemFromNormalImage:@"readItMyself-ipad.png"
  53. selectedImage:@"readItMyself-hover-ipad.png"
  54. target:self
  55. selector:@selector(setReadItMyselfFlagAndGoToBook:)];
  56.  
  57. // menu item to read the book
  58. CCMenuItemImage *menuItemAutoPlay = [CCMenuItemImage
  59. itemFromNormalImage:@"autoplay-ipad.png"
  60. selectedImage:@"autoplay-hover-ipad.png"
  61. target:self
  62. selector:@selector(setAutoPlayFlagAndGoToBook:)];
  63.  
  64.  
  65. // menu item to build a snowman game
  66. CCMenuItemImage *menuItemBuildASnowman = [CCMenuItemImage
  67. itemFromNormalImage:@"buildASnowman-ipad.png"
  68. selectedImage:@"buildASnowman-hover-ipad.png"
  69. target:self
  70. selector:@selector(setAutoPlayFlagAndGoToBook:)];
  71.  
  72.  
  73. // create an instance of our menu
  74. CCMenu * mainMenu = [CCMenu menuWithItems:menuItemReadToMe, menuItemReadItMyself, menuItemAutoPlay, menuItemBuildASnowman, nil];
  75.  
  76. // align our menu items
  77. [mainMenu alignItemsHorizontally];
  78.  
  79. // get window parameters and put em in an object
  80. CGSize s = [[CCDirector sharedDirector] winSize];
  81.  
  82. // move menu to right
  83. [mainMenu setPosition:ccp(s.width/2,60)];
  84.  
  85. // add menu to our scene
  86. [self addChild:mainMenu];
  87.  
  88. }
  89. // on "init" you need to initialize your instance
  90. -(id) init
  91. {
  92. // always call "super" init
  93. // Apple recommends to re-assign "self" with the "super" return value
  94. if( (self=[super initWithColor:ccc4(255, 255, 255, 255)] )) {
  95. //[[CCTextureCache sharedTextureCache] addImageAsync:@"" target:self selector:@selector(textureLoaded:)];
  96. //[[CCTextureCache sharedTextureCache] addImageAsync:@"Page1-ipad.jpeg" target:self selector:@selector(:)];
  97. [[CCTextureCache sharedTextureCache] addImageAsync:@"Page1-ipad.jpeg" target:self selector:@selector(imageLoaded:)];
  98.  
  99. GlobalDataManager *action = [[GlobalDataManager alloc]init];
  100. [action setUserIsInBookFlag];
  101.  
  102. // initialize window object
  103. CGSize s = [[CCDirector sharedDirector] winSize];
  104.  
  105. // add a background to the layer
  106. CCSprite* background = [CCSprite spriteWithFile:@"bg.jpg"];
  107. background.tag = 1;
  108. // background.opacity = 1.0;
  109. background.position = ccp(s.width/2, s.height/2);
  110. // background.anchorPoint = ccp(0,0);
  111. [self addChild:background];
  112.  
  113. // id fadeIn = [CCFadeIn actionWithDuration:3];
  114.  
  115. // [background runAction:fadeIn];
  116.  
  117. // add the top text to the layer
  118. CCSprite* main_menu_logo = [CCSprite spriteWithFile:@"mainMenuLogo-ipad.png"];
  119. main_menu_logo.tag = 1;
  120. main_menu_logo.opacity = 0.0;
  121. main_menu_logo.position = ccp(s.width/2, s.height/1.12);
  122. // background.anchorPoint = ccp(0,0);
  123. [self addChild:main_menu_logo];
  124.  
  125. // fade logo in
  126. // delay first though
  127. // don't forget to pass CCSequence nil since it's an array of objects (NSArray)
  128. id logoDelay = [CCDelayTime actionWithDuration:.8];
  129. id fadeLogoIn = [CCFadeIn actionWithDuration:3];
  130. id mainMenuSequence = [CCSequence actions:logoDelay, fadeLogoIn, nil];
  131.  
  132. [main_menu_logo runAction:mainMenuSequence];
  133.  
  134. // add bottom text to the layer
  135. CCSprite* main_menu_bottom = [CCSprite spriteWithFile:@"authorNames-ipad.png"];
  136. main_menu_bottom.tag = 1;
  137. main_menu_bottom.position = ccp(s.width/2, s.height/1.34);
  138. main_menu_bottom.opacity = 0.0;
  139. [self addChild:main_menu_bottom];
  140.  
  141. id mainMenuBottomDelay = [CCDelayTime actionWithDuration:.8];
  142. id mainMenuBottomFadeIn = [CCFadeIn actionWithDuration:3];
  143. id MainMenuBottomSeq = [CCSequence actions: mainMenuBottomDelay, mainMenuBottomFadeIn, nil];
  144.  
  145. [main_menu_bottom runAction:MainMenuBottomSeq];
  146.  
  147. // add bottom text to the layer
  148. CCSprite* main_menu_top = [CCSprite spriteWithFile:@"photographFantasy-ipad.png"];
  149. main_menu_top.tag = 1;
  150. main_menu_top.position = ccp(s.width/2, s.height/1.25);
  151. main_menu_top.opacity = 0.0;
  152. [self addChild:main_menu_top];
  153.  
  154. id mainMenuTopDelay = [CCDelayTime actionWithDuration:.8];
  155. id mainMenuTopFadeIn = [CCFadeIn actionWithDuration:3];
  156. id MainMenuTopSeq = [CCSequence actions: mainMenuTopDelay, mainMenuTopFadeIn, nil];
  157.  
  158. [main_menu_top runAction:MainMenuTopSeq];
  159.  
  160. // PARTICLE SYSTEM EMITTER SETUP /////////////////////////////////////////
  161. // request window size
  162. CGSize winSize = [[CCDirector sharedDirector] winSize];
  163.  
  164. // initialize the particle system to be a snow system
  165. emitter = [CCParticleSnow node];
  166.  
  167. // add a texture (snowflake image in this case)
  168. emitter.texture = [[CCTextureCache sharedTextureCache] addImage:@"snow.png"]; //Set the texture to use our fire_particle.png
  169.  
  170. // position the emitter (top-middle)
  171. emitter.position = ccp (winSize.width/2, winSize.height);
  172.  
  173. emitter.life = 10;
  174. emitter.lifeVar = 1;
  175.  
  176.  
  177. // make the particles bigger for the ipad
  178. emitter.scale = 1.2;
  179.  
  180. // set the gravity direction
  181. emitter.gravity = ccp(0,-10);
  182.  
  183. // speed of particles
  184. emitter.speed = 15;
  185. emitter.speedVar = 30;
  186.  
  187. [self addChild:emitter];
  188. // EMITTER SETUP END ///////////////////////////////////////////////////////
  189.  
  190. // retrieve the state of the game for user preference
  191. /// int scene = [[NSUserDefaults standardUserDefaults] integerForKey:@"lastViewedScene"];
  192.  
  193. // create and initialize a Label
  194. CCLabelTTF *logo = [CCLabelTTF labelWithString:@"" fontName:@"Marker Felt" fontSize:64];
  195.  
  196. // ask director the the window size
  197. CGSize size = [[CCDirector sharedDirector] winSize];
  198.  
  199. // position the label on the center of the screen
  200. logo.position = ccp( size.width /2 , size.height/1.3 );
  201.  
  202. // add the label as a child to this Layer
  203. [self addChild: logo];
  204.  
  205. [self setUpMenus];
  206. }
  207. return self;
  208. }
  209.  
  210. // UIAlertView implementation
  211. // The button index will allow us to use logic triggered by button's clicks
  212. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  213. {
  214. if (buttonIndex == 0) {
  215. // since we're resuming, retrieve user preference from NSUserDefaults
  216. // that has the last viewed scene
  217. int lastSceneViewed = [[NSUserDefaults standardUserDefaults] integerForKey:@"lastViewedScene"];
  218.  
  219. StoryStateManager *lastScene = [[StoryStateManager alloc] init];
  220. [lastScene resumeStoryToScene:lastSceneViewed];
  221. } else if (buttonIndex == 1) {
  222. PreBookScene *preBookScene = [PreBookScene node];
  223. [[CCDirector sharedDirector] replaceScene:preBookScene];
  224. } else if (buttonIndex == 2) {
  225. NSLog(@"Cancel button to UIAlertView was touched");
  226. }
  227. }
  228.  
  229. - (void) promptToResumeOrStartOver
  230. {
  231. // retrieve the last viewed scene from NSUserDefaults
  232. int lastSceneViewed = [[NSUserDefaults standardUserDefaults] integerForKey:@"lastViewedScene"];
  233.  
  234. // check to see if the last viewed scene is the first scene of the book
  235. // if the last viewed scene is the first scene, skip the dialog/prompt
  236. // and go directly to the first scene.
  237. if (lastSceneViewed > -1) {
  238. // UIAlertView usage
  239. UIAlertView *alert = [[UIAlertView alloc] init];
  240. [alert setTitle:@"Would you like to resume where you left off?"];
  241. [alert setDelegate:self];
  242. [alert addButtonWithTitle:@"Resume"]; // button index 0
  243. [alert addButtonWithTitle:@"Start Over"]; // button index 1
  244. [alert addButtonWithTitle:@"Cancel"];
  245. [alert show];
  246. [alert release];
  247. } else {
  248. // initialize data for story state then load first scene
  249. // with resumeStoryToScene:(integer) function
  250. StoryStateManager *lastScene = [[StoryStateManager alloc] init];
  251. [lastScene resumeStoryToScene:-1];
  252. }
  253. }
  254.  
  255. - (void) setReadToMeFlagAndGoToBook: (CCMenuItem *) menuitem
  256. {
  257. // set story play mode to "Read to Me"
  258. GlobalDataManager *data = [[GlobalDataManager alloc] init];
  259. [data setStoryPlayMode:@"readToMe"];
  260.  
  261. [self promptToResumeOrStartOver];
  262. }
  263.  
  264. - (void) setReadItMyselfFlagAndGoToBook: (CCMenuItem *) menuitem
  265. {
  266. // set story play mode to string
  267. GlobalDataManager *data = [[GlobalDataManager alloc] init];
  268. [data setStoryPlayMode:@"readItMyself"];
  269.  
  270. [self promptToResumeOrStartOver];
  271. }
  272.  
  273. - (void) setAutoPlayFlagAndGoToBook: (CCMenuItem *) menuitem
  274. {
  275. // set story play mode to auto play
  276. GlobalDataManager *data = [[GlobalDataManager alloc] init];
  277. [data setStoryPlayMode:@"autoPlay"];
  278.  
  279.  
  280. [self promptToResumeOrStartOver];
  281. }
  282.  
  283. -(void) imageLoaded: (CCTexture2D*) sitw {
  284.  
  285. }
  286.  
  287. // on "dealloc" you need to release all your retained objects
  288. - (void) dealloc
  289. {
  290. [super dealloc];
  291. }
  292. @end
Add Comment
Please, Sign In to add comment