swift - Changing collisionBitMask according to y value ascending or descending -
swift - spritekit
i making first game , have var player = skspritenode()
, var platform = sknode()
i want detect when player increasing in y direction can turn off collisionbitmask
. therefore making possible jump through platforms when jumping , when falling land on platforms.
so can have like:
if player.position.y = increasing { player.physicsbody?.collisionbitmask = nil }else{ player.physicsbody?.collisionbitmask = physicscategory.platform }
i have looked around , cannot find after, unless i'm looking wrong thing.
many thanks.
edit: solved check emiliopelaez answer, showing how i changed here.
declared var lasty: cgfloat?
outside functions other variables.
override func update(currenttime: cftimeinterval) { /* called before each frame rendered */ // checking y direction if player.position.y > lasty { // player increasing in y direction player.physicsbody?.collisionbitmask = physicscategory.ground } else { // player decreasing in y direction player.physicsbody?.collisionbitmask = physicscategory.ground | physicscategory.platform } lasty = player.position.y }
basically, want compare current value value in previous frame.
the easiest way make variable can store previous value, , use comparison. once have done comparison store current value in variable used next frame.
something this:
var lasty: cgfloat? // in function if let lasty = lasty { if player.y > lasty { // player.y increasing } else { // player.y decreasing } } lasty = player.y
Comments
Post a Comment