我想使用核心運動在sprite工具包中移動sk-sprite節點。我希望它會像在遊戲中一樣傾斜。我的意思是我想傾斜設備以改變節點的位置。 我該怎麼辦? 謝謝。如何用核心運動來移動Sksprite節點? (sprite kit)
-1
A
回答
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框架到您的項目
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
相關問題
- 1. OS X上的Sprite Kit - 移動
- 2. Sprite套件 - 手指移動節點的運動
- 3. 在Sprite Kit中運行棒圖動畫
- 4. Sprite-kit:運動期間移位的物理體
- 5. Sprite Kit中的彈跳節點
- 6. Sprite kit節點在接觸時口吃
- 7. 避免Sprite Kit節點彼此堆疊
- 8. Sprite Kit - 使用Switch Case向場景添加隨機Sprite節點
- 9. Sprite Kit等價於clipsToBounds:
- 10. 在Swift Sprite Kit中有可用的核心數據?
- 11. Sprite-Kit縮放問題UIPinchGestureRecognizer
- 12. 我如何在sprite工具包中快速移動節點
- 13. iOS7 Sprite Kit如何使用Sprite Kit重置/開始新遊戲?
- 14. 使用核心運動
- 15. 如何使用按鈕向上和向下移動spriteKit節點
- 16. 如何檢測是否有子節點被觸摸:Sprite Kit
- 17. iOS如何實現Sprite Kit中節點的UIButton類似行爲?
- 18. iOS7 Sprite Kit如何結合交互式Sprite節點和交互式UIViews?
- 19. SpriteKit + Swift - 如何使用緩動來移動節點?
- 20. 使用accerlerometer移動兩樣東西的核心運動
- 21. iOS 7 Sprite Kit動畫改變速度
- 22. SKSprite節點字典/陣列
- 23. 使用JavaScript來移動節點
- 24. Swift 3.0 - Sprite Kit - Multitouch
- 25. Sprite不移動
- 26. 移動節點
- 27. 移動節點
- 28. Sprite-Kit場景滾動 - Objective-C/iOS
- 29. Hit Sprite Kit上的爆炸動畫
- 30. Sprite Kit Objective C
我想要一個復活節的焦糖巧克力蛋。到目前爲止你嘗試了什麼,你卡在哪裏? – sangony 2015-04-03 13:20:38
我試着用這個 - (void)加速計:(UIAccelerometer *)加速計didAccelerate: – 100tomer 2015-04-03 14:24:20