ios - Swift SpriteKit edgeLoopFromRect Issue -


the following code recognizes bottom , top edges of scene , ball bounces off expected. however, left , right edges of scene breached time. ball goes off screen , returns if enough force applied. if edges of scene beyond edges of iphone simulator window.

import spritekit  class gamescene: skscene {     override func didmovetoview(view: skview){         self.physicsbody = skphysicsbody(edgeloopfromrect: self.frame)         backgroundcolor = uicolor.cyancolor()         var ball = skspritenode(imagenamed: "ball")         ball.position = cgpoint(x: self.size.width/2, y: self.size.height/2)         self.addchild(ball)         ball.physicsbody = skphysicsbody(circleofradius: ball.frame.size.height/2)         let push = cgvectormake(10, 10)         ball.physicsbody.applyimpulse(push)      } } 

i think has .sks file since dimensions set there , if change dimensions reflects on game. know how make line of code takes precedence on .sks file dimensions? way dynamic , work on different devices.

self.physicsbody = skphysicsbody(edgeloopfromrect: self.frame) 

i found same question here never answered: swift physicsbody edge recognition issue

also, new ios app dev , swift forgive me if answer obvious , missed it.

you on right track .sks file. when scene loaded .sks file default size 1024x768. can dynamically set based on view size. so, swap viewdidload viewwilllayoutsubviews this:

override func viewwilllayoutsubviews() {     super.viewwilllayoutsubviews()      if let scene = gamescene(filenamed:"gamescene") {         // configure view.         let skview = self.view as! skview         skview.showsfps = true         skview.showsnodecount = true          /* sprite kit applies additional optimizations improve rendering performance */         skview.ignoressiblingorder = true          //because viewwilllayoutsubviews might called multiple times, check if scene initialized          if(skview.scene == nil){              /* set scale mode scale fit window */             scene.scalemode = .aspectfill             scene.size = skview.bounds.size              skview.presentscene(scene)         }     } } 

there many posts on differences between viewdidload , viewwilllayoutsubviews, skip in answer.

hope helps.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -