0

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)
sangony
  • 11,636
  • 4
  • 39
  • 55
JMD
  • 69
  • 4

1 Answers1

1

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.

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
  • Thanks. I changed it from .AspectFill to Aspect.Fit and the game is zoomed out and I can see the edges but it's not how I want it to look. Is this because my background images are too big, confused about this. – JMD Apr 14 '15 at 21:35
  • Also the Spite now hits off the edges but then "passes" through them and disappears – JMD Apr 14 '15 at 21:36
  • I am assuming you are making a universal app with storyboards? You never set the collisionMask on the scene physics body, I am not sure if it defaults to 0xFFFF. Did you show your collision bounds to make sure they are in the correct places? – Knight0fDragon Apr 15 '15 at 18:24
  • Hi am making an iPhone app and I want it to be Portrait mode only which I have selected under the deployment info options so I'm not sure why the left and right margins extend so far past the screen edge. At the moment I'm just editing the main gamescene, I haven't looked into storyboards yet but will do some research on it. Also have the collision bit mask set as below, the sprite bumps off the edge but then passes through - self.physicsBody?.categoryBitMask = PhysicsCategory.Edge physicsWorld.contactDelegate = self – JMD Apr 15 '15 at 22:00
  • there is a collisionBitMask you need at assign. How it works is you use categoryBitMask is used to identify your sprite, then collisionBitMask to tell the sprite what it can collide with, and finally contactBitMask to determine what other sprites it should touch, but not be considered a collision. If you used the default setup for making a gamescene, then your gamescene environment is something like 600x600. Now remember that Iphones have a total of 4 active resolutions and 1 depricated resolution as of IOS 8 (640x960,640,1136, 750x1334, and 1242×2208 ) (320x480 is depricated). – Knight0fDragon Apr 16 '15 at 14:49
  • The scene will do what is best to fill in the screen, but you need to tell it how to do that. If I were you, start with scene.scaleMode = .ScaleToFit; until you learn how to handle the other devices. – Knight0fDragon Apr 16 '15 at 14:50
  • It's working now - I had forgot to add - let scene = GameScene(size: skView.bounds.size) - in GameViewController. Thanks for the help, it gets me over the hump and allow me continue on :) – JMD Apr 16 '15 at 19:42