i have this app like doodlejump then i added monsters and projectiles and i have finished it
however im getting this 2 crashes.. one(sometimes) when the app opens this occurs :
Code:
[Sprite setString:]: unrecognized selector sent to instance
and the other is when i fire many projectiles this occurs:
Code:
malloc: *** error for object 0x5e99f00: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
i really dont know what to do now...
here's my game.m
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:)];
[self schedule:@selector(update:)];
isTouchEnabled = YES;
isAccelerometerEnabled = YES;
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kFPS)];
[self startGame];
[self schedule:@selector(gameLogic:) interval:5.0];
_targets = [[NSMutableArray alloc]init];
_projectiles = [[NSMutableArray alloc]init];
return self;
}
-(void)addTarget {
Sprite *target = [Sprite spriteWithFile:@"komodo.png"];
target.position = ccp(300,200);
[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,actualX)];
id actionMoveDone = [CallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)];
[target runAction:[Sequence actions:actionMove, actionMoveDone,nil]];
target.tag = 1;
[_targets addObject:target];
}
-(void)spriteMoveFinished:(id)sender {
Sprite *sprite = (Sprite *)sender;
[self removeChild:sprite cleanup:YES];
if (sprite.tag == 1) {
[_targets removeObject:sprite];
} else if (sprite.tag == 2) {
[_projectiles removeObject:sprite];
}
}
-(void)gameLogic:(ccTime)dt {
[self addTarget];
}
-(void) update:(ccTime)dt {
AtlasSpriteManager *spriteManager = (AtlasSpriteManager*)[self getChildByTag:kSpriteManager];
AtlasSprite *bird = (AtlasSprite *) [spriteManager getChildByTag:kBird];
CGRect birdRect = CGRectMake(bird.position.x - (bird.contentSize.width/2), bird.position.y - (bird.contentSize.height/2), bird.contentSize.width/1.8,bird.contentSize.height/1.5);
NSMutableArray *targetsToDelete = [[NSMutableArray alloc]init];
for (Sprite *target in _targets) {
CGRect targetRect = CGRectMake(
target.position.x - (target.contentSize.width/2),
target.position.y - (target.contentSize.height/2),
target.contentSize.width,
target.contentSize.height);
NSMutableArray *projectilesToDelete = [[NSMutableArray alloc]init];
for(Sprite *projectile in _projectiles) {
CGRect projectileRect = CGRectMake(projectile.position.x - (projectile.contentSize.width/2),projectile.position.y - (projectile.contentSize.height/2), projectile.contentSize.width, projectile.contentSize.height);
if(CGRectIntersectsRect(birdRect,targetRect)) {
NSLog(@"HIT!");
gameSuspended = YES;
[self showHighscores];
}
if(CGRectIntersectsRect(targetRect,projectileRect )) {
NSLog(@"Boom Boom Pow");
[targetsToDelete addObject:target];
}
for (Sprite *target in targetsToDelete) {
[_targets removeObject:target];
[self removeChild:target cleanup:YES];
}
if(targetsToDelete.count > 0) {
[projectilesToDelete addObject:projectile];
}
[targetsToDelete release];
}
for(Sprite *projectile in projectilesToDelete) {
[_projectiles removeObject:projectile];
[self removeChild:projectile cleanup:YES];
}
[projectilesToDelete release];
}
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
AtlasSpriteManager *spriteManager = (AtlasSpriteManager*)[self getChildByTag:kSpriteManager];
AtlasSprite *bird = (AtlasSprite*)[spriteManager getChildByTag:kBird];
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[Director sharedDirector]convertCoordinate:location];
CGSize winSize = [[Director sharedDirector]winSize];
Sprite *projectile = [Sprite spriteWithFile:@"bullet.png"];
projectile.position = ccp(bird.position.x,bird.position.y);
int offX = location.x - projectile.position.x;
int offY = location.y - projectile.position.y;
[self addChild:projectile];
int realX = winSize.width + (projectile.contentSize.width/2);
float ratio = (float) offY / (float) offX;
int realY = (realX *ratio) + projectile.position.y;
CGPoint realDest = ccp(realX, realY);
int offRealX = realX - projectile.position.x;
int offRealY = realY - projectile.position.y;
float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
float velocity = 480/1;
float realMoveDuration = length/velocity;
[projectile runAction:[Sequence actions:[MoveTo actionWithDuration:realMoveDuration position:realDest],
[CallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)], nil]];
NSLog(@"Shoot!");
projectile.tag = 2;
[_projectiles addObject:projectile];
}
i badly need help. because i have been working on this for days then when i finally finished it
this crashes appear.... :c hope pro's would look and answer THANKS SO MUCH