Riccardo Burato Riccardo Burato - 1 year ago 77
Swift Question

Detecting touch pressure (force) with swift (SpriteKit)

So, I've got this game with Swift on Xcode where I have a SKShapeNode that jumps. I would like, though, to have this jump commensurated to the touch the player does. Hence, a small tap will get a small jump and a more brisk one gets a higher jump. How is it possible to do this (mind that I do not want any 3D touch options)?

Answer Source

Usually, this sort of thing in games is done by timing the press and altering the jump height accordingly. (Which is also why using physics in 2D platforming games is tricky for this kind of thing).

The way it is done in games like Super Mario is to have the start of the jump movement begin as soon as the touch is pressed. Then after some amount of time (as the character is half way "up" the jump) you check to see if the touch is still down. If it is you can make the jump animation continue upwards. If the touch is released then start to bring the jump down again.

That way a short touch is a small jump but a longer touch is a higher jump.

If you have a smallJumpAction and bigJumpAction at the moment then what you could do is break it down into smaller parts...

beginJumpAction which is triggered immediately and then... smallJumpEndAction and bigJumpEndAction.

This could be done with a sequence action...

let beginJumpAction = //move the player upwards slightly
let smallJumpEndAction = //begin to move the player down
let bigJumpEndAction = //move the player up a bit more and then move down

let blockAction = SKAction.run {
    if touchStillDown {
    } else {

player.runAction(SKAction.sequence([beginJumpAction, blockAction])

Something like this anyway. There are many ways to approach this though :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download