Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @interface Layer_Sprite : CCLayer {
- typeOne *tOne;
- hud *hud;
- spriteSwat *sSwat;
- }
- @property (nonatomic, retain) NSString *spriteType;
- @property (nonatomic, assign) CCSprite *swat;
- @property (nonatomic, assign) enemyClass *sprite;
- @property (nonatomic) CGRect spriteBoundingBox;
- @property (nonatomic, assign) CGRect swatBoundingBox;
- @property (nonatomic, assign) int countdown;
- @property (nonatomic, assign) CCLabelTTF *countdownLabel;
- @property (nonatomic, assign) CGPoint touchLocation;
- @property (retain) NSMutableArray *spriteArray;
- @end
- ------------------------------------------------------------------------------------------
- #import "Layer_Sprite.h"
- int levelNumber;
- NSMutableString *levelNumberString;
- int randomSpriteNumber;
- int spriteNumber;
- CCMenu *spritePickMenu;
- CCNode *spritePick;
- @implementation Layer_Sprite
- @synthesize spriteType = spritetype_;
- @synthesize swat = swat_;
- @synthesize sprite = sprite_;
- @synthesize countdown;
- @synthesize countdownLabel = countdownLabel_;
- @synthesize touchLocation = touchLoaction_;
- @synthesize spriteBoundingBox = spriteBoundingBox_;
- @synthesize swatBoundingBox = swatBoundingBox_;
- @synthesize spriteArray = spriteArray_;
- -(id) init
- {
- // always call "super" init
- // Apple recommends to re-assign "self" with the "super" return value
- if( (self=[super init])) {
- self.isTouchEnabled = YES;
- //increase levelNumber
- levelNumber = 0;
- [self increaseLevelNumber];
- //pick SpriteType
- [self pickSpriteType];
- //find SpriteNumber
- [self findSpriteNumber];
- //create the confimration page
- [self spritePick];
- //update
- }
- return self;
- }
- - (void) increaseLevelNumber {
- levelNumber = levelNumber +1;
- }
- - (void) pickSpriteType {
- randomSpriteNumber = arc4random() % 2+1;
- switch (randomSpriteNumber) {
- case 1:
- self.spriteType = [NSString stringWithFormat:@"typeOne"];
- break;
- case 2:
- self.spriteType = [NSString stringWithFormat:@"typeTwo"];
- break;
- }
- NSLog(@"%@",self.spriteType);
- }
- - (void) findSpriteNumber {
- levelNumberString = [NSMutableString stringWithFormat:@"%d",levelNumber];
- NSString *tmp = [NSString stringWithString:self.spriteType];
- NSString *path = [[NSBundle mainBundle] pathForResource:@"plist_enemies" ofType:@"plist"];
- NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
- spriteNumber = [[[dict objectForKey:tmp] objectForKey:levelNumberString] intValue];
- }
- - (void) spritePick {
- CCMenuItemImage *go = [CCMenuItemImage itemFromNormalImage:@"button_go.png" selectedImage:@"button_go_selected.png" target:self selector:@selector(button)];
- spritePickMenu = [CCMenu menuWithItems:go, nil];
- spritePickMenu.position = ccp(0,0);
- spritePick = [CCNode node];
- [spritePick addChild:spritePickMenu];
- spritePick.position = ccp(240,160);
- [self addChild: spritePick];
- }
- -(void) button {
- [self moveUp];
- countdown = 4;
- self.countdownLabel = [CCLabelTTF labelWithString:@"3" fontName:@"Marker Felt" fontSize:24];
- self.countdownLabel.position = ccp(240,160);
- [self addChild:self.countdownLabel];
- [self schedule:@selector(tick:) interval:1];
- }
- - (void) tick:(ccTime)dt {
- countdown = countdown -1;
- if (countdown == 4) {
- [self.countdownLabel setString:@"3"];
- }
- if (countdown == 3) {
- [self.countdownLabel setString:@"2"];
- }
- if (countdown == 2) {
- [self.countdownLabel setString:@"1"];
- }
- if (countdown == 1) {
- [self.countdownLabel setString:@"GO"];
- }
- if (countdown == 0) {
- [self removeChild:self.countdownLabel cleanup:YES];
- [self buttonPress];
- [self unschedule: @selector(tick:)];
- }
- }
- - (void) buttonPress {
- [self scheduleUpdate];
- [self initSprite];
- [self initHUD];
- }
- - (void) initSprite {
- CGPoint randomPoint = ccp(arc4random()%480, arc4random()%320);
- NSLog(@"initSprite called");
- for (int i = 0; i < spriteNumber; i++) {
- if ([spritetype_ isEqualToString:@"typeOne"]) {
- self.sprite = [[typeOne alloc] initTypeOne];
- self.sprite.position = randomPoint;
- [self.spriteArray addObject:self.sprite];
- [self addChild:self.sprite];
- }
- if ([spritetype_ isEqualToString:@"typeTwo"]) {
- self.sprite = [[typeOne alloc] initTypeOne];
- self.sprite.position = randomPoint;
- [self.spriteArray addObject:self.sprite];
- [self addChild:self.sprite];
- }
- }
- self.swat = [[spriteSwat alloc] initSwat];
- self.swat.position = ccp(240,10);
- [self addChild:self.swat];
- }
- - (void) initHUD {
- hud *HUD = [[hud alloc] init];
- [self addChild:HUD];
- NSLog(@"HUD");
- }
- - (void) moveUp {
- [spritePick runAction:[CCMoveTo actionWithDuration:0.5 position:ccp(240,480)]];
- }
- -(void) registerWithTouchDispatcher {
- [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
- }
- -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
- return YES;
- }
- -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
- self.touchLocation = [touch locationInView: [touch view]];
- NSLog(@"%@", NSStringFromCGPoint(self.touchLocation));
- self.touchLocation = [[CCDirector sharedDirector] convertToGL:self.touchLocation];
- self.touchLocation = [self convertToNodeSpace:self.touchLocation];
- CGPoint swatLocation = ccp(240,10);
- float distance = ccpDistance(swatLocation,self.touchLocation);
- float duration = distance/800;
- id moveToTouch = [self.swat runAction:[CCMoveTo actionWithDuration:duration position:self.touchLocation]];
- id moveBack = [self.swat runAction:[CCMoveTo actionWithDuration:duration position:swatLocation]];
- [self.swat runAction:[CCSequence actions:moveToTouch, moveBack, nil]];
- if (CGRectIntersectsRect(self.sprite.textureRect,self.swat.textureRect)) {
- //NSLog(@"poo");
- }
- }
- - (void) update:(ccTime)dt{
- //NSLog(@"FLY:%@",NSStringFromCGRect(self.sprite.textureRect));
- //NSLog(@"SWAT:%@",NSStringFromCGRect(self.swat.textureRect));
- //NSLog(@"FLY POSITION:%@",NSStringFromCGPoint(self.sprite.position));
- //NSLog(@"SWAT POSITION:%@",NSStringFromCGPoint(self.swat.position));
- //NSLog(@"FLY:%@",NSStringFromCGRect(self.sprite.boundingBox));
- //NSLog(@"SWAT:%@",NSStringFromCGRect(self.swat.boundingBox));
- for (int i = 0; i < spriteNumber; i++) {
- NSLog(@"%@",[self.spriteArray objectAtIndex:i]);
- }
- if (CGRectIntersectsRect(self.sprite.boundingBox, self.swat.boundingBox)) {
- NSLog(@"detected");
- }
- }
- - (void) dealloc {
- [super dealloc];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement