objective c - cocos2d sprite collision detection, one sprite sometimes has a contentSize.width of 0.000000 -
hello having trouble collision detection in cocos2d game. working on game need test if bullet hits character. using cgrectintersectsrect method see if collision happens. in simulator can see bullet pass on character nothing happens. want character disappear if bullet hits it. in code have cclog statement outputs "collision" if bullet hits character. have 2 other cclog statements output contentsize.width of bullet , character. bullet's contentsize.width should 20.0 outputs width 0. here code collision detection.
-(void)testforbulletcollision:(cctime)delta{ cclog(@"bullet.contentsize.width = %f",bullet.contentsize.width); cclog(@"character.contentsize.width = %f",character.contentsize.width); if (cgrectintersectsrect([bullet boundingbox], [character boundingbox])) { cclog(@"bullet collision"); character.visible = no; bullet.visible = no; } }
here code creating character.
character = [ccsprite spritewithfile:@"mcharacter.png"]; character.position = ccp(screenwidth/3.4, screenheight/2 - 100); [self addchild:character z:-3];
here code creating bullet , bullet animation.
-(void)shootthebullets:(cctime)delta{ bullet = [ccsprite spritewithfile:@"thebullet.png"]; bullet.color = ccred; bullet.position = redenemy.position; [self addchild:bullet z:-1]; bulletrect = cgrectmake(bullet.position.x - (bullet.contentsize.width/2), bullet.position.y - (bullet.contentsize.height/2), bullet.contentsize.width, bullet.contentsize.height); cclog(@"bullet.contentsize.width = %f", bullet.contentsize.width); bulletmoveleft = [ccmoveto actionwithduration:4.0 position:ccp(-screenwidth, screenheight/2)]; [bullet runaction:bulletmoveleft]; [self schedule: @selector(stopbullets:)interval:18.0f/1.0f]; } -(void)stopbullets:(cctime)delta{ [self unschedule:@selector(shootthebullets:)]; }
here output.
2013-08-07 16:31:43.637 zach app[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.638 zach app[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.638 zach app[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.638 zach app[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.639 zach app[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.639 zach app[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.685 zach app[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.686 zach app[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.686 zach app[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.687 zach app[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.687 zach app[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.688 zach app[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.688 zach app[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.689 zach app[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.689 zach app[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.690 zach app[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.691 zach app[1320:a0b] bullet.contentsize.width = 20.000000 2013-08-07 16:31:43.836 zach app[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.837 zach app[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.838 zach app[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.838 zach app[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.839 zach app[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.840 zach app[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.841 zach app[1320:a0b] bullet.contentsize.width = 0.000000 2013-08-07 16:31:43.842 zach app[1320:a0b] character.contentsize.width = 64.000000 2013-08-07 16:31:43.843 zach app[1320:a0b] bullet.contentsize.width = 20.000000 2013-08-07 16:31:43.843 zach app[1320:a0b] character.contentsize.width = 64.000000
every time call 'shootthebullets', replacing pointer 'bullet' points to, guess why content size reports 0, pointer no longer pointing original object. should collecting bullets in array or similar , iterating through see if bounding boxes intersect character.
edit:
i'm not sure why you'd given ccnode when put in ccsprite, in case, here's simple snippet of basic requirement -- bullets, character, bullet damage, character damage.
disclaimer: it's cc2d 1.1, non-arc . it's have handy right now.
bullet.h
#import "ccsprite.h" @interface bullet : ccsprite { float damage; cgpoint velocity; } @property float damage; @property cgpoint velocity; +(bullet *) bulletwithdamage:(float) damage andvelocity:(cgpoint) velocity; @end
bullet.m
#import "bullet.h" @implementation bullet @synthesize damage, velocity; +(bullet *) bulletwithdamage:(float) damage andvelocity:(cgpoint) velocity { bullet *bullet = [[[self alloc] initwithfile:@"icon.png"] autorelease]; bullet.damage = damage; bullet.velocity = velocity; bullet.scale = 0.2f; return bullet; } -(id) init { if( (self = [super init]) ) {} return self; } @end
playtest.h
#import "cocos2d.h" @interface playtest : cclayer { nsmutablearray *bullets; ccsprite *character; cgsize winsize; } @end
playtest.m
#import "playtest.h" #import "bullet.h" @implementation playtest -(id) init { if( (self=[super init])) { winsize = [ccdirector shareddirector].winsize; bullets = [[nsmutablearray alloc] initwithcapacity: 10]; character = [ccsprite spritewithfile:@"icon.png"]; character.position = ccp(winsize.width/3.4f, winsize.height/2.0f - 100.0f); [self addchild:character z:-3]; [self schedule:@selector(shootthebullets:) interval:1.0f repeat: 10 delay: 3.0f]; [self scheduleupdate]; } return self; } -(void)shootthebullets:(cctime)delta{ float randx = ccrandom_0_1() * 0.5f; bullet *b = [bullet bulletwithdamage:5.0f andvelocity:ccp( randx, -1.0f)]; b.position = ccp(40.0f, winsize.height); [bullets addobject:b]; [self addchild: b]; } -(void) update:(cctime)dt { [self movethebullets]; [self checkcollisions]; } -(void) movethebullets { (int i=0; i< bullets.count; i++) { bullet *b = (bullet *)[bullets objectatindex:i]; b.position = ccpadd(b.position, b.velocity); } } -(void) checkcollisions { nsmutablearray *collisions = [nsmutablearray arraywithcapacity:10]; bool characterhit = no; (int i=0; i< bullets.count; i++) { bullet *b = (bullet *)[bullets objectatindex:i]; if(cgrectintersectsrect(character.boundingbox, b.boundingbox) ) { nslog(@"bullet collision character"); [collisions addobject: b]; characterhit = yes; } else if (b.position.y < 0.0f) { nslog(@"bullet went off screen without hitting anything"); [collisions addobject: b]; b.damage = 0.0f; } } (int i=0; i< collisions.count; i++) { bullet *b = (bullet *)[collisions objectatindex:i]; // damage character here, like: // characterdamage -= b.damage [self removechild:b cleanup:yes]; [bullets removeobject: b]; nslog(@"bullets count %d", bullets.count); } if(characterhit) // show character got damaged { if( ![character numberofrunningactions]) { id 1 = [ccactiontween actionwithduration:0.1f key:@"opacity" from:255 to:128]; id 2 = [ccactiontween actionwithduration:0.1f key:@"opacity" from:128 to:255]; id onetwo = [ccsequence actions: one, two, nil]; [character runaction: onetwo]; } } } -(void) dealloc { [bullets release]; [super dealloc]; } @end
Comments
Post a Comment