SpriteKit上的引力混乱

时间:2015-07-01 10:38:58

标签: ios objective-c sprite-kit skspritenode

当场景初始化时我设定了世界重力-2.0f。我在场景和操纵杆节点上添加了对象,我控制了这个对象。当我触摸操纵杆节点时,世界重力设置为0,当结束触摸操纵杆时,世界重力设置为-2.0f。

我不明白,我做错了什么。因为物体有时会慢速移动。

有关详细信息,请参阅video / video2示例。

谢谢你的回答。 附:对不起我的英文。我希望你明白。 ;)

移动方法:

- (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));
}

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];
}

调用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(受影响的重力,动态)不进行任何更改

testSprite position

1 个答案:

答案 0 :(得分:0)

修复对我有用

if (_joystickController.velocity.y > 0)
    self.physicsWorld.gravity = CGVectorMake(.0f, 2.0f);