2014-01-24 88 views
1

使用Sprite工具包編寫遊戲,我已經編寫了加速計代碼並且可以工作。但問題是,它擊中屏幕邊緣時不會停止,任何人都可以幫助我解決這個問題嗎? 這裏是玩家的代碼: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

你可以當它擊中邊(做,在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)]; 
} 
+0

我嘗試過,但沒有得到它的工作?有任何想法嗎?順便說一句,它iPhone和它的船隻左右移動。 – user3110546

+0

@ user3110546看到編輯答案。 – Greg

+0

我已經更新了問題中的代碼,我還需要做什麼,因爲它仍然不起作用 – user3110546