SpriteKit: How to implement touchesBegan and touchesEnded to split action into two parts?

StackOverflow https://stackoverflow.com/questions/23365030

  •  11-07-2023
  •  | 
  •  

Question

I'm trying to do a little archery game and at the moment I have a basic touchesBegan method that uses SKActions to animate an archer and shoot an arrow.

I'm having a hard time getting it so that I hold touch to draw the bow, then when I release, the rest of the animation plays out and the arrow shoots.

I tried it with using two separate NSMutableArray Atlases spread across touchesBegan and touchesEnded methods with userInteractionEnabled flags but that was a bit of a no-go...

Ultimately I want the duration of the hold to dictate a value for applyImpulse:CGVectorMake.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKNode *archerNode = [self childNodeWithName:@"archerNode"];

if (archerNode != nil)
 {
    SKAction *draw = [SKAction animateWithTextures:self.archerDraw timePerFrame:0.075];
    [archerNode runAction:draw];

    SKAction *shootArrow = [SKAction runBlock:^{

        SKNode *arrowNode = [self createArrowNode];

        [self addChild:arrowNode];
        [arrowNode.physicsBody applyImpulse:CGVectorMake(20.0, 0)];
    }];

    SKAction *sequence = [SKAction sequence:@[draw, shootArrow]];

    [archerNode runAction:sequence];
   }
  }
Was it helpful?

Solution

Initiate the charging animation in the touchesBegan: section & move the arrow release code in the touchesEnded: (+ touchesCancelled:) function. Keeping the time at which the touches began in an instance variable will help you calculate the impulse vector.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top