Objective C error: [BodyObject alloc]: unrecognized selector sent to instance
I have been trying to use some objective C, i have got as far as creating a couple of objects and im now trying to instantiate them. The code compiles with no warnings but when it runs i get this in the console:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[CCSprite copyWithZone:]: unrecognized selector sent to instance 0x6424ad0'
Im guessing there is just a problem with the code somewhere, im not calling a copyWithZone method anywhere
Here is the code for game object:
gameobject.h
#import "cocos2d.h"
#import "Box2D.h"
@interface GameObject : NSObject {
}
-(id) initWithSprite:(CCSprite*) sprite
andVelocity:(b2Vec2*开发者_Go百科) velocity;
@property (nonatomic, copy) CCSprite *Sprite;
@property (nonatomic) b2Vec2 *Velocity;
@end
gameobject.mm
#import "cocos2d.h"
#import "GameObject.h"
#import "Box2D.h"
@implementation GameObject
@synthesize Sprite;
@synthesize Velocity;
-(id) initWithSprite:(CCSprite*) sprite
andVelocity:(b2Vec2*) velocity
{
self = [super init];
self.Sprite = sprite;
self.Velocity = velocity;
return self;
}
@end
here is the code for BodyObject:
BodyObject.mm
#import "BodyObject.h"
@implementation BodyObject
@synthesize Body;
@synthesize Fixture;
-(id) initWithBody:(b2Body*) body
andFixture:(b2Fixture*) fixture
andVelocity:(b2Vec2*) velocity
andSprite:(CCSprite*) sprite
{
self = [super initWithSprite:(CCSprite*)sprite
andVelocity:(b2Vec2*)velocity];
self.Body=body;
self.Fixture=fixture;
return self;
}
@end
BodyObject.h
#import "GameObject.h"
#import "Box2D.h"
#import "cocos2d.h"
@interface BodyObject : GameObject {
}
@property (nonatomic) b2Body *Body;
@property (nonatomic) b2Fixture *Fixture;
-(id) initWithBody:(b2Body*) body
andFixture:(b2Fixture*) fixture
andVelocity:(b2Vec2*) velocity
andSprite:(CCSprite*) sprite;
@end
And where im trying to instantiate it
BodyObject *bodyObject = [[BodyObject alloc] initWithBody:(b2Body *)body
andFixture:(b2Fixture *)fixture
andVelocity:(b2Vec2*) vector
andSprite:(CCSprite*) sprite];
Can anyone point me as to where to start looking for the solution for these types of errors or see a problem with the code? I can post more of the classes if required.
Thanks,
This is wrong: self = [[super initWithSprite:(CCSprite*)sprite andVelocity:(b2Vec2*)velocity] alloc];
The right procedure is: self = [super initWithSprite:(CCSprite*)sprite andVelocity:(b2Vec2*)velocity];
If you've reached the point in your code where your custom init methods are being called, then your object has already been allocated, so remove those outer calls to +alloc
altogether.
(Also, unrelated, but the convention when naming selectors is not to include works like and, or, and but. Something like -initWithSprite:velocity:
would conform to the Cocoa naming conventions.)
The likely reason for this crash is the CCSprite does not conform to NSCopying. If you either implement NSCopying on CCSprite (providing a copyWithZone: method) or change your Sprite property to retain, this crash will stop.
As a side note, accepted Objective-C style frowns upon any variable starting with a capital letter.
精彩评论