stringByStandardizingPath EXC_BAD_ACCESS? - cocos2d-iphone

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!

Related

run another animation after running an animation with ccbanimationmanager

I'm running cocos2d with cocosbuilder 2.1 and I use the cocosbuilder animation delegate (CCBAnimationManagerDelegate::completedAnimationSequenceNamed) to get notified when an animation has completed and take actions like firing another cocosbuilder animation.
It runs fine the first time the foodfactoryshow animation is run from the delegate and after the animation is completed it also runs restoration animation correctly. However, when restoration animation is completed, the parameter name for -(void) completedAnimationSequenceNamed method is NULL!?
-(void) completedAnimationSequenceNamed:(NSString*)name
{
if ([name isEqualToString:#"foodfactoryshow"])
{
[manager runAnimationsForSequenceNamed:#"restoration"];
}
if ([name isEqualToString:#"restoration"])
{
[self colorLayerChanged];
self.gameLayer.isLock = true;
}
}
Is this a bug or am I not supposed to run animations from the CCBAnimationManagerDelegate::completedAnimationSequenceNamed method!?
Thanks in advance for your help.
I believe it is a CCBReader bug. There is an open issue about it in the CocosBuilder github page (https://github.com/cocos2d/CocosBuilder/issues/121). It should be fixed in the latest version of CocosBuilder + CCBReader, however, if you want to use the 2.1 version you can change the "sequenceCompleted" method of CCBAnimationManager to the following:
- (void) sequenceCompleted
{
NSString *completedSequenceName = [runningSequence.name copy];
int nextSeqId = runningSequence.chainedSequenceId;
runningSequence = NULL;
if (nextSeqId != -1)
{
[self runAnimationsForSequenceId:nextSeqId tweenDuration:0];
}
[delegate completedAnimationSequenceNamed:completedSequenceName];
[completedSequenceName release];
}

cocos2d-iphone: init method does not work properly

I'm studying Strougo/Wenderlich tutorial (space Viking project). I got troubles with chapter 4.
In RadarDish.m:
-(void)initAnimations
{
[self setTransmittingAnim: [self loadPlistForAnimationWithName:#"transmittingAnim" andClassName:NSStringFromClass([self class])]];
}
-(void)changeState:(CharacterStates)newState {
[self stopAllActions];
id action = nil;
[self setCharacterState:newState];
switch (newState) {
.
.
case kStateIdle:
action = [CCAnimate actionWithAnimation:transmittingAnim
restoreOriginalFrame:NO];
break; }
if (action != nil) {
[self runAction:action];
}
}
-(id)init
{
self=[super init];
if (self!=nil) {
.
.
[self initAnimations];
.
.
}
return self;
}
Exact the same code as in the tutorial. Failure:
*** Assertion failure in -[CCAnimate initWithAnimation:], /Users/macowner/Documents/examples/SpaceViking/SpaceViking/libs/cocos2d/CCActionInterval.
Using debugger with breakpoints, i noticed that value of transmittingAnim = nil.
So, if i put line with
[self setTransmittingAnim:
[self loadPlistForAnimationWithName:#"transmittingAnim" andClassName:NSStringFromClass([self class])]];
into case of
-(void)changeState then animation works correctly.
Why [self initAnimations] from (id)init is not called?
Im using cocos2d v.2 templates.
Great thanks in advance.
I had problems because I have been building project using cocos 2d v.2.0, while tutorial is based on cocos 2d templates v.1.x.x If you are going to follow the book "Learning Cocos2D", I strongly recommend you loading cocos2d-iphone version 1.0.1. Here is the link download cocos2d 1.x.x branch
if you still want to use latest cocos2d templates, I can give you some advice:
Follow the instructions in this link cocos2d v2.0 migration guide
You are going to have a lot of deprecations and changes to fix, so use this link to understand how to fix those deprecations and changes.
Now few words about the solution of the problem I mentioned here. In each of the GameObjects, EnemyObjects, and PowerUps, I added a method to override initWithFrameName.
-(id) initWithSpriteFrameName:(NSString*)frameName{
if ((self=[super init])) {
if ((self = [super initWithSpriteFrameName:frameName])) {
CCLOG(#"### RadarDish initialized");
[self initAnimations]; // 1
characterHealth = 100.0f; // 2
gameObjectType = kEnemyTypeRadarDish; // 3
[self changeState:kStateSpawning]; // 4
}
}
return self;
}
    This allows the GameObject and GameCharacter init methods to run before the CCSprite's initWithSpriteFrameName method to run.
    
    The Viking GameObject had to have a slightly different solution because it is initialized with initWithSpriteFrame rather than initWithSpriteFrameName. However, the override implementation is basically the same as the example of RadarDish above.

Can't load CCSpriteBatchNode using cocos2d when compiling in release mode

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?

Set Focus to a Cocoa Window in Carbon App

I'm trying to create a Cocoa Window within an otherwise Carbon Application (it's an OpenGL API that uses AGL. Can't change it so don't comment on that).
Here's a code snippit:
WindowRef winref = static_cast<eq::AGLWindow*>(getOSWindow())->getCarbonWindow();
vc = [[SFAttachedViewController alloc] initWithConfig:config]; //loads from view nib
NSPoint buttonPoint = NSMakePoint(event.pointerButtonPress.x + [cocoaWrap frame].origin.x, [cocoaWrap frame].size.height - event.pointerButtonPress.y + [cocoaWrap frame].origin.y);
MAAttachedWindow *attachedWindow = [[MAAttachedWindow alloc] initWithView:[vc view] attachedToPoint:buttonPoint onSide:side atDistance:0.0f]; // some Matt Gemmell goodness!
And I try to show it with one of the following lines:
// A)
[NSApp runModalForWindow:[attachedWindow retain]]; // makes a white box
// B)
NSWindow *cocoaWrap = [[NSWindow alloc] initWithWindowRef:winref];
[cocoaWrap addChildWindow:attachedWindow ordered:NSWindowAbove];
// C)
[attachedWindow makeKeyAndOrderFront:NSApp];
The window shows, but the focus is never given. I can't edit any of the controls, and everything is grayed out.
help!?
I tried
HIViewRef viewRef;
HICocoaViewCreate([vc view], 0, &viewRef);
WindowRef attachedRef = (WindowRef)[attachedWindow windowRef];
SetKeyboardFocus(attachedRef, viewRef, kControlNoPart);
Thinking it might have been a Carbon/Cocoa thing, but to no avail.
Did you call NSApplicationLoad() before calling Cocoa methods?

CCLabelAtlas setString doesn't update label

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.