Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-03-2012, 08:43 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 19
francis is on a distinguished road
Default Cocos 2d game simple collision detection

im really new to cocos2d and i downloaded this open source app.

what im trying to do is to put enemies on the app and hopefully add some other functions

i have successfully added the enemies however im stucked at this collision detection. as i said im new to cocos2d and i really dont know anything about

box2d and chipmunk.. i dont have knowledge in java or in c++

maybe there's some other way to detect collision??

here a sample of my code:
Code:
- (id)init {
//	NSLog(@"Game::init");
	
	if(![super init]) return nil;
	
	gameSuspended = YES;

	AtlasSpriteManager *spriteManager = (AtlasSpriteManager*)[self getChildByTag:kSpriteManager];

	[self initPlatforms];
	
	AtlasSprite *bird = [AtlasSprite spriteWithRect:CGRectMake(608,16,44,32) spriteManager:spriteManager];
	[spriteManager addChild:bird z:4 tag:kBird];

	AtlasSprite *bonus;

	for(int i=0; i<kNumBonuses; i++) {
		bonus = [AtlasSprite spriteWithRect:CGRectMake(608+i*32,256,25,25) spriteManager:spriteManager];
		[spriteManager addChild:bonus z:4 tag:kBonusStartTag+i];
		bonus.visible = NO;
	}

//	LabelAtlas *scoreLabel = [LabelAtlas labelAtlasWithString:@"0" charMapFile:@"charmap.png" itemWidth:24 itemHeight:32 startCharMap:' '];
//	[self addChild:scoreLabel z:5 tag:kScoreLabel];
	
	BitmapFontAtlas *scoreLabel = [BitmapFontAtlas bitmapFontAtlasWithString:@"0" fntFile:@"bitmapFont.fnt"];
	[self addChild:scoreLabel z:5 tag:kScoreLabel];
	scoreLabel.position = ccp(160,430);

	[self schedule:@selector(step:)];
	
	isTouchEnabled = YES;
	isAccelerometerEnabled = YES;

	[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kFPS)];
	
	[self startGame];
	[self schedule:@selector(gameLogic:) interval:5.0];
	return self;
	
	
}



-(void)addTarget {
	
	Sprite *target = [Sprite spriteWithFile:@"waa.gif"];
	target.position = ccp(300,400);
	
	[self addChild:target];
	
	
	
	CGSize winSize = [[Director sharedDirector]winSize];
	int minX = target.contentSize.height/2;
	int maxX = winSize.height -target.contentSize.height/2;
	int rangeX = maxX - minX;
	int actualX = (arc4random() % rangeX) +minX;
	
	int minDuration = 2.0;
	int maxDuration = 4.0;
	int rangeDuration = maxDuration - minDuration;
	int actualDuration = (arc4random() % rangeDuration) + minDuration;
	
	id actionMove = [MoveTo actionWithDuration:actualDuration position:ccp(-target.contentSize.width/2,actualX)];
	id actionMoveDone = [CallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)];
	[target runAction:[Sequence actions:actionMove, actionMoveDone,nil]];
	
	
}


-(void)spriteMoveFinished:(id)sender {
	Sprite *sprite = (Sprite *)sender;
	[self removeChild:sprite cleanup:YES];
}
-(void)gameLogic:(ccTime)dt {
	[self addTarget];
	
	
}
i hope someone could help i really need it.. thanks
francis is offline   Reply With Quote
Reply

Bookmarks

Tags
cocos2d, collision detection, iphone, sprites

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 382
9 members and 373 guests
apatsufas, comicool, Creativ, dansparrow, husthlj, LunarMoon, mer10, Murphy, pbart
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,677
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, husthlj
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:05 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0