user2288946
user2288946

Reputation: 19

I'm having issues with touchesBegan updating a CGVector value

I have made a CGVector which makes an upward impulse (simulating a jump) of the player ball. If I run the game with what I currently have (below), it will start fine, and the jump will happen perfectly as soon as the game starts. I am trying to make the ball jump every time a touch is recorded but I'm having issues calling the right method / making it work at all.

This is my code:

#import "MyScene.h"

@interface MyScene ()

@property (nonatomic) SKSpriteNode *paddle;


@end

int jump = 0;


@implementation MyScene

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{


NSLog(@"Touch Detected!");

// create the vector
//    myVector = CGVectorMake(0, 15);
//    [ball.physicsBody applyImpulse:myVector];




}

- (void)addBall:(CGSize)size {
SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];

// create a new sprite node from an image

// create a CGPoint for position
CGPoint myPoint = CGPointMake(size.width/2,0);
ball.position = myPoint;

// add a physics body
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/2];

ball.physicsBody.restitution = 0;

// add the sprite node to the scene
[self addChild:ball];




// create the vector
CGVector myVector = CGVectorMake(0, 15);

// apply the vector
[ball.physicsBody applyImpulse:myVector];


}



-(id)initWithSize:(CGSize)size {    
if (self = [super initWithSize:size]) {
    /* Setup your scene here */
    self.backgroundColor = [SKColor whiteColor];

    // add a physics body to the scene
    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

    // change gravity settings of the physics world
    self.physicsWorld.gravity = CGVectorMake(0, -8);

    [self addBall:size];
//        [self addPlayer:size];


}
return self;
}


-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}

@end

You can see I have commented out the myVector = CGVectorMake(0, 15); and the [ball.physicsBody applyImpulse:myVector]; in touchesBegan because It can't find ball and i'm not even sure it's right at all. Could anyone help me implement this jump using the impulse? Thanks!

Upvotes: 0

Views: 506

Answers (1)

ZeMoon
ZeMoon

Reputation: 20274

You are on the right track, but multiple impulses of the vector (0,15), will cause the ball to go very high. You need to reset the ball's velocity to zero before applying any subsequent impulse.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    NSLog(@"Touch Detected!");

    // create the vector
    ball.physicsBody.velocity = CGVectorMake(0,0);

    myVector = CGVectorMake(0, 15);
    [ball.physicsBody applyImpulse:myVector];

}

Also, you need to declare the ball as a global variable like so:

@implementation MyScene
{
    SKSpriteNode *ball;
}

And then in the addBall: method, instead of

SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];

just write

ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];

Upvotes: 1

Related Questions