Reputation: 69
I am making a game with a Sprite that moves around the screen, I have created a collision for the edges using self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame) This seems to work for the top and bottom of the screen but the left and right sides the Sprite just moves off screen. I'm not sure why this is, appreciate any advice for a beginner. I included code for a scrolling background I have set up. I also have other enemy sprites spawning off screen and moving into view, maybe this effects the boundary?
class GameScene: SKScene, SKPhysicsContactDelegate{
struct PhysicsCategory {
static let None : UInt32 = 0
static let All : UInt32 = UInt32.max
static let Edge : UInt32 = 0b1
static let Spaceman: UInt32 = 0b10
static let Ship: UInt32 = 0b11
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody?.categoryBitMask = PhysicsCategory.Edge
physicsWorld.contactDelegate = self
// Repeating Background
var bgTexture = SKTexture(imageNamed: "spacebackground")
var movebg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 9)
var replacebg = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
var moveForever = SKAction.repeatActionForever(SKAction.sequence([movebg, replacebg]))
for var i:CGFloat=0; i<3; i++ {
var wave = SKSpriteNode(texture: bgTexture)
wave.position = CGPoint(x: bgTexture.size().width/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
wave.size.height = self.frame.height
wave.runAction(moveForever)
self.addChild(wave)
}
Spaceman.texture = (Texture1)
Spaceman.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
Spaceman.setScale(0.4)
Spaceman.physicsBody=SKPhysicsBody(rectangleOfSize: TheBat.size)
Spaceman.physicsBody?.affectedByGravity = true
Spaceman.physicsBody?.dynamic = true
Spaceman.physicsBody?.allowsRotation = false
Spaceman.physicsBody?.categoryBitMask = PhysicsCategory.Spaceman
Spaceman.physicsBody?.contactTestBitMask = PhysicsCategory.Ship
Spaceman.physicsBody!.collisionBitMask = PhysicsCategory.Edge
self.addChild(Spaceman)
Upvotes: 0
Views: 773
Reputation: 16837
Check to make sure your sceneview is set to the bounds of the viewcontroller, this should be done where you are adding the scene to the view, should be one of these
/* Set the scale mode to scale to fit the window */
scene.size = skView.bounds.size
scene.scaleMode = .ScaleToFit;
/* Set the scale mode to scale to fit the window */
scene.size = skView.bounds.size
scene.scaleMode = .AspectFit;
if it is .AspectFill, you will go outside the screen bounds.
Also do showPhysics = true in your SKView to display what your collision bounds are.
Upvotes: 1