Sprite套件 - 播放器使用加速度计的屏幕

Sprite Kit - Player going of screen with accelerometer

本文关键字:加速度计 屏幕 套件 播放器 Sprite      更新时间:2023-10-16

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)];
}