ios - Swift - Strange Sprite behaviour using accelerometer -
i'm beginner swift , have first game complete except 1 annoying issue. have sprite flies around screen, accelerometer determines x position , touching screen moves sprite screen. works 95% of time way intend, every , accelerometer stops working 30 second minute - can't find pattern , it's driving me crazy - have suggestion ? partial code below
import spritekit import coremotion class gamescene: skscene, skphysicscontactdelegate{ var destx:cgfloat = 0.0 var blast = skspritenode(imagenamed: "blast") var motionmanager = cmmotionmanager() var texture1 = sktexture(imagenamed: "bat") var texture2 = sktexture(imagenamed: "batjet") override func didmovetoview(view: skview) { // scene setup physics self.physicsbody = skphysicsbody(edgeloopfromrect: self.frame) self.physicsbody!.categorybitmask = physicscategory.edge physicsworld.contactdelegate = self self.physicsbody!.friction = 0 self.physicsworld.gravity = cgvectormake(-0.0, -0.8) // set player thebat.texture = (texture1) thebat.position = cgpoint(x: self.frame.size.width/2, y: self.frame.size.height/2) thebat.setscale(0.25) thebat.physicsbody=skphysicsbody(rectangleofsize: thebat.size) thebat.physicsbody?.affectedbygravity = true thebat.physicsbody?.dynamic = true thebat.physicsbody?.allowsrotation = false thebat.physicsbody?.categorybitmask = physicscategory.bat thebat.physicsbody?.contacttestbitmask = physicscategory.ship thebat.physicsbody!.collisionbitmask = physicscategory.edge | physicscategory.ship self.addchild(thebat) // determine player x position motionmanager.accelerometerupdateinterval = 0.1 motionmanager.startaccelerometerupdatestoqueue(nsoperationqueue.currentqueu e(), withhandler: { (accelerometerdata: cmaccelerometerdata!, error: nserror!) in let acceleration = accelerometerdata.acceleration self.destx = (cgfloat(acceleration.x) * 0.75) + (self.destx * 0.25) }) override func didsimulatephysics() { // set velocity based on x-axis acceleration thebat.physicsbody?.velocity = cgvector(dx: destx * 600.0, dy: thebat.physicsbody!.velocity.dy)}
Comments
Post a Comment