Domanda

I have a SpriteNode which is affected by gravity only on the y-Axis and can be moved with the accelerometer on the x-Axis. I also have a border (SKPhysicsBody) on my Scene which keeps my node inside my scene. The problem is now that my node ignores the border when it performs its SKAction caused by the accelerometer.

My code for the x movement/ accelerometer and its action:

birdNode is my SpriteNode

float destX = 0.0;


         float currentX = birdNode.position.x;
         BOOL shouldMove = NO;

         if (data.acceleration.x < -0.1) {
             destX = currentX + (data.acceleration.x * playerXSpeed);
             shouldMove = YES;
         }
         else if (data.acceleration.x > 0.1) {
             destX = currentX + (data.acceleration.x * playerXSpeed);
             shouldMove = YES;
         }

         if (shouldMove) {
             if (birdNode.position.x + destX < self.frame.size.width || birdNode.position.x - destX > 0) {

                SKAction *moveBird = [SKAction moveToX:destX duration:0.1];
                 [birdNode runAction:moveBird];
             }
         }
È stato utile?

Soluzione

I assume that you have your collision bit masks properly set up to handle collisions. You should confirm this with a test to double check.

I believe your issue is with the speed of your object. If the object is moving too fast, and I do not see any velocity limits in your posted code, your object can move past the screen boundary in one update cycle.

For example, your current object's position is (400,100). Given enough velocity, your object's next update: position could be (600,100). This means that your object literally jumped past the boundary without causing a collision.

The solution is to either limit the velocity or to institute a position check before setting your object's new position. For example, if the new x position is > the screen width then set the x position to the max allowed screen width x.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top