ios - How to store position in spritekit -
i need change character appearance during game. character define node:
skspritenode* _character; .... sktexture* tmp = [sktexture texturewithimagenamed:string]; tmp.filteringmode = sktexturefilteringnearest; [texturearray addobject:tmp]; _character = [skspritenode spritenodewithtexture:tmp]; then under condition in game, if check satisfied, change appearance by:
-(void)update:(cftimeinterval)currenttime { /* called before each frame rendered */ if (condition_satisfied) { ... _store.position = _character.position; [_character removefromparent]; ... //load new character texture here, won't post code, short. .... _character.position = _store.position; ... [self addchild:_character]; } i use store node store position, because character's position change during game human touch. in game, cause crash, collision... guess not right way restore right position of character. how should here ?
to change texture set skspritenode's texture property:
_character.texture = [sktexture texturewithimagenamed:@"image"]; or use skaction, if want add fading effect when texture changing:
skaction *fadeout = [skaction fadeoutbyduration:0.3f]; skaction *fadein = [skaction fadeinbyduration:0.3f]; skaction *changetexture = [sktexture settexture:[sktexture texturewithimagenamed:@"image"]]; [_character runaction:[skaction sequence:@[fadeout,changetexture,fadein]]]; there no need removefromparent node.
if need store node's position, it's better store in @property:
@interface gamescene() @property (nonatomic) cgpoint characterposition; @end @implementation gamescene -(void)update:(cftimeinterval)currenttime { /* called before each frame rendered */ if (condition_satisfied) { ... self.characterposition = _character.position; [_character removefromparent]; ... //load new character texture here, won't post code, short. .... _character.position = self.characterposition; ... [self addchild:_character]; } @end
Comments
Post a Comment