Search code examples
objective-cios5xcode4sparrow-framework

Sparrow - how to fix SIGABRT error, Game initwithwitdth


I've just started with the sparrow framework, and have been following "The Big Sparrow Tutorial" by Gamua themselves. I'm on the first part of the tutorial, using the AppScaffold 1.3 but when I try to compile my basic code it hangs at the loading screen and gives me a SIGABRT error.

I put an exception breakpoint, and it stopped here, in GameController.m (seen at bottom) of the AppScaffold:

mGame = [[Game alloc] initWithWidth:gameWidth height:gameHeight];

This was also my only output:

2012-07-30 07:19:54.787 AppScaffold[1682:10a03] -[Game initWithWidth:height:]: unrecognized selector sent to instance 0x7553980
(lldb)

I am using the stock AppScaffold, the only thing I changed was the Game.m.

This is my Game.m:

@interface Game : SPSprite
@end

@implementation Game
{
@private
    SPImage *mBackground;
    SPImage *mBasket;
    NSMutableArray *mEggs;
}

- (id)init
{
    if((self = [super init]))
    {
        //load the background image first, add it to the display tree
        //and keep it for later use
        mBackground = [[SPImage alloc] initWithContentsOfFile:@"background.png"];
        [self addChild:mBackground];

        //load the image of the basket, add it to the display tree
        //and keep it for later use
        mBasket = [[SPImage alloc] initWithContentsOfFile:@"basket.png"];
        [self addChild:mBasket];

        //create a list that will hold the eggs,
        //which we will add and remove repeatedly during the game
        mEggs = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)dealloc
{
    [mBackground release];
    [mBasket release];
    [mEggs release];
    [super dealloc];
}

@end

I've tried my best to use my basic troubleshooting tactics, but I'm very new to Obj-C and Sparrow and could use a hand :)

Thanks

EDIT: I've addded the GameController.m contents here for clarity:

//
//  GameController.m
//  AppScaffold
//

#import <OpenGLES/ES1/gl.h>
#import "GameController.h"


@interface GameController ()

- (UIInterfaceOrientation)initialInterfaceOrientation;

@end


@implementation GameController

- (id)initWithWidth:(float)width height:(float)height
{
    if ((self = [super initWithWidth:width height:height]))
    {
        float gameWidth  = width;
        float gameHeight = height;

        // if we start up in landscape mode, width and height are swapped.
        UIInterfaceOrientation orientation = [self initialInterfaceOrientation];
        if (UIInterfaceOrientationIsLandscape(orientation)) SP_SWAP(gameWidth, gameHeight, float);

        mGame = [[Game alloc] initWithWidth:gameWidth height:gameHeight];

        mGame.pivotX = gameWidth  / 2;
        mGame.pivotY = gameHeight / 2;

        mGame.x = width  / 2;
        mGame.y = height / 2;

        [self rotateToInterfaceOrientation:orientation animationTime:0];
        [self addChild:mGame];
    }

    return self;
}

- (void)dealloc
{
    [mGame release];
    [super dealloc];
}

- (UIInterfaceOrientation)initialInterfaceOrientation
{
    // In an iPhone app, the 'statusBarOrientation' has the correct value on Startup; 
    // unfortunately, that's not the case for an iPad app (for whatever reason). Thus, we read the
    // value from the app's plist file.

    NSDictionary *bundleInfo = [[NSBundle mainBundle] infoDictionary];
    NSString *initialOrientation = [bundleInfo objectForKey:@"UIInterfaceOrientation"];
    if (initialOrientation)
    {
        if ([initialOrientation isEqualToString:@"UIInterfaceOrientationPortrait"])
            return UIInterfaceOrientationPortrait;
        else if ([initialOrientation isEqualToString:@"UIInterfaceOrientationPortraitUpsideDown"])
            return UIInterfaceOrientationPortraitUpsideDown;
        else if ([initialOrientation isEqualToString:@"UIInterfaceOrientationLandscapeLeft"])
            return UIInterfaceOrientationLandscapeLeft;
        else
            return UIInterfaceOrientationLandscapeRight;
    }
    else 
    {
        return [[UIApplication sharedApplication] statusBarOrientation];
    }
}

- (void)rotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
                       animationTime:(double)animationTime
{
    float angles[] = {0.0f, 0.0f, -PI, PI_HALF, -PI_HALF};

    float oldAngle = mGame.rotation;
    float newAngle = angles[(int)interfaceOrientation];

    // make sure that rotation is always carried out via the minimal angle
    while (oldAngle - newAngle >  PI) newAngle += TWO_PI;
    while (oldAngle - newAngle < -PI) newAngle -= TWO_PI;

    // rotate game
    if (animationTime)
    {
        SPTween *tween = [SPTween tweenWithTarget:mGame time:animationTime
                                       transition:SP_TRANSITION_EASE_IN_OUT];
        [tween animateProperty:@"rotation" targetValue:newAngle];
        [[SPStage mainStage].juggler removeObjectsWithTarget:mGame];
        [[SPStage mainStage].juggler addObject:tween];
    }
    else 
    {
        mGame.rotation = newAngle;
    }

    // inform all display objects about the new game size
    BOOL isPortrait = UIInterfaceOrientationIsPortrait(interfaceOrientation);
    float newWidth  = isPortrait ? MIN(mGame.gameWidth, mGame.gameHeight) : 
                                   MAX(mGame.gameWidth, mGame.gameHeight);
    float newHeight = isPortrait ? MAX(mGame.gameWidth, mGame.gameHeight) :
                                   MIN(mGame.gameWidth, mGame.gameHeight);

    if (newWidth != mGame.gameWidth)
    {
        mGame.gameWidth  = newWidth;
        mGame.gameHeight = newHeight;

        SPEvent *resizeEvent = [[SPResizeEvent alloc] initWithType:SP_EVENT_TYPE_RESIZE
                                width:newWidth height:newHeight animationTime:animationTime];
        [mGame broadcastEvent:resizeEvent];
        [resizeEvent release];
    }
}

// Enable this method for the simplest possible universal app support: it will display a black
// border around the iPhone (640x960) game when it is started on the iPad (768x1024); no need to 
// modify any coordinates. 
/*
- (void)render:(SPRenderSupport *)support
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        glEnable(GL_SCISSOR_TEST);
        glScissor(64, 32, 640, 960);
        [super render:support];
        glDisable(GL_SCISSOR_TEST);
    }
    else 
        [super render:support];
}
*/

@end

Here is my Xcode project: http://cl.ly/2e3g02260N47


Solution

  • The tutorial was very old and was incompatible with the latest scaffold;

    I did this:

    - (id)init
    {
        if((self = [super init]))
        {
    

    when I should've done this:

    - (id)initWithWidth:(float)width height:(float)height
    {
        if ((self = [super initWithWidth:width height:height]))
        {
    

    thanks, though sergio!

    (There are much better sparrow tutorials and I'm even making my own video tutorials :P)