精灵套件 - 播放器带有加速度计的屏幕

时间:2014-01-24 12:35:50

标签: c++ objective-c sprite-kit

我使用精灵套件编程游戏,我已经编写了加速计代码,它可以工作。但问题是,当它击中屏幕边缘时它不会停止,任何人都可以帮我解决这个问题吗? 以下是播放器的代码:

    -(void)addShip
{
    //initalizing spaceship node
    ship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
    [ship setScale:0.5];
    ship.zRotation = - M_PI / 2;

    //Adding SpriteKit physicsBody for collision detection
    ship.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ship.frame.size];
    ship.physicsBody.mass = 0.02;
    ship.physicsBody.categoryBitMask = shipCategory;
    ship.physicsBody.dynamic = YES;
    ship.physicsBody.affectedByGravity = NO;
    ship.physicsBody.contactTestBitMask = DonutCategory | PizzaCategory | ChocolateCategory | SoftCategory | AppleCategory | GrapeCategory | OrangeCategory | BananaCategory;
    ship.physicsBody.collisionBitMask = 0;
    ship.physicsBody.usesPreciseCollisionDetection = YES;
    ship.name = @"ship";
    ship.position = CGPointMake(260,30);
    [self addChild:ship];



    motionManager = [[CMMotionManager alloc] init];
    if ([motionManager isAccelerometerAvailable] == YES) {
        [motionManager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
                                            withHandler:^(CMAccelerometerData *data, NSError *error)
         {
             float destX, destY;
             float currentX = ship.position.x;
             float currentY = ship.position.y;
             BOOL shouldMove = NO;

             if(data.acceleration.x < -0.25) {  // tilting the device to the right
                 destX = currentX + (data.acceleration.x * kPlayerSpeed);
                 destY = currentY;
                 shouldMove = YES;
             } else if (data.acceleration.x > 0.25) {  // tilting the device to the left
                 destX = currentX + (data.acceleration.x * kPlayerSpeed);
                 destY = currentY;
                 shouldMove = YES;
             }
             if(shouldMove) {
                 SKAction *action = [SKAction moveTo:CGPointMake(destX, destY) duration:1];
                 [ship runAction:action];
             }
         }];
    }

}

1 个答案:

答案 0 :(得分:1)

你可以在你的屏幕上添加边缘,这样当物理到达边缘时物理将停止发射(在init方法中执行):

self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
//Add this to change some behaviour when ship will collide with the screen
//self.physicsBody.friction = 0.0f;

另一种方法是在船舶接触到屏幕边缘并更改船舶位置时检入更新方法。

//扩展

我相信moveTo:持续时间:方法搞砸了你的相位,修复它只是确定你的destX和destX更多的屏幕尺寸宽度(高度) - 船舶尺寸宽度(高度)和更小的原点x和原点y

你不应该使用moveTo:duration:方法,而应该对你的船施加武力。 尝试这个代码,它与你的有点不同,但这是更好的方式来移动你的船(忽略上面的代码):

//Your ship setting
_ship.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_ship.frame.size];
_ship.physicsBody.mass = 0.02;
_ship.physicsBody.categoryBitMask = shipCategory;
_ship.physicsBody.dynamic = YES;
_ship.physicsBody.affectedByGravity = NO;

// Edge around the screen
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

//Apply movement (instead of moveTo:duration:)
//Get the accelerometer value
CMAccelerometerData* accelData = _motionManager.accelerometerData;
if (fabs(accelData.acceleration.x) > 0.2) {
    // 35 is the value you can play with to make your ship movement feel more natural
    [_ship.physicsBody applyForce:CGVectorMake(0.0, 35.0 * data.acceleration.x)];
}