如何用核心运动移动Sksprite节点? (精灵套件)
how to move Sksprite node with core motion? (sprite kit)
我想使用核心运动在 sprite kit 中移动一个 sk-sprite 节点。
我希望它会像在游戏 tilt2live 中一样。我的意思是我想倾斜设备以更改节点位置。
我该怎么做?
谢谢。
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) 将 CoreMotion 框架添加到您的项目中
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
我想使用核心运动在 sprite kit 中移动一个 sk-sprite 节点。 我希望它会像在游戏 tilt2live 中一样。我的意思是我想倾斜设备以更改节点位置。 我该怎么做? 谢谢。
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) 将 CoreMotion 框架添加到您的项目中
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