0

我对 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];
    }
} 

这是子类的标题:

@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"];
}

任何帮助表示赞赏。

4

1 回答 1

0

这是一个明显的问题:

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

在 C 中,逗号是运算符。编译器将此语句解释为此方法声明:

- (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"];
}

不过,这可能不是您的问题。

于 2012-12-28T21:21:52.350 回答