When scene init I set world gravity -2.0f. And I added object on scene and joystick node, which I control this object. When I touch joystick node, world gravity set to 0, and when end touch joystick, world gravity set to -2.0f.
I can`t understand, whats wrong I do. Because object some times have slow speed moving.
For more details you can see video / video2 sample.
Thanks for answer. P.S. Sorry my English. I hope you understand. ;)
Move method:
- (void)moveJoystick {
if (_joystickController.isTracking) {
self.physicsWorld.gravity = CGVectorMake(.0f, 0.0f);
_shooter.position = CGPointMake(_shooter.position.x + .3f * _joystickController.velocity.x,
_shooter.position.y + .3f * _joystickController.velocity.y);
// if (_joystickController.velocity.x > 0) {
// [_shooter changeShooterSpriteSide:NO];
// } else {
// [_shooter changeShooterSpriteSide:YES];
// }
NSLog(@"_shooter.position %@", NSStringFromCGPoint(_shooter.position));
} else {
self.physicsWorld.gravity = CGVectorMake(.0f, -2.0f);
}
}
Joystick:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint touchPoint = [touch locationInNode:self];
if (_isTracking && sqrtf(powf(touchPoint.x - _innerControl.position.x, 2) +
powf(touchPoint.y - _innerControl.position.y, 2)) < _outerControl.size.height) {
if (sqrtf(powf(touchPoint.x, 2) + powf(touchPoint.y, 2)) <= _innerControl.size.height / 2) {
_innerControl.position = CGPointMake(touchPoint.x, touchPoint.y);
} else {
double magV = sqrt(touchPoint.x * touchPoint.x + touchPoint.y * touchPoint.y);
double aX = touchPoint.x / magV * _innerControl.size.width;
double aY = touchPoint.y / magV * _innerControl.size.width / 2;
_innerControl.position = CGPointMake(aX, aY);
}
_velocity = CGPointMake(_innerControl.position.x, _innerControl.position.y);
NSLog(@"_velocity %@", NSStringFromCGPoint(_velocity));
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self resetInnerControlWithVelocity];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self resetInnerControlWithVelocity];
}
- (void)resetInnerControlWithVelocity {
_isTracking = NO;
_velocity = CGPointZero;
_innerControl.position = INNERCONTROLDEFAULTPOINT;
NSLog(@"%@", NSStringFromCGPoint(_velocity));
}
SpriteNode:
- (void)initTestSprite {
SKShapeNode *testShape = [SKShapeNode shapeNodeWithCircleOfRadius:15.0f];
testShape.strokeColor = [UIColor greenColor];
testShape.name = @"test";
testShape.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:15.0f];
testShape.physicsBody.dynamic = YES;
testShape.physicsBody.affectedByGravity = YES;
testShape.physicsBody.categoryBitMask = CBShooterCategory;
testShape.physicsBody.collisionBitMask = CBAsteroidCategory | CBGroundCategory | CBWorldCategory;
testShape.physicsBody.contactTestBitMask = CBAsteroidCategory | CBWorldCategory;
[self addChild:testShape];
}
Call MoveJoystick:
- (instancetype)initWithSize:(CGSize)sceneSize {
if (self = [super initWithSize:sceneSize]) {
self.physicsWorld.contactDelegate = self;
_velocityTick = [CADisplayLink displayLinkWithTarget:self selector:@selector(moveJoystick)];
[_velocityTick addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
Change physicBody (affectedGravity, dynamic) don`t take any changes
testSprite