2015-04-03 58 views
-1

我想使用核心運動在sprite工具包中移動sk-sprite節點。我希望它會像在遊戲中一樣傾斜。我的意思是我想傾斜設備以改變節點的位置。 我該怎麼辦? 謝謝。如何用核心運動來移動Sksprite節點? (sprite kit)

+2

我想要一個復活節的焦糖巧克力蛋。到目前爲止你嘗試了什麼,你卡在哪裏? – sangony 2015-04-03 13:20:38

+0

我試着用這個 - (void)加速計:(UIAccelerometer *)加速計didAccelerate: – 100tomer 2015-04-03 14:24:20

回答

1

Tuts +上有一個優秀的tutorial,演示了在SpriteKit遊戲中使用加速計。是的,你必須做什麼亮點如下:

首先,導入CoreMotion庫:

#import <CoreMotion/CoreMotion.h> 

聲明一個CMMotionManager屬性現場

@property (strong, nonatomic) CMMotionManager *motionManager; 

-initWithSize:方法,實例化運動管理器

//CoreMotion 
self.motionManager = [[CMMotionManager alloc] init]; 
self.motionManager.accelerometerUpdateInterval = .2; 

[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] 
             withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { 
             [self outputAccelertionData:accelerometerData.acceleration]; 
             if(error) 
             { 
              NSLog(@"%@", error); 
             } 
}]; 

你將開始將加速度計數據沿着x和y軸獲取到outputAccelertionData:方法。

用它來移動節點。

1

1)添加CoreMotion框架到您的項目

enter image description here

2)使用基本的代碼示例使用移動一個CoreMotion SKSpriteNode。

#import "GameScene.h" 
#import <CoreMotion/CoreMotion.h> 

@implementation GameScene { 
    CMMotionManager *motionManager; 
    CMDeviceMotion *devMotion; 
    SKSpriteNode *node0; 
} 

-(void)didMoveToView:(SKView *)view { 
    self.backgroundColor = [SKColor blackColor]; 

    motionManager = [[CMMotionManager alloc] init]; 
    [motionManager startDeviceMotionUpdates]; 
    [motionManager setDeviceMotionUpdateInterval:1.0/30.0]; 
    devMotion = motionManager.deviceMotion; 

    node0 = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(20, 20)]; 
    node0.position = CGPointMake(300, 300); 
    node0.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:node0.size]; 
    node0.physicsBody.dynamic = YES; 
    node0.physicsBody.affectedByGravity = NO; 
    [self addChild:node0]; 
} 

-(void)update:(CFTimeInterval)currentTime { 

    CMDeviceMotion *currDeviceMotion = motionManager.deviceMotion; 
    //NSLog(@"Roll Pitch and Yaw are %f, %f, %f",currDeviceMotion.attitude.roll, currDeviceMotion.attitude.pitch, currDeviceMotion.attitude.yaw); 

    // update position using CGPointMake 
    if(currDeviceMotion.attitude.pitch > 0.10) { 
     node0.position = CGPointMake(node0.position.x+1, node0.position.y); 
    } 

    if(currDeviceMotion.attitude.pitch < -0.10) { 
     node0.position = CGPointMake(node0.position.x-1, node0.position.y); 
    } 

    if(currDeviceMotion.attitude.roll > 0.10) { 
     node0.position = CGPointMake(node0.position.x, node0.position.y+1); 
    } 

    if(currDeviceMotion.attitude.roll < -0.10) { 
     node0.position = CGPointMake(node0.position.x, node0.position.y-1); 
    } 

    /* 
    // update position using CGVectorMake 
    if(currDeviceMotion.attitude.pitch > 0.10) { 
     node0.physicsBody.velocity = CGVectorMake(node0.physicsBody.velocity.dx+10, node0.physicsBody.velocity.dy); 
     // set speed limit of 100 
     if(node0.physicsBody.velocity.dx > 100) 
      node0.physicsBody.velocity = CGVectorMake(100, node0.physicsBody.velocity.dy); 
    } 

    if(currDeviceMotion.attitude.pitch < -0.10) { 
     node0.physicsBody.velocity = CGVectorMake(node0.physicsBody.velocity.dx-10, node0.physicsBody.velocity.dy); 
     // set speed limit of 100 
     if(node0.physicsBody.velocity.dx < -100) 
      node0.physicsBody.velocity = CGVectorMake(-100, node0.physicsBody.velocity.dy); 
    } 

    if(currDeviceMotion.attitude.roll > 0.10) { 
     node0.physicsBody.velocity = CGVectorMake(node0.physicsBody.velocity.dx, node0.physicsBody.velocity.dy+10); 
     // set speed limit of 100 
     if(node0.physicsBody.velocity.dy > 100) 
      node0.physicsBody.velocity = CGVectorMake(node0.physicsBody.velocity.dx, 100); 
    } 

    if(currDeviceMotion.attitude.roll < -0.10) { 
     node0.physicsBody.velocity = CGVectorMake(node0.physicsBody.velocity.dx, node0.physicsBody.velocity.dy-10); 
     if(node0.physicsBody.velocity.dy < -100) 
      node0.physicsBody.velocity = CGVectorMake(node0.physicsBody.velocity.dx, -100); 
    } 
    */ 
} 

@end 
+0

好的,謝謝你的幫助 – 100tomer 2015-04-03 23:11:54