0

I'm using an on-screen DPAD to move my character but I'm having trouble with passing in my sprites based on direction. Basically what I need is if the character is moving up, use the walking up animation. Moving down, use down. Left, use left, and right, use right animations. The angles are what I'm really having trouble with. This is the code that I'm using to move my character.

- (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];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Mythrid12
  • 33
  • 4

2 Answers2

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”];
dragoneye
  • 703
  • 6
  • 14
0

I've got the animations working but I'm not sure how to pass them in according to which direction the character is moving. I have up and down working with this:

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

but my issue comes in with the angles, like if someone were to press between up and right on the control pad. If they press more to the right then up I'd like it to pass in my moving right animation but if they press more to the top of the DPAD I want to pass in the move up animation. Not sure exactly how to do this.

Mythrid12
  • 33
  • 4