sprite kit - SKPhysicsJointFixed in SpriteKit and Swift -
i'm making game in sprite kit , swift , have sprite @ bottom of screen , falling sprites top want catch , stick sprite @ bottom, i'm trying use skphysicsjointfixed when objects collide instead of falling object sticking 1 @ bottom supposed catch , have attached, seems if bottom sprite adapts physics of falling sprite , falls off screen it. here's code have in didbegincontact method. , skewer name of sprite @ bottom should @ bottom , not disappear.
if contact.bodya.node!.name == "skewer" { let boundx = skewer.physicsbody?.node?.position.x let fixedjoint = skphysicsjointfixed.jointwithbodya(contact.bodya.node!.physicsbody, bodyb: contact.bodyb.node!.physicsbody, anchor: cgpoint(x: boundx!, y: boundy)) physicsworld.addjoint(fixedjoint) // contact.bodyb.node!.removefromparent() } else { contact.bodya!.node!.removefromparent() }
and physics bottom screen sprite here
func makeskewer() { skewer.name = "skewer" skewer.position = cgpoint(x: size.width * 0.5, y: size.height * 0.244) skewer.physicsbody = skphysicsbody(rectangleofsize: skewer.size) skewer.physicsbody?.affectedbygravity = false skewer.physicsbody?.categorybitmask = kskewercategory skewer.physicsbody?.contacttestbitmask = kfoodcategory skewer.physicsbody?.collisionbitmask = ksceneedgecategory addchild(skewer) }
and physics falling sprites here
func random() ->cgfloat { return cgfloat(float(arc4random()) / 0xffffffff) } func random(#min: cgfloat,max: cgfloat) -> cgfloat { return random() * (max - min) + min } func addfood() { let randomcatchindex = int(arc4random_uniform(uint32(foods.count))) let food = skspritenode(imagenamed: foods[randomcatchindex]) let actualx = random(min: food.size.width/2, max: size.width - food.size.width/2) let actualduration = random(min: cgfloat(1.5), max: cgfloat(8.0)) let actionmove = skaction.moveto(cgpoint(x: actualx, y: -food.size.height/2), duration: nstimeinterval(actualduration)) let actionmovedone = skaction.removefromparent() food.physicsbody = skphysicsbody(rectangleofsize: food.size) food.position = cgpoint(x: actualx, y: size.height + food.size.height/2) food.physicsbody?.categorybitmask = kfoodcategory food.physicsbody?.contacttestbitmask = kskewercategory food.physicsbody?.collisionbitmask = 0x0 food.physicsbody?.dynamic = true food.runaction(skaction.sequence([actionmove, actionmovedone])) addchild(food) }
set skewer not have dynamic physics. have not being affected gravity, , locks onto food (which traveling down , has momentum), skewer moves it.
in creation of skewer, run following line:
skewer.physicsbody?.dynamic = false
you can ignore affectedbygravity
affects dynamic objects.
Comments
Post a Comment