2017-02-12 80 views
0

我有一个精灵节点,它通过用户触摸从左向右移动。 不过目前它会退出屏幕,我想在任何一边添加一个节点,所以如果精灵节点触及它所拥抱的任一侧的节点并保持在那里,直到用户触摸使它沿相反方向行进。在屏幕/视图中包含一个精灵节点

这就是我想要做的,但它目前不工作。

let shipTexture = SKTexture(imageNamed: "ship.png") 
    ship = SKSpriteNode(texture: shipTexture) 
    ship.position = CGPoint(x: self.frame.midX, y: self.frame.midY) 
    ship.zPosition = 3 
    ship.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 30, height: 100)) 
    ship.physicsBody!.isDynamic = true 
    ship.physicsBody?.collisionBitMask = 0b1 
    ship.physicsBody?.contactTestBitMask = 0b1 
    ship.physicsBody!.collisionBitMask = 0b1 
    ship.physicsBody?.affectedByGravity = false 
    self.addChild(ship) 



    let side = SKNode() 
    side.position = CGPoint(x: self.frame.width/2, y: self.frame.midY) 

    side.physicsBody = SKPhysicsBody(edgeLoopFrom: CGRect(x: -240, y: -160, width: 480, height: 320)) 

    side.physicsBody!.isDynamic = false 
    side.physicsBody?.collisionBitMask = 0b1 
    side.physicsBody?.contactTestBitMask = 0b1 
    side.physicsBody!.collisionBitMask = 0b1 

    self.addChild(side) 

    func didBegin(_ contact: SKPhysicsContact) { 
     print("Collision") 
    } 
} 

//var moveLeft = SKAction.moveBy(x: 800, y: 0, duration: 2) 
//frame.size.width 




override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 




    ship.removeAllActions() 

    switch direction ?? .left { 
    case .left: 
     ship.run(SKAction.moveBy(x: -frame.size.width, y: 0, duration: 3)) 
    case .right: 
     ship.run(SKAction.moveBy(x: frame.size.width, y: 0, duration: 3)) 
    } 

    direction = direction == nil || direction == .right ? .left : .right 
} 

回答

1

我明白了。原因是 - 你用行动来移动你的节点,更应该用物理 - 力和冲动

试试这个:

import SpriteKit 
import GameplayKit 

var ship = SKSpriteNode() 
var bg = SKSpriteNode() 

class GameScene: SKScene, SKPhysicsContactDelegate { 


    override func didMove(to view: SKView) { 


     let bgTexture = SKTexture(imageNamed: "bg.png") 
     let moveBGanimation = SKAction.move(by: CGVector(dx: 0, dy: -bgTexture.size().height), duration: 4) 
     let shiftBGAnimation = SKAction.move(by: CGVector(dx: 0, dy: bgTexture.size().height), duration: 0) 
     let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGanimation, shiftBGAnimation])) 

     var i: CGFloat = 0 

     while i < 3 { 

      bg = SKSpriteNode(texture: bgTexture) 
      bg.position = CGPoint(x: self.frame.midX, y: bgTexture.size().height * i) 
      bg.size.width = self.frame.width 

      bg.run(moveBGForever) 
      self.addChild(bg) 

      i += 1 

     } 

     let shipTexture = SKTexture(imageNamed: "ship.png") 
     ship = SKSpriteNode(texture: shipTexture) 
     ship.position = CGPoint(x: self.frame.midX, y: self.frame.midY) 
     ship.zPosition = 3 
     ship.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 30, height: 100)) 
     ship.physicsBody!.isDynamic = true 
     ship.physicsBody?.collisionBitMask = 0b1 
     ship.physicsBody?.contactTestBitMask = 0b1 
     ship.physicsBody!.categoryBitMask = 0b1 
     ship.physicsBody?.affectedByGravity = false 
     self.addChild(ship) 

     let side = SKNode() 
     side.position = CGPoint(x: 0, y: 0) 
     side.physicsBody = SKPhysicsBody(edgeLoopFrom: CGRect(x: -self.frame.width/2, y: -self.frame.height/2, width: self.frame.width, height: self.frame.height)) 

     side.physicsBody!.isDynamic = false 
     side.physicsBody?.collisionBitMask = 0b1 
     side.physicsBody?.contactTestBitMask = 0b1 
     side.physicsBody!.categoryBitMask = 0b1 

     self.addChild(side) 

     self.physicsWorld.contactDelegate = self 

     func didBegin(_ contact: SKPhysicsContact) { 
      print("Collision") 
     } 

    } 

    //var moveLeft = SKAction.moveBy(x: 800, y: 0, duration: 2) 
    //frame.size.width 

    enum Direction: Int { 
     case left = 0 
     case right 
    } 

    var direction: Direction? 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 




     ship.removeAllActions() 

     switch direction ?? .left { 
     case .left: 
      ship.physicsBody?.applyImpulse(CGVector(dx: -20, dy: 0)) 
      //ship.run(SKAction.moveBy(x: -frame.size.width, y: 0, duration: 3)) 
     case .right: 
      ship.physicsBody?.applyImpulse(CGVector(dx: 20, dy: 0)) 
      //ship.run(SKAction.moveBy(x: frame.size.width, y: 0, duration: 3)) 
     } 

     direction = direction == nil || direction == .right ? .left : .right 
    } 




    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 



    } 

    override func update(_ currentTime: TimeInterval) { 


    } 
} 
+0

这是不工作的一些原因。还有什么需要添加?我是否正确地将节点添加到右侧? –

+0

确保你也为飞蛾节点设置相同的'CategoryBitMask' –

+0

我肯定会在某个地方出错。对于船舶和船边来说,它们都设置为0b1。视角右侧的位置是否正确? –

相关问题