简体   繁体   English

精灵不在萤幕上(左右)

[英]Sprite not staying on screen (left and right)

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. 我正在制作一个在屏幕上移动的Sprite游戏,我使用self.physicsBody = SKPhysicsBody(edgeLoopF​​romRect:self.frame)为边缘创建了一个碰撞。这似乎适用于屏幕的顶部和底部,但左侧而Sprite的右侧则移出屏幕。 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)

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 检查以确保您的sceneview设置为viewcontroller的边界,应该在将场景添加到视图中的位置执行此操作,应该是其中之一

/* 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. 如果是.AspectFill,则将超出屏幕范围。

Also do showPhysics = true in your SKView to display what your collision bounds are. 还要在SKView中执行showPhysics = true来显示碰撞范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM