[英]Confusion with gravity on SpriteKit
當場景初始化時,我將世界重力設置為 -2.0f。 我在場景和操縱桿節點上添加了對象,我控制這個對象。 當我觸摸操縱桿節點時,世界重力設置為 0,當結束觸摸操縱桿時,世界重力設置為 -2.0f。
我無法理解,我做錯了什么。 因為物體有時移動速度很慢。
有關更多詳細信息,您可以查看video / video2示例。
謝謝你的回答。 PS對不起我的英語。 我希望你明白。 ;)
移動方法:
- (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);
}
}
操縱桿:
- (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));
}
精靈節點:
- (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];
}
調用 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];
改變 physicBody (affectedGravity, dynamic) 不做任何改變
測試精靈
修復對我有用
if (_joystickController.velocity.y > 0)
self.physicsWorld.gravity = CGVectorMake(.0f, 2.0f);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.