SpriteKit:根据方向改变纹理

时间:2014-07-17 23:00:02

标签: ios sprite-kit

我正在使用屏幕上的DPAD来移动我的角色但是我在根据方向传递我的精灵时遇到了麻烦。基本上我需要的是如果角色向上移动,使用步行动画。向下移动,使用下来。左,使用左,右,使用右动画。角度是我真正遇到麻烦的。这是我用来移动角色的代码。

- (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];

2 个答案:

答案 0 :(得分:0)

//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”];

答案 1 :(得分:0)

我的动画有效,但我不知道如何根据角色的移动方向传递动画。我上下工作:

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

但我的问题在于角度,比如有人要在控制板上向上和向右按压。如果他们向右按下更多,那么我希望它传递我的移动右侧动画但是如果他们按下更多到DPAD的顶部我想传递向上移动动画。不确定如何做到这一点。