I've been having some issues with a SpriteKit game I'm working on. I'm trying to do a platformer-style game, where the player taps on the screen to make the character jump. I'm achieving this with:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// reset the velocity each jump, so that a constant impulse is always applied
self.player.physicsBody.velocity = CGVectorMake(0, 0);
// jump the player up
[self.player.physicsBody applyImpulse:CGVectorMake(0, PLAYER_JUMP_HEIGHT)];
}
PLAYER_JUMP_HEIGHT
is just a constant. I also setup the player with a constant mass.
This works just great on the iPad simulator, which is where I did the bulk of my initial testing. However, after adjusting my code to scale with device resolution (for phones and such), it appears my physics are now very off, particularly on the iPhone 4S simulator. That same impulse now jumps the player up very high, and they also seem to fall off objects such as platforms quicker, in more of a linear curve than before.
I scaled my scene to accommodate multiple devices by first setting the scaling mode of the scene:
scene.scaleMode = SKSceneScaleModeResizeFill;
I also came up with a way to scale everything else in the game by comparing the current device's resolution to the resolution of the iPad, and dividing to get a scale factor to multiply everything by. So, where the iPad screen width is 2048, if I was running on the iPhone 4S, the scale factor would be 960 / 2048 = 0.46. In this way I can design the game using one set of constants that would scale across all device resolutions, without the cropping or other scaling issues of other scale modes.
I'm pretty stumped as to what this could be. I've tried multiplying the player mass and jump height by the screen scale factor, in all possible combinations, to no avail. I also tried messing with the physicsWorld gravity and speed, which didn't work either. If anyone has experience in dealing with this and would be up for sharing, any help would be greatly appreciated. Thank you!
CGVector
forapplyImpulse
according to the scale factor? – WangYudong