编辑 4:我在 CCSpriteFrameCache spriteFrameByName 方法中添加了一个断点,并且无法跨过以下步骤(我很清楚 spriteFrames 以某种方式从 CCSpriteFrameCache 中删除):
编辑 3:在投票结束问题之前,请使用Cocos2d 2.0 和附加的文件和 plist 文件尝试下面的代码(EDIT2) 。
编辑 2:我创建了一个全新的 GameScene 测试类,所以你们都可以尝试这个问题。一旦动画开始重复,它就会崩溃。我也更新了标题。
// GameScene2.h
//
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface GameScene2 : CCScene {
}
@end
// GameScene2.m
//
#import "GameScene2.h"
@implementation GameScene2
-(id)init {
self = [super init];
if (self != nil) {
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"artfile.plist"];
CCSpriteBatchNode* batchNode = [CCSpriteBatchNode batchNodeWithFile:@"artfile.png"];
[self addChild:batchNode];
CCSprite * test = [CCSprite spriteWithSpriteFrameName:@"frame0.png"];
test.anchorPoint = CGPointMake(0.5f, 0.5f);
test.position = CGPointMake(160.0f, 160.0f);
[batchNode addChild:test z:1];
NSMutableArray* frames = [[NSMutableArray alloc]initWithCapacity:2];
CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"frame0.png"];
[frames addObject:frame];
CCSpriteFrame* frame2 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"frame1.png"];
[frames addObject:frame2];
CCAnimation* anim = [CCAnimation animationWithSpriteFrames:frames delay:0.3f];
[[CCAnimationCache sharedAnimationCache] addAnimation:anim name:@"test"];
CCAnimate * animate = [CCAnimate actionWithAnimation:anim];
CCRepeatForever * repeat = [CCRepeatForever actionWithAction:animate];
//CCSequence * seq = [CCSequence actions:anim, nil];
[test runAction:repeat];
}
return self;
}
@end
artfile.png 如下所示:
plist文件是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>frames</key>
<dict>
<key>frame0.png</key>
<dict>
<key>aliases</key>
<array>
</array>
<key>spriteColorRect</key>
<string>{{3, 40}, {76, 28}}</string>
<key>spriteOffset</key>
<string>{0, 0}</string>
<key>spriteSize</key>
<string>{150, 94}</string>
<key>spriteSourceSize</key>
<string>{150, 94}</string>
<key>spriteTrimmed</key>
<false/>
<key>textureRect</key>
<string>{{0, 96}, {150, 94}}</string>
<key>textureRotated</key>
<false/>
</dict>
<key>frame1.png</key>
<dict>
<key>aliases</key>
<array>
</array>
<key>spriteColorRect</key>
<string>{{10, 33}, {98, 32}}</string>
<key>spriteOffset</key>
<string>{0, 0}</string>
<key>spriteSize</key>
<string>{150, 94}</string>
<key>spriteSourceSize</key>
<string>{150, 94}</string>
<key>spriteTrimmed</key>
<false/>
<key>textureRect</key>
<string>{{0, 192}, {150, 94}}</string>
<key>textureRotated</key>
<false/>
</dict>
<key>frame2.png</key>
<dict>
<key>aliases</key>
<array>
</array>
<key>spriteColorRect</key>
<string>{{4, 17}, {146, 48}}</string>
<key>spriteOffset</key>
<string>{0, 0}</string>
<key>spriteSize</key>
<string>{150, 94}</string>
<key>spriteSourceSize</key>
<string>{150, 94}</string>
<key>spriteTrimmed</key>
<false/>
<key>textureRect</key>
<string>{{0, 0}, {150, 94}}</string>
<key>textureRotated</key>
<false/>
</dict>
</dict>
<key>metadata</key>
<dict>
<key>version</key>
<string>1.5.2</string>
<key>format</key>
<integer>3</integer>
<key>size</key>
<string>{1024, 1024}</string>
<key>name</key>
<string>artfile</string>
<key>premultipliedAlpha</key>
<false/>
<key>target</key>
<dict>
<key>name</key>
<string>default</string>
<key>textureFileName</key>
<string>artfile_default</string>
<key>textureFileExtension</key>
<string>.png</string>
<key>coordinatesFileName</key>
<string>artfile_default</string>
<key>coordinatesFileExtension</key>
<string>.plist</string>
<key>premultipliedAlpha</key>
<false/>
</dict>
</dict>
</dict>
</plist>
EDIT2结束:
原始问题:
我得到:
-[CCSprite setTexture:], /Users/.../libs/cocos2d/CCSprite.m:934 中的断言失败
它肯定与batchNode有关,因为没有它它曾经可以工作......
我知道动画的精灵帧应该在同一个精灵表中,就是这样。我不知道如何解决这个问题,它必须与使用批处理节点有关。
//
// Navigator.h
@interface Navigator : CCLayer {
}
//Batch Nodes
@property(readwrite, nonatomic) CCSpriteBatchNode* batchNode;
// Navigator.m
@implementation Navigator
@synthesize batchNode;
+(id) scene {
CCScene *scene = [CCScene node];
Navigator *layer = [Navigator node];//??
[scene addChild:layer];
return scene;
}
-(void) loadStuff{
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"art1-hd.plist"];
batchNode = [CCSpriteBatchNode batchNodeWithFile:@"art1-hd.png"];
[self addChild:batchNode];
CCSprite * test = [CCSprite spriteWithSpriteFrameName:@"emptyCircle0.png"];
test.anchorPoint = CGPointMake(0.5f, 0.5f);
test.position = CGPointMake(160.0f, 160.0f);
[batchNode addChild:test z:1];
NSMutableArray* frames = [[NSMutableArray alloc]initWithCapacity:2];
CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"emptyCircle0.png"];
[frames addObject:frame];
CCSpriteFrame* frame2 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"emptyCircle1.png"];
[frames addObject:frame2];
CCAnimation* anim = [CCAnimation animationWithSpriteFrames:frames delay:0.3f];
//[[CCAnimationCache sharedAnimationCache] addAnimation:anim name:@"test"];
CCAnimate * animate = [CCAnimate actionWithAnimation:anim];
CCRepeatForever * repeat = [CCRepeatForever actionWithAction:animate];
//CCSequence * seq = [CCSequence actions:anim, nil];
[test runAction:repeat];
}
-(id)init
{
CCLOG(@"Navigator init");
if((self=[super init])){
CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
[self loadStuff];
}
return self;
}
编辑:我检查了 CCSprite setTexture 的源代码,这就是我所看到的。然而,控制台日志确实只说 NSAssert 而没有给出问题所在的日志消息。添加断点似乎使第一个 NSAssert 失败,但我不确定 100%,因为应用程序会立即转到此图像:
-(void) setTexture:(CCTexture2D*)texture
{
// If batchnode, then texture id should be the same
NSAssert( !batchNode_ || texture.name == batchNode_.texture.name , @"CCSprite: Batched sprites should use the same texture as the batchnode");
// accept texture==nil as argument
NSAssert( !texture || [texture isKindOfClass:[CCTexture2D class]], @"setTexture expects a CCTexture2D. Invalid argument");
if( ! batchNode_ && texture_ != texture ) {
[texture_ release];
texture_ = [texture retain];
[self updateBlendFunc];
}
}