SpriteKit:根据方向改变纹理

我正在使用屏幕上的DPAD来移动我的angular色,但是我在根据方向传递我的小精灵时遇到了麻烦。 基本上我需要的是如果angular色正在向上移动,使用向上走animation。 向下移动,放下。 左边,左边和右边,使用正确的animation。 angular度是我真正遇到的麻烦。 这是我用来移动我的angular色的代码。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInNode:self]; SKNode *node = [self nodeAtPoint:location]; //if control pad touched if ([node.name isEqualToString:@"controlPadNode"]) { touchX = location.x +100; //adjust for anchor touchY = location.y +43; controlButtonDown = YES; 

}

 -(void)update:(CFTimeInterval)currentTime { if (controlButtonDown == YES) { //the node I want to move SKNode *character = (CSCharacter*)node; //compute the angle between parameters pad and the horizontal float angle = atan2f (touchY - controlPadY, touchX - controlPadX) ; //move the character SKAction *moveCharacter = [SKAction moveByX: 1*cosf(angle) y:1*sinf(angle) duration:0.005]; [character runAction: moveCharacter]; 

 //add h and m file in your project #import <SpriteKit/SpriteKit.h> //spriteSequence @interface spriteSheet : SKSpriteNode { //private variable no one } @property SKTexture *startingFrame; @property SKSpriteNode *animatedsprite; @property SKAction *Animation; @property SKAction *currentAction; @property NSMutableArray *spritesArray; @property NSMutableArray *frameRateArray; @property NSMutableArray *actionKeys; @property NSString *previousAction; //public mehods -(id) initSpritesheet:(NSString*)firstFrame; -(void)addAnimation:(NSMutableArray*)frames frameRate:(float)time withKey:(NSString*)Key; -(void)gotoAndPlay:(int)animationindex label:(NSString*)framelabel; -(void)removeThisAction:(int)index; //for ground hero @property float totalWidth; //for zipline @property float Hspeed; // @end #import "spriteSheet.h" @implementation spriteSheet -(id) initSpritesheet:(SKTexture*)firstFrame{ self=[super init]; if(self) { //passing rect to rectangle _startingFrame=firstFrame; [self addRefernce]; } return self; } -(void) addRefernce { //adding a reference _animatedsprite=[SKSpriteNode spriteNodeWithTexture:_startingFrame]; _animatedsprite.anchorPoint=CGPointMake(0.5, 0.5); [self addChild:_animatedsprite]; _spritesArray=[[NSMutableArray alloc] init]; _frameRateArray=[[NSMutableArray alloc] init]; _actionKeys=[[NSMutableArray alloc] init]; } // -(void)addAnimation:(NSMutableArray*)frames frameRate:(float)time withKey:(NSString*)Key { [_spritesArray addObject:frames]; [_frameRateArray addObject:[NSNumber numberWithFloat:time]]; [_actionKeys addObject:Key]; } -(void)gotoAndPlay:(int)animationindex label:(NSString*)framelabel; { // [self removeAllActions]; NSMutableArray *frames=_spritesArray[animationindex]; float time=[_frameRateArray[animationindex] floatValue]; NSString *key=_actionKeys[animationindex]; _Animation = [SKAction animateWithTextures:frames timePerFrame:time resize:TRUE restore:TRUE]; if([framelabel isEqualToString:@"loop"]) { _currentAction = [SKAction repeatActionForever:_Animation ]; } else if([framelabel isEqualToString:@"playonce"]) { _currentAction=_Animation; } [_animatedsprite runAction:_currentAction withKey:key]; } -(void)removeThisAction:(int)index { [_animatedsprite removeActionForKey:_actionKeys[index]]; } @end adding all animation NSString *frame =gameViewController.commanDataHolder.heroRunning[0];//add first frame _gameHero=[[herospriteSheet alloc] initSpritesheet:frame]; //adding animation [_gameHero addAnimation:gameViewController.commanDataHolder.heroRunning frameRate:1 withKey:@“up”]; //index 0 [_gameHero addAnimation:gameViewController.commanDataHolder.heroJumpUp frameRate:6 withKey:@“down]; //index 1 [_gameHero addAnimation:gameViewController.commanDataHolder.heroFallDown frameRate:1 withKey:@“left”];//index 2 [_gameHero addAnimation:gameViewController.commanDataHolder.heroJumpDown frameRate:0.5 withKey:@“right”];//index 3 [self addObject:_gameHero]; at any action degree of rotation or swipe etc //play first animation in loop [_gameHero gotoAndPlay:0 label:@"loop"]; //play first animation in loop [_gameHero gotoAndPlay:0 label:@“playonce”]; at any action degree of rotation or swipe etc //play second animation in loop [_gameHero gotoAndPlay:1 label:@"loop"]; //play first animation in loop [_gameHero gotoAndPlay:1 label:@“playonce”]; 

我有animation工作,但我不知道如何通过他们根据angular色正在移动的方向。 我有这个工作:

  if (touchY > 1 ) { [leader runWalkBackTextures]; //character moving up animations }else{ [leader runWalkFrontTextures]; //character moving down animations } 

但是我的问题来自于angular度,就像有人在控制板上向上和向右一样。 如果他们向右按,然后向上,我希望它传递给我右移的animation,但是如果他们按下更多的DPAD的顶部,我想通过向上移动animation。 不确定如何做到这一点。