Following is part of my program:
CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
NSString *plistFileName = [NSString stringWithFormat:#"%#%#", aName, #".plist"];
NSString *pngFileName = [NSString stringWithFormat:#"%#%#", aName, #".png"];
[frameCache addSpriteFramesWithFile:plistFileName textureFilename:pngFileName];
self.sprite = [CCSprite spriteWithSpriteFrameName:#"bandit_standby_1"];
self.batchNode = [CCSpriteBatchNode batchNodeWithFile:pngFileName];
The problem occurs in the last line:
self.batchNode = [CCSpriteBatchNode batchNodeWithFile:pngFileName];
When compiling in debug mode, nothing is wrong.
However, after changing mode to release, an error occurs:
ImageIO: CGImageSourceCreateWithData data parameter is nil
Can anyone tell me where is wrong?
Related
I am using Cocos2D 3.1 and I am also using SpriteBuilder 1.1 to make a game for the iPhone. In one of my ccbi's however, every time I try to load it and present it on screen there is a crash in Cocos2D's CCFileUtils.m in the standarizePath method.
The NSString *ret = [path stringByStandardizingPath]; line below is the line that it crashes on and all it says is that it is a EXC_BAD_ACCESS. I use ARC in my app and so does Cocos2D so I am not sure how to fix this. Here is the method:
-(NSString*) standarizePath:(NSString*)path
{
NSString *ret = [path stringByStandardizingPath];
if( _searchMode == CCFileUtilsSearchModeSuffix )
ret = [self removeSuffixFromFile:ret];
return ret;
}
How would one fix this crash? I am not even sure where to begin because my other views work perfectly fine!
I had everything working fine also the timers where running. But when i ran into weird problems i had to restructure my cocos2d scene.
Now i am not able to fire the NSTimers anymore, the BonusTimetimer below is not even fired once.
Thanks in advance, below is the code.
BonusTimeTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(CountTimeBonus:) userInfo:nil repeats:YES];
// (lately i added the line below, but it does not help)
[[NSRunLoop mainRunLoop] addTimer:BonusTimeTimer forMode:NSDefaultRunLoopMode];
-(void)CountTimeBonus:(NSTimer *) sender {
NSLog (#"Method -> Countimebonus");
if ( (scoreTotal + 37) < TargetScore){
scoreTotal = scoreTotal + 37;
TimeBonus = TimeBonus-37;
NSString *level_timebonus = [NSString stringWithFormat:#"%d", TimeBonus];
[labelTimeBonus setString: level_timebonus];
NSString *scorestr = [NSString stringWithFormat:#"%d", scoreTotal];
[labelMainScore setString: scorestr];
[[SimpleAudioEngine sharedEngine] playEffect:#"light_switch_.mp3"];
}
else {
// add the Last few points and finish BonusTimer
scoreTotal = scoreTotal + TimeBonus;
TimeBonus=0;
NSString *level_timebonus = [NSString stringWithFormat:#"%d", timebonusgrayed];
[labelTimeBonus setString: #"" ];
[labelTimeBonusGrayed setString: level_timebonus];
NSString *scorestr = [NSString stringWithFormat:#"%d", scoreTotal];
[labelMainScore setString: scorestr];
[[SimpleAudioEngine sharedEngine] playEffect:#"light_switch_.mp3"] ;
[BonusTimeTimer invalidate];
BonusTimeTimer = nil;
timeBonusisdone = true;
timeBonusisactive = false;
}
}
I've never really drilled down that rabbit hole (ie why this is), but there is a persistent rumour around here that NSTimer and cocos2d do not mix well. Instead, I use cocos' own methods
[self schedule:#selector(CountTimeBonus:) interval:.01];
// and to invalidate this
[self unschedule:#selector(CountTimeBonus:)];
the CountTimeBonus signature will be :
-(void) CountTimeBonus:(ccTime) dt {
}
Thanks YvesLeBorg that worked for me.
There are a few more things to consider,
1) the cocos2d code:
[self schedule:#selector(myTimer:) delay:.01];
has a syntax problem, i used this instead (below):
[self schedule:#selector(myTimer:) interval:.01];
2) I got the NSTimer working again, but when i place the NStimer call in a do-while loop it won't work.
Thanks again and best of luck
Johan
I have an NSMutable array that I want to add Sprites to so that I can check them if they've hit the wall. I use this code to do so:
NSString *bulletName = [NSString stringWithFormat:#"tank%d_bullet.png", _type];
bullet = [CCSprite spriteWithSpriteFrameName:bulletName];
bullet.tag = _type;
bullet.position = ccpAdd(self.position, ccpMult(_shootVector, _turret.contentSize.height));
CCMoveBy * move = [CCMoveBy actionWithDuration:duration position:actualVector];
CCCallBlockN * call = [CCCallBlockN actionWithBlock:^(CCNode *node) {
[node removeFromParentAndCleanup:YES];
}];
if (!bulletIsGone) {
[self schedule:#selector(updator:) interval:0.01];
}
else {
[self unschedule:#selector(updator:)];
}
[bullet runAction:[CCSequence actions:move, call, nil]];
[_layer.batchNode addChild:bullet];
[bulletsArray addObject:bullet];
if ([bulletsArray objectAtIndex:0] == nil) {
NSLog(#"HELP");
}
NSLog(#"%#", [bulletsArray objectAtIndex:0]);
}
-(void)updator: (ccTime) dt{
for(CCSprite *bulletz in bulletsArray){
NSLog(#"this is the for loop");
CGRect rect1 = CGRectMake(bulletz.position.x - bulletz.contentSize.width/2, bulletz.position.y - bulletz.contentSize.height/2, 20, 20);
if ([_layer isWallAtRect:rect1]) {
NSLog(#"bulletHitWall");
[_layer.batchNode removeChild:bulletz cleanup:NO];
bulletIsGone = YES;
}
}
}
However, when I build and run, I get the console output of '(null)' and 'HELP.' The method before the 'updator' is called from touchesEnded. Can someone see what I'm doing wrong?
Thank you!
Do you initialise the array? That would seem like the most likely reason
Try this in your viewDidLoad method...
- (void)viewDidLoad
{
[super viewDidLoad];
bulletsArray = [NSMutableArray alloc] init];
}
Since NSMutableArray cannot hold nil objects, the only way the condition
[bulletsArray objectAtIndex:0] == nil
could evaluate to true is that bulletsArray is nil. You need to make sure that the array is properly allocated. A typical place to do it is the designated initializer of your class.
Why do you want to add bullets to another array? You already have a batch node that contains them all which is _layer.children.
Are you sure that the array itself (bulletsArray) is not nil? where is it initialized?
Finally you should consider looping with CCARRAY_FOREACH which is more performant.
my cocos2d game high score's are written into a plist. Every time i go into the plist only one row of data is written, every time I write into the plist it overwrites the previous data?
Heres my code for writing to my plist:
-(void)writeToPlistHighScore {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#",documentsDirectory,#"HighScore.plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] init];
[plistDict setObject:[NSNumber numberWithInteger:scoreNumber] forKey:#"Level2_HighScore"];
[plistDict writeToFile:filePath atomically: YES];
NSLog(#"HighScore wrote: %i", HighScore);
}
That's because you're creating a new, empty NSDictionary every time. Then you add one score and save that dictionary.
You need to initialize your dictionary with the existing plist so that the dictionary loads the existing highscores:
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile: writeToFile:filePath];
// if dictionary is nil the highscore file doesn't exist yet
if (plistDict == nil)
plistDict = [[NSMutableDictionary alloc] init];
Also, don't forget to release the dictionary when you're done! Right now you're leaking memory.
[plistDict release];
I have a CCLabelAtlas that I have on a layer to display the current score within my game...the problem that I am having with it is that it does not update when I use [scoreLabel setString:]. Here's how I have it laid out:
In the header:
#interface GameScene : CCLayer {
...
CCLabelAtlas* scoreLabel;
}
And in the init:
scoreLabel = [[CCLabelAtlas alloc] initWithString:#"0" charMapFile:#"scoreCharMap.png" itemWidth:6 itemHeight:8 startCharMap:'.'];
[scoreLabel setPosition:ccp(105, 414)];
[self addChild:scoreLabel z: 6];
scoreCharMap.png is a custom map that includes ./0123456789 of my desired font. When the score is changed, I attempt to do this to get the label to update with the current score:
NSString* str = [[NSString alloc] initWithFormat:#"%d", [[GameRunner sharedInstance] currentScore]];
[scoreLabel setString: str];
[str release];
[scoreLabel draw];
Problem is that it doesn't ever update - it just sits there and displays 0. I know for a fact due to breakpoints and debugging that setString is being called, and that the string that it should be displaying is correct - but it just doesn't update. Hard-coding the value and saying [scoreLabel setString:#"1234"] does nothing, either. What am I doing wrong here? I am using Cocos2d 0.99 - thanks in advance!
Maybe this is something wrong with the font you are using? Try using one of the font maps that came with Cocos2D and see if it works for you.
The method -[CCLabelAtlas setString:] does a few things.
Can you verify that following goes right: (place a breakpoint and step through the function)
resizeCapacity doesn't fail to resize
updateAtlasvalues retreives a UTF8 character array pointer. Inspect that to see if that's the correct string, and while you're there, inspect n in the same method, which should be your string length
See code for setString below:
- (void) setString:(NSString*) newString {
if( newString.length > textureAtlas_.totalQuads )
[textureAtlas_ resizeCapacity: newString.length];
[string_ release];
string_ = [newString retain];
[self updateAtlasValues];
CGSize s;
s.width = [string_ length] * itemWidth;
s.height = itemHeight;
[self setContentSize:s];
}
Let me know what the results are, if you still need any help.