I want to move a sk-sprite node in sprite kit using core motion. I want it will be like in the game tilt2live. I mean that I want to tilt the device in order to change the node position. how can I do it? thanks.
1) Add the CoreMotion framework to your project
2) Use the basic sample code to move a SKSpriteNode using CoreMotion.
#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