Sprite套件 - 播放器使用加速度计的屏幕
Sprite Kit - Player going of screen with accelerometer
im使用Sprite套件编程游戏,我编写了加速度计代码,并且可以使用。但是问题在于,撞到屏幕边缘时并没有停止,有人可以帮我吗?这是播放器的代码:
-(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];
}
}];
}
}
您可以将边缘添加到屏幕上,以便物理时将其撞到边缘时停止(在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:持续时间:方法弄乱了您的阶段性,以确保您的DETX和DETX的效果更大,以使屏幕尺寸宽度(高度) - 船舶尺寸宽度(高度),而较小的原点X和Origin y则更少。
您不应该使用Moveto:持续时间:方法,而应在船上施加武力。尝试此代码,您的代码有点不同,但这是移动船的更好方法(忽略上面的代码):
//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)];
}
相关文章:
- 计算数组c++的平均值
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 如何在GTK程序运行时禁用屏幕保护程序/电源管理/屏幕消隐
- 使用 SFML 和 C++ 将 Pixel 打印到屏幕上
- 在 Windows 8/10 技术中完全实时的屏幕捕获,没有延迟
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- Winapi:屏幕截图未显示在窗口中
- 如何在快板的屏幕中显示子位图的绘制?
- 按回车键后输出屏幕关闭
- 要在屏幕上绘制一些小瓷砖,我应该使用 QQuickItem 还是 QQuickPaintedItem?
- 将 IPv6 支持添加到雨量计
- SFML 向下移动时如何围绕屏幕中心旋转?
- 截取屏幕截图后程序卡住
- 使用阈值进行加速度计振动检测
- 使用 Qt c++ 在 Windows Surface Pro 上获取加速度计数据
- Sprite套件 - 播放器使用加速度计的屏幕
- 设备陀螺仪(加速度计)在 Cocos2dx-2.2.6 中无法使用 -ObjC 链接器标志
- 无法访问在类"Windows::D evices::传感器::加速度计"中声明的私有成员
- 如何在 cocos2dx 中使用加速度计
- 加速度计"高度变化"代码 PIC