Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>every time you call 'shootTheBullets', you are replacing the pointer that 'bullet' points to, which I would guess is why your content size reports 0, as the pointer is no longer pointing to the original object. You should be collecting your bullets in an array or similar and iterating through those to see if the bounding boxes intersect with your character.</p> <p>edit:</p> <p>I'm not sure why you'd be given a CCNode back when you put in a CCSprite, but in any case, here's a simple snippet of your basic requirement -- bullets, a character, bullet damage, character damage.</p> <p>Disclaimer: It's for cc2d 1.1, non-ARC . It's what I have handy right now.</p> <p><strong>Bullet.h</strong></p> <pre><code>#import "CCSprite.h" @interface Bullet : CCSprite { float damage; CGPoint velocity; } @property float damage; @property CGPoint velocity; +(Bullet *) bulletWithDamage:(float) damage andVelocity:(CGPoint) velocity; @end </code></pre> <p><strong>Bullet.m</strong></p> <pre><code>#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 </code></pre> <p><strong>PlayTest.h</strong></p> <pre><code>#import "cocos2d.h" @interface PlayTest : CCLayer { NSMutableArray *bullets; CCSprite *character; CGSize winSize; } @end </code></pre> <p><strong>PlayTest.m</strong></p> <pre><code>#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 { for (int i=0; i&lt; 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; for (int i=0; i&lt; bullets.count; i++) { Bullet *b = (Bullet *)[bullets objectAtIndex:i]; if(CGRectIntersectsRect(character.boundingBox, b.boundingBox) ) { NSLog(@"bullet collision with character"); [collisions addObject: b]; characterHit = YES; } else if (b.position.y &lt; 0.0f) { NSLog(@"bullet went off screen without hitting anything"); [collisions addObject: b]; b.damage = 0.0f; } } for (int i=0; i&lt; collisions.count; i++) { Bullet *b = (Bullet *)[collisions objectAtIndex:i]; // you could damage the character here, something like: // characterDamage -= b.damage [self removeChild:b cleanup:YES]; [bullets removeObject: b]; NSLog(@"bullets count is %d", bullets.count); } if(characterHit) // show character got damaged { if( ![character numberOfRunningActions]) { id one = [CCActionTween actionWithDuration:0.1f key:@"opacity" from:255 to:128]; id two = [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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload