change these lines to make it compatible with retina screen and rotate back into correct position
region->rotation = -90;
region->width = size.width/CC_CONTENT_SCALE_FACTOR();
region->height = size.height/CC_CONTENT_SCALE_FACTOR();
This is my final code.
-(RegionAttachment*) regionWithTexture2d:(CCTexture2D*)tex2d name:(NSString*)_name{
CCTextureAtlas *tex = [[CCTextureAtlas alloc] initWithTexture:tex2d capacity:1];
AtlasPage *page = AtlasPage_create([_name UTF8String]);
page->rendererObject = tex;
page->width = tex2d.contentSizeInPixels.width;
page->height = tex2d.contentSizeInPixels.height;
AtlasRegion *reg = AtlasRegion_create();
reg->page = page;
RegionAttachment *region = RegionAttachment_create([_name UTF8String]);
region->rendererObject = reg;
CGSize size = tex2d.contentSizeInPixels;
float u = 0;
float u2 = 1;
float v = 0;
float v2 = 1;
RegionAttachment_setUVs(region,u,v,u2,v2,0);
region->regionOffsetX = 0; // zero if not doing whitespace stripping
region->regionOffsetY = 0;
region->regionWidth = size.width;
region->regionHeight = size.height;
region->regionOriginalWidth = region->regionWidth; // same if not doing whitespace stripping
region->regionOriginalHeight = region->regionHeight;
region->x = 0;
region->y = 0;
region->scaleX = 1/CC_CONTENT_SCALE_FACTOR();
region->scaleY = 1/CC_CONTENT_SCALE_FACTOR();
region->rotation = -90;
region->width = size.width;
region->height = size.height;
spRegionAttachment_updateOffset(region);
return region;
}
these are my CCSkeletonAnimation.h and CCSkeletonAnimation.m
CCSkeletonAnimation.h
/******************************************************************************
* Spine Runtime Software License - Version 1.1
*
* Copyright (c) 2013, Esoteric Software
* All rights reserved.
*
* Redistribution and use in source and binary forms in whole or in part, with
* or without modification, are permitted provided that the following conditions
* are met:
*
* 1. A Spine Essential, Professional, Enterprise, or Education License must
* be purchased from Esoteric Software and the license must remain valid:
* http://esotericsoftware.com/
* 2. Redistributions of source code must retain this license, which is the
* above copyright notice, this declaration of conditions and the following
* disclaimer.
* 3. Redistributions in binary form must reproduce this license, which is the
* above copyright notice, this declaration of conditions and the following
* disclaimer, in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#define SPINE_SHORT_NAMES
#import <spine/spine.h>
#import <spine/CCSkeleton.h>
#import "cocos2d.h"
@class CCSkeletonAnimation;
@protocol CCSkeletonAnimationDelegate <NSObject>
@optional
- (void) animationDidStart:(CCSkeletonAnimation*)animation track:(int)trackIndex;
- (void) animationWillEnd:(CCSkeletonAnimation*)animation track:(int)trackIndex;
- (void) animationDidTriggerEvent:(CCSkeletonAnimation*)animation track:(int)trackIndex event:(Event*)event;
- (void) animationDidComplete:(CCSkeletonAnimation*)animation track:(int)trackIndex loopCount:(int)loopCount;
@end
/** Draws an animated skeleton, providing an AnimationState for applying one or more animations and queuing animations to be
* played later. */
@interface CCSkeletonAnimation : CCSkeleton {
AnimationState* _state;
bool _ownsAnimationStateData;
id<CCSkeletonAnimationDelegate> _delegate;
bool _delegateStart, _delegateEnd, _delegateEvent, _delegateComplete;
}
+ (id) skeletonWithData:(SkeletonData*)skeletonData ownsSkeletonData:(bool)ownsSkeletonData;
+ (id) skeletonWithFile:(NSString*)skeletonDataFile atlas:(Atlas*)atlas scale:(float)scale;
+ (id) skeletonWithFile:(NSString*)skeletonDataFile atlasFile:(NSString*)atlasFile scale:(float)scale;
- (id) initWithData:(SkeletonData*)skeletonData ownsSkeletonData:(bool)ownsSkeletonData;
- (id) initWithFile:(NSString*)skeletonDataFile atlas:(Atlas*)atlas scale:(float)scale;
- (id) initWithFile:(NSString*)skeletonDataFile atlasFile:(NSString*)atlasFile scale:(float)scale;
- (void) setAnimationStateData:(AnimationStateData*)stateData;
- (void) setMixFrom:(NSString*)fromAnimation to:(NSString*)toAnimation duration:(float)duration;
- (void) setDelegate:(id<CCSkeletonAnimationDelegate>)delegate;
- (TrackEntry*) setAnimationForTrack:(int)trackIndex name:(NSString*)name loop:(bool)loop;
- (TrackEntry*) addAnimationForTrack:(int)trackIndex name:(NSString*)name loop:(bool)loop afterDelay:(int)delay;
- (TrackEntry*) getCurrentForTrack:(int)trackIndex;
- (void) clearTracks;
- (void) clearTrack:(int)trackIndex;
- (void) onAnimationStateEvent:(int)trackIndex type:(EventType)type event:(Event*)event loopCount:(int)loopCount;
- (bool) setCCTexture2D:(CCTexture2D*)texture intoSlot:(NSString*)slotName;
- (bool) setCCSprite:(CCSprite*)sprite intoSlot:(NSString*)slotName;
@property (nonatomic, readonly) AnimationState* state;
@end
#define AnimationStateToString(TYPE) [NSString stringWithFormat:@"%s",AnimationState_getCurrent(TYPE.state,trackIndex)->animation->name]//animation to NSString
CCSkeletonAnimation.m
/******************************************************************************
* Spine Runtime Software License - Version 1.1
*
* Copyright (c) 2013, Esoteric Software
* All rights reserved.
*
* Redistribution and use in source and binary forms in whole or in part, with
* or without modification, are permitted provided that the following conditions
* are met:
*
* 1. A Spine Essential, Professional, Enterprise, or Education License must
* be purchased from Esoteric Software and the license must remain valid:
* http://esotericsoftware.com/
* 2. Redistributions of source code must retain this license, which is the
* above copyright notice, this declaration of conditions and the following
* disclaimer.
* 3. Redistributions in binary form must reproduce this license, which is the
* above copyright notice, this declaration of conditions and the following
* disclaimer, in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#import <spine/CCSkeletonAnimation.h>
#import <spine/spine-cocos2d-iphone.h>
static void callback (AnimationState* state, int trackIndex, EventType type, Event* event, int loopCount) {
[(CCSkeletonAnimation*)state->context onAnimationStateEvent:trackIndex type:type event:event loopCount:loopCount];
}
@interface CCSkeletonAnimation (Private)
- (void) initialize;
- (RegionAttachment*) regionWithTexture2d:(CCTexture2D*)tex2d name:(NSString*)_name;
@end
@implementation CCSkeletonAnimation
@synthesize state = _state;
+ (id) skeletonWithData:(SkeletonData*)skeletonData ownsSkeletonData:(bool)ownsSkeletonData {
return [[[self alloc] initWithData:skeletonData ownsSkeletonData:ownsSkeletonData] autorelease];
}
+ (id) skeletonWithFile:(NSString*)skeletonDataFile atlas:(Atlas*)atlas scale:(float)scale {
return [[[self alloc] initWithFile:skeletonDataFile atlas:atlas scale:scale] autorelease];
}
+ (id) skeletonWithFile:(NSString*)skeletonDataFile atlasFile:(NSString*)atlasFile scale:(float)scale {
return [[[self alloc] initWithFile:skeletonDataFile atlasFile:atlasFile scale:scale] autorelease];
}
- (void) initialize {
_ownsAnimationStateData = true;
_state = AnimationState_create(AnimationStateData_create(_skeleton->data));
_state->context = self;
_state->listener = callback;
}
- (id) initWithData:(SkeletonData*)skeletonData ownsSkeletonData:(bool)ownsSkeletonData {
self = [super initWithData:skeletonData ownsSkeletonData:ownsSkeletonData];
if (!self) return nil;
[self initialize];
return self;
}
- (id) initWithFile:(NSString*)skeletonDataFile atlas:(Atlas*)atlas scale:(float)scale {
self = [super initWithFile:skeletonDataFile atlas:atlas scale:scale];
if (!self) return nil;
[self initialize];
return self;
}
- (id) initWithFile:(NSString*)skeletonDataFile atlasFile:(NSString*)atlasFile scale:(float)scale {
self = [super initWithFile:skeletonDataFile atlasFile:atlasFile scale:scale];
if (!self) return nil;
[self initialize];
return self;
}
- (void) dealloc {
if (_ownsAnimationStateData) AnimationStateData_dispose(_state->data);
AnimationState_dispose(_state);
[super dealloc];
}
- (void) update:(ccTime)deltaTime {
[super update:deltaTime];
deltaTime *= _timeScale;
AnimationState_update(_state, deltaTime);
AnimationState_apply(_state, _skeleton);
Skeleton_updateWorldTransform(_skeleton);
}
- (void) setAnimationStateData:(AnimationStateData*)stateData {
NSAssert(stateData, @"stateData cannot be null.");
if (_ownsAnimationStateData) AnimationStateData_dispose(_state->data);
AnimationState_dispose(_state);
_ownsAnimationStateData = false;
_state = AnimationState_create(stateData);
_state->context = self;
_state->listener = callback;
}
- (void) setMixFrom:(NSString*)fromAnimation to:(NSString*)toAnimation duration:(float)duration {
AnimationStateData_setMixByName(_state->data, [fromAnimation UTF8String], [toAnimation UTF8String], duration);
}
- (void) setDelegate:(id<CCSkeletonAnimationDelegate>)delegate {
_delegate = delegate;
_delegateStart = [delegate respondsToSelector:@selector(animationDidStart:track:)];
_delegateEnd = [delegate respondsToSelector:@selector(animationWillEnd:track:)];
_delegateEvent = [delegate respondsToSelector:@selector(animationDidTriggerEvent:track:event:)];
_delegateComplete = [delegate respondsToSelector:@selector(animationDidComplete:track:loopCount:)];
}
- (TrackEntry*) setAnimationForTrack:(int)trackIndex name:(NSString*)name loop:(bool)loop {
Animation* animation = SkeletonData_findAnimation(_skeleton->data, [name UTF8String]);
if (!animation) {
CCLOG(@"Spine: Animation not found: %@", name);
return 0;
}
return AnimationState_setAnimation(_state, trackIndex, animation, loop);
}
- (TrackEntry*) addAnimationForTrack:(int)trackIndex name:(NSString*)name loop:(bool)loop afterDelay:(int)delay {
Animation* animation = SkeletonData_findAnimation(_skeleton->data, [name UTF8String]);
if (!animation) {
CCLOG(@"Spine: Animation not found: %@", name);
return 0;
}
return AnimationState_addAnimation(_state, trackIndex, animation, loop, delay);
}
- (TrackEntry*) getCurrentForTrack:(int)trackIndex {
return AnimationState_getCurrent(_state, trackIndex);
}
- (void) clearTracks {
AnimationState_clearTracks(_state);
}
- (void) clearTrack:(int)trackIndex {
AnimationState_clearTrack(_state, trackIndex);
}
- (void) onAnimationStateEvent:(int)trackIndex type:(EventType)type event:(Event*)event loopCount:(int)loopCount {
if (!_delegate) return;
switch (type) {
case ANIMATION_START:
if (_delegateStart) [_delegate animationDidStart:self track:trackIndex];
break;
case ANIMATION_END:
if (_delegateEnd) [_delegate animationWillEnd:self track:trackIndex];
break;
case ANIMATION_COMPLETE:
if (_delegateComplete) [_delegate animationDidComplete:self track:trackIndex loopCount:loopCount];
break;
case ANIMATION_EVENT:
if (_delegateEvent) [_delegate animationDidTriggerEvent:self track:trackIndex event:event];
break;
}
}
- (bool) setCCTexture2D:(CCTexture2D*)texture intoSlot:(NSString*)slotName{
RegionAttachment *reg = [self regionWithTexture2d:texture
name:slotName];
Slot *slot = [self findSlot:slotName];
if ((slot!=nil)&&(reg!=nil)) {
Slot_setAttachment(slot,(Attachment*)reg);
return true;
}else{
return false;
}
return false;
}
- (bool) setCCSprite:(CCSprite*)sprite intoSlot:(NSString*)slotName{
//render for get all CCSprite setting
sprite.position = ccp(sprite.contentSize.width,sprite.contentSize.height);
sprite.anchorPoint = ccp(0,0); //set sprite to origin
sprite.rotation = 180; //the rendered texture will flip
sprite.flipX = YES; // flip back
CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:sprite.contentSize.width
height:sprite.contentSize.height];
[rt beginWithClear:0 g:0 b:0 a:0]; //start with clear
[sprite visit]; //visit for draw
[rt end];//end
return [self setCCTexture2D:rt.sprite.texture intoSlot:slotName]; //return
}
-(RegionAttachment*) regionWithTexture2d:(CCTexture2D*)tex2d name:(NSString*)_name{
CCTextureAtlas *tex = [[CCTextureAtlas alloc] initWithTexture:tex2d capacity:1];
AtlasPage *page = AtlasPage_create([_name UTF8String]);
page->rendererObject = tex;
page->width = tex2d.contentSizeInPixels.width;
page->height = tex2d.contentSizeInPixels.height;
AtlasRegion *reg = AtlasRegion_create();
reg->page = page;
RegionAttachment *region = RegionAttachment_create([_name UTF8String]);
region->rendererObject = reg;
CGSize size = tex2d.contentSizeInPixels;
float u = 0;
float u2 = 1;
float v = 0;
float v2 = 1;
RegionAttachment_setUVs(region,u,v,u2,v2,0);
Slot *slot = [self findSlot:_name];
RegionAttachment* attachment = (RegionAttachment*)slot->attachment;
region->regionOffsetX = 0;
region->regionOffsetY = 0;
region->regionWidth = size.width;
region->regionHeight = size.height;
region->regionOriginalWidth = region->regionWidth; // same if not doing whitespace stripping
region->regionOriginalHeight = region->regionHeight;
region->x = attachment->x;
region->y = attachment->y;
region->scaleX = self.nodeScale;
region->scaleY = self.nodeScale;
region->rotation = attachment->rotation;
region->width = size.width;
region->height = size.height;
spRegionAttachment_updateOffset(region);
return region;
}
@end
now we can change the part on-fly by this
CGSize wins = [[CCDirector sharedDirector]winSize];
Atlas *spineBoyAtlas = Atlas_readAtlasFile([@"spineboy.atlas" UTF8String]);
CCSkeletonAnimation *spineNode = [CCSkeletonAnimation skeletonWithFile:@"spineboy.json" atlas:spineBoyAtlas scale:1.0f];
[spineNode setMixFrom:@"walk" to:@"jump" duration:0.2f];
[spineNode setMixFrom:@"jump" to:@"walk" duration:0.4f];
[spineNode setDelegate:self];
[spineNode addAnimationForTrack:0 name:@"walk" loop:YES afterDelay:0];
[spineNode addAnimationForTrack:0 name:@"jump" loop:YES afterDelay:4];
[spineNode setPosition:ccp(wins.width*0.5f, wins.height*0.25f)];
[self addChild:spineNode z:1 tag:100];
//change
CCSprite *sprite = [CCSprite spriteWithFile:@"Icon.png"];
//can change color or other CCSprite properties
sprite.color = ccc3(100, 200, 300);
[spineNode setCCSprite:sprite intoSlot:@"neck"];
Thank you.