Advertisement
Guest User

Untitled

a guest
Jun 11th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. @interface Layer_Sprite : CCLayer {
  2. typeOne *tOne;
  3. hud *hud;
  4. spriteSwat *sSwat;
  5. }
  6.  
  7. @property (nonatomic, retain) NSString *spriteType;
  8. @property (nonatomic, assign) CCSprite *swat;
  9. @property (nonatomic, assign) enemyClass *sprite;
  10. @property (nonatomic) CGRect spriteBoundingBox;
  11. @property (nonatomic, assign) CGRect swatBoundingBox;
  12. @property (nonatomic, assign) int countdown;
  13. @property (nonatomic, assign) CCLabelTTF *countdownLabel;
  14. @property (nonatomic, assign) CGPoint touchLocation;
  15. @property (retain) NSMutableArray *spriteArray;
  16.  
  17. @end
  18.  
  19. ------------------------------------------------------------------------------------------
  20.  
  21. #import "Layer_Sprite.h"
  22.  
  23. int levelNumber;
  24. NSMutableString *levelNumberString;
  25. int randomSpriteNumber;
  26. int spriteNumber;
  27. CCMenu *spritePickMenu;
  28. CCNode *spritePick;
  29.  
  30. @implementation Layer_Sprite
  31.  
  32. @synthesize spriteType = spritetype_;
  33. @synthesize swat = swat_;
  34. @synthesize sprite = sprite_;
  35. @synthesize countdown;
  36. @synthesize countdownLabel = countdownLabel_;
  37. @synthesize touchLocation = touchLoaction_;
  38. @synthesize spriteBoundingBox = spriteBoundingBox_;
  39. @synthesize swatBoundingBox = swatBoundingBox_;
  40. @synthesize spriteArray = spriteArray_;
  41.  
  42. -(id) init
  43. {
  44. // always call "super" init
  45. // Apple recommends to re-assign "self" with the "super" return value
  46. if( (self=[super init])) {
  47.  
  48. self.isTouchEnabled = YES;
  49.  
  50. //increase levelNumber
  51. levelNumber = 0;
  52. [self increaseLevelNumber];
  53.  
  54. //pick SpriteType
  55. [self pickSpriteType];
  56.  
  57. //find SpriteNumber
  58. [self findSpriteNumber];
  59.  
  60. //create the confimration page
  61. [self spritePick];
  62.  
  63. //update
  64.  
  65.  
  66. }
  67. return self;
  68. }
  69.  
  70. - (void) increaseLevelNumber {
  71. levelNumber = levelNumber +1;
  72. }
  73.  
  74. - (void) pickSpriteType {
  75.  
  76. randomSpriteNumber = arc4random() % 2+1;
  77.  
  78. switch (randomSpriteNumber) {
  79. case 1:
  80. self.spriteType = [NSString stringWithFormat:@"typeOne"];
  81. break;
  82.  
  83. case 2:
  84. self.spriteType = [NSString stringWithFormat:@"typeTwo"];
  85. break;
  86. }
  87.  
  88. NSLog(@"%@",self.spriteType);
  89. }
  90.  
  91. - (void) findSpriteNumber {
  92.  
  93. levelNumberString = [NSMutableString stringWithFormat:@"%d",levelNumber];
  94. NSString *tmp = [NSString stringWithString:self.spriteType];
  95.  
  96. NSString *path = [[NSBundle mainBundle] pathForResource:@"plist_enemies" ofType:@"plist"];
  97. NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
  98. spriteNumber = [[[dict objectForKey:tmp] objectForKey:levelNumberString] intValue];
  99.  
  100. }
  101.  
  102. - (void) spritePick {
  103.  
  104. CCMenuItemImage *go = [CCMenuItemImage itemFromNormalImage:@"button_go.png" selectedImage:@"button_go_selected.png" target:self selector:@selector(button)];
  105. spritePickMenu = [CCMenu menuWithItems:go, nil];
  106. spritePickMenu.position = ccp(0,0);
  107.  
  108.  
  109. spritePick = [CCNode node];
  110. [spritePick addChild:spritePickMenu];
  111. spritePick.position = ccp(240,160);
  112. [self addChild: spritePick];
  113.  
  114.  
  115. }
  116.  
  117. -(void) button {
  118. [self moveUp];
  119. countdown = 4;
  120. self.countdownLabel = [CCLabelTTF labelWithString:@"3" fontName:@"Marker Felt" fontSize:24];
  121. self.countdownLabel.position = ccp(240,160);
  122. [self addChild:self.countdownLabel];
  123. [self schedule:@selector(tick:) interval:1];
  124.  
  125. }
  126.  
  127. - (void) tick:(ccTime)dt {
  128. countdown = countdown -1;
  129. if (countdown == 4) {
  130. [self.countdownLabel setString:@"3"];
  131. }
  132. if (countdown == 3) {
  133. [self.countdownLabel setString:@"2"];
  134. }
  135. if (countdown == 2) {
  136. [self.countdownLabel setString:@"1"];
  137. }
  138. if (countdown == 1) {
  139. [self.countdownLabel setString:@"GO"];
  140. }
  141. if (countdown == 0) {
  142. [self removeChild:self.countdownLabel cleanup:YES];
  143. [self buttonPress];
  144. [self unschedule: @selector(tick:)];
  145. }
  146. }
  147.  
  148. - (void) buttonPress {
  149. [self scheduleUpdate];
  150. [self initSprite];
  151. [self initHUD];
  152. }
  153.  
  154. - (void) initSprite {
  155. CGPoint randomPoint = ccp(arc4random()%480, arc4random()%320);
  156. NSLog(@"initSprite called");
  157. for (int i = 0; i < spriteNumber; i++) {
  158. if ([spritetype_ isEqualToString:@"typeOne"]) {
  159.  
  160. self.sprite = [[typeOne alloc] initTypeOne];
  161. self.sprite.position = randomPoint;
  162. [self.spriteArray addObject:self.sprite];
  163. [self addChild:self.sprite];
  164.  
  165.  
  166. }
  167. if ([spritetype_ isEqualToString:@"typeTwo"]) {
  168. self.sprite = [[typeOne alloc] initTypeOne];
  169. self.sprite.position = randomPoint;
  170. [self.spriteArray addObject:self.sprite];
  171. [self addChild:self.sprite];
  172.  
  173. }
  174. }
  175. self.swat = [[spriteSwat alloc] initSwat];
  176. self.swat.position = ccp(240,10);
  177. [self addChild:self.swat];
  178.  
  179.  
  180. }
  181.  
  182. - (void) initHUD {
  183. hud *HUD = [[hud alloc] init];
  184. [self addChild:HUD];
  185. NSLog(@"HUD");
  186. }
  187.  
  188.  
  189. - (void) moveUp {
  190. [spritePick runAction:[CCMoveTo actionWithDuration:0.5 position:ccp(240,480)]];
  191. }
  192.  
  193. -(void) registerWithTouchDispatcher {
  194. [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
  195. }
  196.  
  197. -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
  198. return YES;
  199. }
  200.  
  201. -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
  202. self.touchLocation = [touch locationInView: [touch view]];
  203. NSLog(@"%@", NSStringFromCGPoint(self.touchLocation));
  204. self.touchLocation = [[CCDirector sharedDirector] convertToGL:self.touchLocation];
  205. self.touchLocation = [self convertToNodeSpace:self.touchLocation];
  206. CGPoint swatLocation = ccp(240,10);
  207. float distance = ccpDistance(swatLocation,self.touchLocation);
  208. float duration = distance/800;
  209. id moveToTouch = [self.swat runAction:[CCMoveTo actionWithDuration:duration position:self.touchLocation]];
  210. id moveBack = [self.swat runAction:[CCMoveTo actionWithDuration:duration position:swatLocation]];
  211. [self.swat runAction:[CCSequence actions:moveToTouch, moveBack, nil]];
  212. if (CGRectIntersectsRect(self.sprite.textureRect,self.swat.textureRect)) {
  213. //NSLog(@"poo");
  214. }
  215.  
  216. }
  217.  
  218. - (void) update:(ccTime)dt{
  219. //NSLog(@"FLY:%@",NSStringFromCGRect(self.sprite.textureRect));
  220. //NSLog(@"SWAT:%@",NSStringFromCGRect(self.swat.textureRect));
  221. //NSLog(@"FLY POSITION:%@",NSStringFromCGPoint(self.sprite.position));
  222. //NSLog(@"SWAT POSITION:%@",NSStringFromCGPoint(self.swat.position));
  223. //NSLog(@"FLY:%@",NSStringFromCGRect(self.sprite.boundingBox));
  224. //NSLog(@"SWAT:%@",NSStringFromCGRect(self.swat.boundingBox));
  225. for (int i = 0; i < spriteNumber; i++) {
  226. NSLog(@"%@",[self.spriteArray objectAtIndex:i]);
  227. }
  228. if (CGRectIntersectsRect(self.sprite.boundingBox, self.swat.boundingBox)) {
  229. NSLog(@"detected");
  230. }
  231. }
  232.  
  233. - (void) dealloc {
  234. [super dealloc];
  235. }
  236.  
  237. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement