在屏幕/视图中包含一个精灵节点
Posted
技术标签:
【中文标题】在屏幕/视图中包含一个精灵节点【英文标题】:Contain a sprite node within the screen/view 【发布时间】:2017-02-12 21:16:12 【问题描述】:我有一个精灵节点,它随着用户的触摸从左向右移动。 但是目前它会退出屏幕,我想在任一侧添加一个节点,所以如果精灵节点接触到任一侧的节点,它会拥抱它并保持在那里,直到用户触摸以使其向相反方向移动。
这是我想做的,但目前不起作用。
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)
【讨论】:
由于某种原因这不起作用。还有什么需要补充的吗?我是否将节点正确添加到右侧? 确保你也为蛾节点设置了相同的CategoryBitMask
我肯定在某个地方出错了。船和舷侧都设置为 0b1。侧边的位置是否正确为视图的右侧边缘?
如果有帮助,我已将完整代码包含在我的原始帖子中
所以你的船是正确移动的,所有的问题是碰撞没有任何反应,对吧?以上是关于在屏幕/视图中包含一个精灵节点的主要内容,如果未能解决你的问题,请参考以下文章