Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Cocos 2d game simple collision detection

francisfrancis Posts: 19Registered Users
edited January 2012 in iPhone SDK Game Development
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:
- (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
Sign In or Register to comment.