我遇到了一个错误,我的游戏会轰炸我并给我以下信息:
由于未捕获的异常而终止应用 'NSInternalInconsistencyException',原因:'CCSprite没有使用 相同的纹理ID'
以下是源代码:
Hero.h
#import "cocos2d.h"
#import "ActionSprite.h"
#import "GameLayer.h"
#import "Goblin.h"
@class GameLayer, Waypoint, Goblin;
@interface Hero : ActionSprite {
Waypoint *destinationWaypoint;
Goblin *chosenGoblin;
NSMutableArray *attackedBy;
int maxHp;
}
@property (nonatomic, weak) GameLayer *theGame;
@property (nonatomic, strong) CCSprite *mySprite;
+(id) nodeWithTheGame: (GameLayer *)_game location: (CGPoint)location;
-(id) initWithTheGame: (GameLayer *)_game location: (CGPoint)location;
-(void) startMoving;
@end
Hero.m
#import "Hero.h"
#import "Waypoint.h"
#import "Goblin.h"
#define HEALTH_BAR_WIDTH 20
#define HEALTH_BAR_ORIGIN -10
@implementation Hero
@synthesize theGame;
@synthesize mySprite;
+(id) nodeWithTheGame: (GameLayer *)_game location: (CGPoint)location
{
return [[self alloc] initWithTheGame:_game location:location];
}
-(id) initWithTheGame:(GameLayer *)_game location:(CGPoint)location
{
//if ((self = [super initWithSpriteFrameName:@"TriElfWalk000.1@2x.png"])) {
if (self = [super init]) {
int i;
// Idle Animation
CCArray *idleFrames = [CCArray arrayWithCapacity: 11];
for (i = 0; i < 11; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat: @"TriElfWalk%03d.1@2x.png", i]];
[idleFrames addObject: frame];
}
CCAnimation *idleAnimation = [CCAnimation animationWithSpriteFrames: [idleFrames getNSArray] delay: 1.0/11.0];
self.idleAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation: idleAnimation]];
// Attack Animation
CCArray *attackFrames = [CCArray arrayWithCapacity: 11];
for (i = 0; i < 11; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"TriElfWalk%03d.1@2x.png", i]];
[attackFrames addObject: frame];
}
CCAnimation *attackAnimation = [CCAnimation animationWithSpriteFrames: [attackFrames getNSArray] delay: 1.0/11.0];
self.attackAction = [CCSequence actions:[CCAnimate actionWithAnimation: attackAnimation], [CCCallFunc actionWithTarget:self selector:@selector(idle)], nil];
// Walk Animation
CCArray *walkFrames = [CCArray arrayWithCapacity: 11];
for (i = 0; i < 11; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat: @"TriElfWalk%03d.1@2x.png", i]];
[walkFrames addObject: frame];
}
CCAnimation *walkAnimation = [CCAnimation animationWithSpriteFrames: [walkFrames getNSArray] delay: 1.0/11.0];
self.walkAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation: walkAnimation]];
// Death Animation
CCArray *deathFrames = [CCArray arrayWithCapacity:11];
for (int i = 0; i < 11; i++) {
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat: @"TriElfWalk%03d.1@2x.png", i]];
[deathFrames addObject: frame];
}
CCAnimation *deathAnimation = [CCAnimation animationWithSpriteFrames: [deathFrames getNSArray] delay: 1.0/12.0];
self.deathAction = [CCSequence actions: [CCAnimate actionWithAnimation: deathAnimation], [CCBlink actionWithDuration: 2.0 blinks: 10.0], nil];
self.centerToBottom = 24.5;
self.centerToSides = 21.5;
self.hitPoints = 100.0;
self.damage = 20.0;
//self.walkSpeed = 8;
self.cost = 100;
self.incomeProvided = 3;
// Create bounxing boxes
self.hitBox = [self createBoundingBoxWithOrigin:ccp(-self.centerToSides, -self.centerToBottom) size:CGSizeMake(self.centerToSides * 2, self.centerToBottom * 2)];
self.attackBox = [self createBoundingBoxWithOrigin:ccp(self.centerToSides, -10) size:CGSizeMake(10, 10)];
maxHp = self.hitPoints;
[self scheduleUpdate];
}
return self;
}
-(void) startMoving
{
//CCLOG(@"I SHOULD BE MOVING!");
self.desiredPosition = ccp(1050, 360);
[self runAction: [CCMoveTo actionWithDuration:50 position:self.desiredPosition]];
}
-(void) update: (ccTime)dt
{
CCLOG(@"FOOTMAN Y: %f", self.position.y);
for (Goblin *goblin in theGame.goblins) {
CCLOG(@"GOBLIN Y: %f", goblin.position.y);
}
}
-(void) getRemoved
{
for (Tower *attacker in attackedBy) {
[attacker targetKilled];
}
[self.parent removeChild: self cleanup: true];
[theGame.goblins removeObject: self];
// Notify the game that we killed an enemy so we can check if we can send another wave
[theGame goblinGotKilled];
}
-(void) draw
{
ccDrawSolidRect( ccp(self.position.x + HEALTH_BAR_ORIGIN, self.position.y + 16), ccp(self.position.x + HEALTH_BAR_ORIGIN + HEALTH_BAR_WIDTH, self.position.y + 14), ccc4f(1.0, 0, 0, 1.0));
ccDrawSolidRect( ccp(self.position.x + HEALTH_BAR_ORIGIN, self.position.y + 16), ccp(self.position.x + HEALTH_BAR_ORIGIN + (float)(self.currentHitPoints * HEALTH_BAR_WIDTH)/maxHp, self.position.y + 14), ccc4f(0, 1.0, 0, 1.0));
}
@end
我不完全确定这是命名我的精灵/精灵表的错误。当我删除+ nodeWithTheGame函数并将-initWithTheGame更改为init时,游戏运行时没有出现这个问题。
任何有关解决此问题的帮助(不按上述方式执行)将非常感谢!
编辑:这是第一个call throw stack:
*** First throw call stack:
(
0 CoreFoundation 0x039d85e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01fd38b6 objc_exception_throw + 44
2 CoreFoundation 0x039d8448 +[NSException raise:format:arguments:] + 136
3 Foundation 0x01732fee -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 KingdomOfRosak 0x00083457 -[CCSpriteBatchNode addChild:z:tag:] + 743
5 KingdomOfRosak 0x000732de -[CCNode addChild:] + 318
6 KingdomOfRosak 0x000110d6 -[GameLayer initHero] + 102
7 KingdomOfRosak 0x000123c9 -[GameLayer footsoldierPurchased:] + 505
8 libobjc.A.dylib 0x01fe581f -[NSObject performSelector:withObject:] + 70
9 KingdomOfRosak 0x000b5907 __83-[CCMenuItemImage initWithNormalImage:selectedImage:disabledImage:target:selector:]_block_invoke + 71
10 KingdomOfRosak 0x000b1fac -[CCMenuItem activate] + 108
11 KingdomOfRosak 0x000c62d9 -[CCMenu ccTouchEnded:withEvent:] + 297
12 libobjc.A.dylib 0x01fe5874 -[NSObject performSelector:withObject:withObject:] + 77
13 KingdomOfRosak 0x00054d4f -[CCTouchDispatcher touches:withEvent:withTouchType:] + 1487
14 KingdomOfRosak 0x000558e3 -[CCTouchDispatcher touchesEnded:withEvent:] + 115
15 KingdomOfRosak 0x0001c2be -[CCGLView touchesEnded:withEvent:] + 110
16 UIKit 0x0028051d -[UIWindow _sendTouchesForEvent:] + 852
17 UIKit 0x00281184 -[UIWindow sendEvent:] + 1232
18 UIKit 0x00254e86 -[UIApplication sendEvent:] + 242
19 UIKit 0x0023f18f _UIApplicationHandleEventQueue + 11421
20 CoreFoundation 0x0396183f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
21 CoreFoundation 0x039611cb __CFRunLoopDoSources0 + 235
22 CoreFoundation 0x0397e29e __CFRunLoopRun + 910
23 CoreFoundation 0x0397dac3 CFRunLoopRunSpecific + 467
24 CoreFoundation 0x0397d8db CFRunLoopRunInMode + 123
25 GraphicsServices 0x032f69e2 GSEventRunModal + 192
26 GraphicsServices 0x032f6809 GSEventRun + 104
27 UIKit 0x00241d3b UIApplicationMain + 1225
28 KingdomOfRosak 0x00091506 main + 134
29 KingdomOfRosak 0x00001eb5 start + 53
30 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
如果有人可以帮助确定问题的位置,我认为我没有接近解决问题。