In a cocos2d 2.1 project (kobold2d), I had some code that takes pieces of a spriteframes out of sprite sheets and creates an animation. i'm having trouble getting a similar result in 3.2.1
2.1 code
//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];
in 3.2.1 i'm having trouble getting the animation to work; i've played with everything i can think of. if i do this within the while loop rather then init a new spriteframe, i get the full LargeSprite rather than a small piece:
spriteFrameForAnimation = spritePlaceHolder.spriteFrame;
and if i do this within the loop or something similar, it just is blank. i'm wondering if the issue is original Size? offset?
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;
}
in answer to comment, i've compared several rects:
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);
which'll output a lot of data, here's a snippet:
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