i am trying to detect Collision of two sprites in the following way...but when i try to run the game no collision takes place.... what am i possibly doing wrong??
- (void)update:(ccTime)dt {
CGRect projectileRect = CGRectMake(projectile.position.x - (projectile.contentSize.width/2),
projectile.position.y - (projectile.contentSize.height/2),
projectile.contentSize.width,
projectile.contentSize.height);
//CGRectMake(0,220,320,50);
CGRect targetRects = CGRectMake(_monkey.position.x - (_monkey.contentSize.width/2),
_monkey.position.y - (_monkey.contentSize.height/2),
_monkey.contentSize.width,
_monkey.contentSize.height);
if (CGRectIntersectsRect(projectileRect, targetRects)) {
NSLog(@"ha ha Collision detected");
}
}
projectile sprite is animating from top of screen to the bottom and monkey sprite is animating from left to right at the bottom the projectile goes through the monkey but the log does not get called???
- (void)update:(ccTime)dt {
CGRect projectileRect = [projectile boundingBox];
CGRect targetRects = [_monkey boundingBox];
if (CGRectIntersectsRect(projectileRect, targetRects))
{
NSLog(@"ha ha Collision detected");
}
CGRect projectileRects = CGRectMake(projectile.position.x - (projectile.contentSize.width/2),
projectile.position.y - (projectile.contentSize.height/2),
projectile.contentSize.width,
projectile.contentSize.height);
CGRect targetRect = CGRectMake(_monkey.position.x - (_monkey.contentSize.width/2),
_monkey.position.y - (_monkey.contentSize.height/2),
_monkey.contentSize.width,
_monkey.contentSize.height);
if (CGRectIntersectsRect(projectileRects, targetRect)) {
NSLog(@"@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
}
}
-(void)spriteMoveFinished:(id)sender {
//NSLog(@"spriteMoveFinished");
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];
if (sprite.tag == 1) {
[_targets removeObject:sprite];
} else if (sprite.tag == 2) {
[_projectiles removeObject:sprite];
}
}
-(void)addTarget {
projectile = [CCSprite spriteWithFile:@"egg.png" rect:CGRectMake(0, 0, 10, 10)];
projectile.position = ccp(_bear.position.x,_bear.position.y-20);
projectile.tag=2;
[self addChild:projectile];
CGPoint realDest = ccp(_bear.position.x, _bear.position.y - 380);
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
// Move projectile to actual endpoint
[projectile runAction:[CCSequence actions:
[CCMoveTo actionWithDuration:actualDuration position:realDest],
[CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
nil]];
// Add to projectiles array
projectile.tag = 2;
[_projectiles addObject:projectile];
}
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
if(CGRectContainsPoint(CGRectMake(0,0,320,50),touchLocation))
{
if (![_walkMonkey isDone]) {
[_monkey runAction:_walkMonkey];
}
}
else {
}
return YES;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
if(CGRectContainsPoint(CGRectMake(0,0,320,50),touchLocation))
{
[_monkey stopAction:_walkMonkey];
}
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
CGPoint translation = ccpSub(touchLocation, oldTouchLocation);
if (translation.x > 3) {
_monkey.flipX=YES;
}
else if (translation.x < -3){
_monkey.flipX=NO;
}
if(CGRectContainsPoint(CGRectMake(40,0,240,50),touchLocation))
{
CGPoint newPos = ccpAdd(translation,_monkey.position);
if(newPos.x >= 320 || newPos.x <= 20)
{
NSLog(@"monkey not walking");
}
else {
newPos.y = 100;
_monkey.position = newPos;
}
}
}
Collision detection concerns the detection of collisions between objects in the virtual environment. Primarily employed to stop objects moving through each other and the environment. Collision Detection is everywhere in computer games: between characters and characters, between characters and terrain, etc.
Circle Circle Collision But instead of checking against the radius of only one circle, we check against the sum of the radiuses of both circles. If the distance between the centers of the circle is less than the sum of the radiuses, then the circles are colliding!
Definition. Collision detection is the process of determining computationally if two or more objects intersect, the time, and the parts of the objects that are involved in the intersection.
Cocos2D suits companies that decide to build games and interactive apps that are 2D, developed with the C++ programming language, JavaScript, C# and Lua, and can be played on both Android and iOS mobile technologies, as well as across all the main operating systems (Windows, Mac, Linux).
You should use the built-in functionality:
CGRect projectileRect = [projectile boundingBox];
CGRect targetRects = [_monkey boundingBox];
if (CGRectIntersectsRect(projectileRect, targetRects))
{
NSLog(@"ha ha Collision detected");
}
The boundingBox method takes a couple more things into account, for example if the node is positioned relative to its parent.
Also note that prefixing variables with an underscore is considered bad practice in Objective-C. Apple reserves variable names with leading underscores for their internal libraries. If you really need to classify instance variables, one common way is to suffix them with an underscore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With