1

我运行这段代码:

- (void)unitButtonButtonTapped:(id)sender {
    [_label setString:@"Last button: Unembossed square"];
    MilitaryUnits *target = nil;
    target = [Peasants militaryUnits];
    target.position = ccp(100, 450);
    [self addChild:target];
}

我收到此错误: *由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“参数必须为非零”

这些是我的 .h 和 .m 类文件

#import "cocos2d.h"

@interface MilitaryUnits : CCSprite {
    int _experience;
    int _number_of_units;
    int _stamina;
    int _armor_level;
    int _weapon_levell;
}

@property (nonatomic, assign) int experience;
@property (nonatomic, assign) int number_of_units;
@property (nonatomic, assign) int stamina;
@property (nonatomic, assign) int armor_level;
@property (nonatomic, assign) int weapon_levell;

@end

@interface Peasants : MilitaryUnits{

}
+(id)militaryUnits;

@end

#import "MilitaryUnits.h"

@implementation MilitaryUnits

@synthesize number_of_units = _number_of_units;
@synthesize stamina = _stamina;
@synthesize experience = _experience;
@synthesize armor_level = _armor_level;
@synthesize weapon_levell = _weapon_levell;

@end

@implementation Peasants

+ (id)militaryUnits {

    Peasants *militaryUnits = nil;
    if ((militaryUnits = [[[super alloc] initWithFile:@"Target.png"] autorelease])) {
    }
    return militaryUnits;

}

@end

注意,我使用的是 cocos 2d

4

1 回答 1

2

在我看来你的精灵是零,即找不到文件“Target.png”。确保文件名与您在代码中拼写的大小写相同(在查找器中),并且该文件包含在 Xcode 中目标的成员资格中。

+ (id)militaryUnits {

    Peasants *militaryUnits;
    if ((militaryUnits = [[[super alloc] initWithFile:@"Target.png"] autorelease])) {
        return militaryUnis;
    } else {
        CCLOGERROR(@"your favorite whine style for errors like file not found");
        return nil;
    }  
}
于 2012-07-02T07:21:05.450 回答