无法定位CCSprite子类的实例

时间:2012-12-28 20:56:01

标签: objective-c cocos2d-iphone ccsprite

我对Objective-C并不陌生,但我无法弄清楚这个问题。我正在尝试创建一个我所做的CCSprite子类的实例,但它总是在(0,0)创建实例,我无法移动它。我已经设置了我的代码,以便它解析我放置级别信息的.txt文件,然后根据该信息创建精灵。

这是初始化精灵的代码:

-(void)initLevel{

NSLog(@"Level %i is of length %i", lvlNum, [FileReader getLengthOfLevel:[FileReader getStartOfLevel:lvlNum atPath:lvlPack] atPath:lvlPack]);
CCSprite *spriteToMake;
int start = [FileReader getStartOfLevel:lvlNum atPath:lvlPack];
int length = [FileReader getLengthOfLevel:start atPath:lvlPack];

NSString *tmp = [FileReader getLineFromFile:lvlPack byIndex:start];
NSArray *tmpArray = [tmp componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSMutableArray *tmpArray2 = [tmpArray mutableCopy];
[tmpArray2 removeObject:@""];

requiredLinks = [(NSString*)[tmpArray2 objectAtIndex:2] intValue];

[tmpArray2 release];

for(int i = start + 1; i <= start + length; i++){
    NSString *line = [FileReader getLineFromFile:lvlPack byIndex:i];
    int x = 0;
    int y = 0;

    NSArray *temp = [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSMutableArray *temp2 = [temp mutableCopy];
    [temp2 removeObject:@""];

    //Determine the type of sprite
    if([(NSString*)[temp2 objectAtIndex:0] isEqualToString:@"Basic_Sprite"]){
        spriteToMake = [BasicLink sprite];
    }else if([(NSString*)[temp2 objectAtIndex:0] isEqualToString:@"Big_Sprite"]){
        spriteToMake = [BasicLink spriteWithFile:@"Big_Link.png"];
    }else{
        spriteToMake = nil;
    }

    //Create the sprite
    if(spriteToMake != nil){
        x = [(NSString*)[temp2 objectAtIndex:1] intValue];
        y = [(NSString*)[temp2 objectAtIndex:2] intValue];
        spriteToMake.position = ccp(x, y);
        NSLog(@"%@", spriteToMake);

        [self addChild:spriteToMake];
        [spriteToMake setUpdate];
    }else{
        NSLog(@"Sprite set to NULL");
    }

    NSLog(@"%i, %i, %i", x, y, [temp2 count]);

    [temp2 release];
    }
} 

这是子类'header:

@interface BasicLink : CCSprite{
CGPoint position;
CGPoint movement;
int explosionRadius, width, height;
CCScene *sceneIn;
}

@property (assign)CGPoint pos, movement;
@property (assign)int explosionRadius, width, height;
@property (assign)CCScene* sceneIn;

+(CCSprite*)sprite;
+(CCSprite*)spriteAtX:(int)x atY: (int)y;

-(void)die;
-(void)explode;
-(void)updateSprite;
-(CGRect)getBounds;
-(void)setUpdate;
-(void)move:(int)x, int(y);

@end

这是初始化精灵的子类的一部分:

@implementation BasicLink

@synthesize position, movement, explosionRadius, sceneIn, width, height;

+(CCSprite*)sprite{
    return [BasicLink spriteWithFile:@"Basic_Link.png"];
}

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

这是一个明显的问题:

-(void)move:(int)x, int(y);

在C中,comma is an operator。编译器将此语句解释为此方法声明:

- (void)move:(int)x

我不完全确定它在使用int(y)做了什么;它可能将其视为C函数声明。无论如何,它不是方法声明的一部分。

您的移动声明应如下所示:

- (void)moveToX:(int)x andY:(int)y;

另一个小错误是类方法的返回类型:

+(CCSprite*)sprite{
    return [BasicLink spriteWithFile:@"Basic_Link.png"];
}

应该是:

+ (BasicLink *)sprite{
    return [BasicLink spriteWithFile:@"Basic_Link.png"];
}

但这可能不是你的问题。