Objective c 与2.1相比,在3.x中从纹理创建CCSpriteFrame
在CoCoS2D2.1项目kobold2d中,我有一些代码,可以从精灵图纸中提取精灵框架的片段并创建动画。在3.2.1中,我很难得到类似的结果 2.1代码Objective c 与2.1相比,在3.x中从纹理创建CCSpriteFrame,objective-c,cocos2d-iphone,cocos2d-iphone-3,Objective C,Cocos2d Iphone,Cocos2d Iphone 3,在CoCoS2D2.1项目kobold2d中,我有一些代码,可以从精灵图纸中提取精灵框架的片段并创建动画。在3.2.1中,我很难得到类似的结果 2.1代码 //subclassed CCSprite //inits a LargeSquare Sprite from within a sprite sheet self = [super initWithSpriteFrameName:theBoxFrameName andNumberOfColors:numColors]; if (self)
//subclassed CCSprite
//inits a LargeSquare Sprite from within a sprite sheet
self = [super initWithSpriteFrameName:theBoxFrameName andNumberOfColors:numColors];
if (self)
{
CGRect currentRect = self.textureRect;
//defines a new rect for the sprite that represents a small piece of the LargeSquare
self.textureRect = CGRectMake(currentRect.origin.x+((xCoord/depth)*currentRect.size.width/andNumberAcrossForThisDepth),
currentRect.origin.y+(((andNumberAcrossForThisDepth-1)-(yCoord/depth))*currentRect.size.width/andNumberAcrossForThisDepth),
currentRect.size.width/andNumberAcrossForThisDepth,
currentRect.size.height/andNumberAcrossForThisDepth
);
//finds other LargeSquares in the sprite sheet and takes similar pieces
NSMutableArray * spritesForAnimation = [NSMutableArray array];
int counterer = andNumberAcrossForThisDepth*2;
while (counterer <=64) {
CCSprite * spritePlaceHolder;
CCSpriteFrame * spriteFrameForAnimation;
NSString * spriteForAnimationName = [NSString stringWithFormat:@"%@_%i.png",[levelDictionary objectForKey:@"imageFrameName"],(counterer)];
spritePlaceHolder = [CCSprite spriteWithSpriteFrameName:spriteForAnimationName];
CGRect spriteForAnimationRect = spritePlaceHolder.textureRect;
CGRect newRect = CGRectMake(spriteForAnimationRect.origin.x+((xCoord/depth)*spriteForAnimationRect.size.width/andNumberAcrossForThisDepth),
spriteForAnimationRect.origin.y+(((andNumberAcrossForThisDepth-1)-(yCoord/depth))*spriteForAnimationRect.size.width/andNumberAcrossForThisDepth),
spriteForAnimationRect.size.width/andNumberAcrossForThisDepth,
spriteForAnimationRect.size.height/andNumberAcrossForThisDepth
);
spriteFrameForAnimation = [CCSpriteFrame frameWithTexture:self.texture rect:newRect];
[spritesForAnimation addObject: spriteFrameForAnimation];
counterer = counterer * 2;
}
completionAnimation = [CCAnimation animationWithSpriteFrames:spritesForAnimation delay:0.2];
如果我在循环中这样做或类似的事情,它就是空白的。我想知道问题是不是原版尺寸?抵消
while (counterer <=64) {
CCSprite * spritePlaceHolder;
CCSpriteFrame * spriteFrameForAnimation;
NSString * spriteForAnimationName = [NSString stringWithFormat:@"%@_%i.png",[levelDictionary objectForKey:@"imageFrameName"],(counterer)];
spritePlaceHolder = [CCSprite spriteWithSpriteFrameName:spriteForAnimationName];
CGRect spriteForAnimationRect = spritePlaceHolder.textureRect;
CGRect newRect = CGRectMake(spriteForAnimationRect.origin.x+((xCoord/depth)*spriteForAnimationRect.size.width/andNumberAcrossForThisDepth),
spriteForAnimationRect.origin.y+(((andNumberAcrossForThisDepth-1)-(yCoord/depth))*spriteForAnimationRect.size.width/andNumberAcrossForThisDepth),
spriteForAnimationRect.size.width/andNumberAcrossForThisDepth,
spriteForAnimationRect.size.height/andNumberAcrossForThisDepth
);
// BEGIN NEW CODE
CGFloat texScale = spritePlaceHolder.texture.contentScale;
CGRect rectInPixels = CGRectMake(newRect.origin.x*texScale, newRect.origin.y*texScale, newRect.size.width*texScale, newRect.size.height*texScale);
spriteFrameForAnimation = [CCSpriteFrame frameWithTexture:spritePlaceHolder.texture
rectInPixels:rectInPixels
rotated:NO
offset:ccp(0,0)
originalSize:originalSize.size];
[spritesForAnimation addObject: spriteFrameForAnimation];
counterer = counterer * 2;
}
这将输出大量数据,下面是一个片段:
2014-09-26 16:21:03.790 artThiefSB4[33484:690960] newRect 806.000000,302.000000,100.000000,100.000000
2014-09-26 16:21:03.790 artThiefSB4[33484:690960] spritePlaceHolder.spriteFrame 806.000000,2.000000,400.000000,400.000000 1.000000
2014-09-26 16:21:03.790 artThiefSB4[33484:690960] rectInPixel 806.000000,302.000000,100.000000,100.000000 texScale 1.000000
2014-09-26 16:21:03.790 artThiefSB4[33484:690960] <CCSpriteFrame = 0x7fc4bb80 | Texture=(null), Rect = (806.00,302.00,100.00,100.00)> rotated:0 offset=(0.00,0.00) D 8
2014-09-26 16:21:03.791 artThiefSB4[33484:690960] newRect 404.000000,1106.000000,100.000000,100.000000
2014-09-26 16:21:03.791 artThiefSB4[33484:690960] spritePlaceHolder.spriteFrame 404.000000,806.000000,400.000000,400.000000 1.000000
2014-09-26 16:21:03.791 artThiefSB4[33484:690960] rectInPixel 404.000000,1106.000000,100.000000,100.000000 texScale 1.000000
2014-09-26 16:21:03.791 artThiefSB4[33484:690960] <CCSpriteFrame = 0x7fc4c030 | Texture=(null), Rect = (404.00,1106.00,100.00,100.00)> rotated:0 offset=(0.00,0.00) D 16
2014-09-26 16:21:03.791 artThiefSB4[33484:690960] newRect 404.000000,704.000000,100.000000,100.000000
2014-09-26 16:21:03.791 artThiefSB4[33484:690960] spritePlaceHolder.spriteFrame 404.000000,404.000000,400.000000,400.000000 1.000000
2014-09-26 16:21:03.791 artThiefSB4[33484:690960] rectInPixel 404.000000,704.000000,100.000000,100.000000 texScale 1.000000
2014-09-26 16:21:03.792 artThiefSB4[33484:690960] <CCSpriteFrame = 0x7fc4c420 | Texture=(null), Rect = (404.00,704.00,100.00,100.00)> rotated:0 offset=(0.00,0.00) D 32
2014-09-26 16:21:03.792 artThiefSB4[33484:690960] newRect 1208.000000,302.000000,100.000000,100.000000
2014-09-26 16:21:03.792 artThiefSB4[33484:690960] spritePlaceHolder.spriteFrame 1208.000000,2.000000,400.000000,400.000000 1.000000
2014-09-26 16:21:03.792 artThiefSB4[33484:690960] rectInPixel 1208.000000,302.000000,100.000000,100.000000 texScale 1.000000
2014-09-26 16:21:03.792 artThiefSB4[33484:690960] <CCSpriteFrame = 0x7fc4c750 | Texture=(null), Rect = (1208.00,302.00,100.00,100.00)> rotated:0 offset=(0.00,0.00) D 64
你有没有比较过newRect v2和rectInPixels v3,看看它们在什么地方以及如何不同,因为前者当然可能在某些方面有所不同;将添加一个编辑。问题还在于它是完全空白的,所以我不认为它只是一个偏移量。嗯,我记得v3中的纹理在某一点上是颠倒的,也许这是v3的一般情况,或者你的v3版本是颠倒的issue@LearnCocos2Dpvr现在从翻转的Y纹理加载。我必须从v2.1游戏中翻转所有纹理,以便在CCSpriteFrame上获得正确的裁剪。您可以使用TexturePacker执行此操作,但如果您有大量的TexturePacker,请查看此处:。
CCLOG(@"newRect %f,%f,%f,%f", newRect.origin.x, newRect.origin.y, newRect.size.width,newRect.size.height);
CCLOG(@"spritePlaceHolder.spriteFrame %f,%f,%f,%f %f", spritePlaceHolder.spriteFrame.rect.origin.x, spritePlaceHolder.spriteFrame.rect.origin.y, spritePlaceHolder.spriteFrame.rect.size.width,spritePlaceHolder.spriteFrame.rect.size.height,spritePlaceHolder.spriteFrame.texture.contentScale);
CCLOG(@"rectInPixel %f,%f,%f,%f texScale %f", rectInPixels.origin.x, rectInPixels.origin.y, rectInPixels.size.width,rectInPixels.size.height,texScale);
CCLOG(@"%@ D %i\n\n",spriteFrameForAnimation,counterer);
2014-09-26 16:21:03.790 artThiefSB4[33484:690960] newRect 806.000000,302.000000,100.000000,100.000000
2014-09-26 16:21:03.790 artThiefSB4[33484:690960] spritePlaceHolder.spriteFrame 806.000000,2.000000,400.000000,400.000000 1.000000
2014-09-26 16:21:03.790 artThiefSB4[33484:690960] rectInPixel 806.000000,302.000000,100.000000,100.000000 texScale 1.000000
2014-09-26 16:21:03.790 artThiefSB4[33484:690960] <CCSpriteFrame = 0x7fc4bb80 | Texture=(null), Rect = (806.00,302.00,100.00,100.00)> rotated:0 offset=(0.00,0.00) D 8
2014-09-26 16:21:03.791 artThiefSB4[33484:690960] newRect 404.000000,1106.000000,100.000000,100.000000
2014-09-26 16:21:03.791 artThiefSB4[33484:690960] spritePlaceHolder.spriteFrame 404.000000,806.000000,400.000000,400.000000 1.000000
2014-09-26 16:21:03.791 artThiefSB4[33484:690960] rectInPixel 404.000000,1106.000000,100.000000,100.000000 texScale 1.000000
2014-09-26 16:21:03.791 artThiefSB4[33484:690960] <CCSpriteFrame = 0x7fc4c030 | Texture=(null), Rect = (404.00,1106.00,100.00,100.00)> rotated:0 offset=(0.00,0.00) D 16
2014-09-26 16:21:03.791 artThiefSB4[33484:690960] newRect 404.000000,704.000000,100.000000,100.000000
2014-09-26 16:21:03.791 artThiefSB4[33484:690960] spritePlaceHolder.spriteFrame 404.000000,404.000000,400.000000,400.000000 1.000000
2014-09-26 16:21:03.791 artThiefSB4[33484:690960] rectInPixel 404.000000,704.000000,100.000000,100.000000 texScale 1.000000
2014-09-26 16:21:03.792 artThiefSB4[33484:690960] <CCSpriteFrame = 0x7fc4c420 | Texture=(null), Rect = (404.00,704.00,100.00,100.00)> rotated:0 offset=(0.00,0.00) D 32
2014-09-26 16:21:03.792 artThiefSB4[33484:690960] newRect 1208.000000,302.000000,100.000000,100.000000
2014-09-26 16:21:03.792 artThiefSB4[33484:690960] spritePlaceHolder.spriteFrame 1208.000000,2.000000,400.000000,400.000000 1.000000
2014-09-26 16:21:03.792 artThiefSB4[33484:690960] rectInPixel 1208.000000,302.000000,100.000000,100.000000 texScale 1.000000
2014-09-26 16:21:03.792 artThiefSB4[33484:690960] <CCSpriteFrame = 0x7fc4c750 | Texture=(null), Rect = (1208.00,302.00,100.00,100.00)> rotated:0 offset=(0.00,0.00) D 64