从 SKNode 楼层类中移除重力
Posted
技术标签:
【中文标题】从 SKNode 楼层类中移除重力【英文标题】:Remove Gravity From SKNode Floor Class 【发布时间】:2017-12-22 00:34:00 【问题描述】:我的游戏开始时出现了两个矩形,但一旦开始,重力就会将它们拉下来。我在一个单独的“Floor.swift”类中有下面的代码。我还有一个“BitMaskCategories.swift”文件,其中包含以下信息。
let ballCategory : UInt32 = 0x1 << 1
let avoidCategory : UInt32 = 0x1 << 2
let floorCategory : UInt32 = 0x1 << 3
let pointCategory : UInt32 = 0x1 << 4
let lifeCategory : UInt32 = 0x1 << 5
这是我的“Floor.swift”类
import Foundation
import SpriteKit
class Floor: SKNode
override init()
super.init()
let leftWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 50, height: 300))
leftWall.position = CGPoint(x: 0, y: 100)
leftWall.physicsBody!.isDynamic = false
leftWall.physicsBody = SKPhysicsBody(rectangleOf: leftWall.size)
self.addChild(leftWall)
let rightWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 50, height: 300))
rightWall.position = CGPoint(x: 100, y: 200)
rightWall.physicsBody!.isDynamic = false
rightWall.physicsBody = SKPhysicsBody(rectangleOf: rightWall.size)
self.addChild(rightWall)
// self.physicsBody?.affectedByGravity = false
// self.physicsBody?.isDynamic = false
// Set the bit mask properties
self.physicsBody?.categoryBitMask = balloonCategory
self.physicsBody?.contactTestBitMask = nailDropCategory | pointCategory | lifeCategory
//self.physicsBody?.collisionBitMask = balloonCategory
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemted")
为什么我得到错误,“线程 1:致命错误:在展开可选值时意外发现 nil”但是当我使用“?”代替 ”!”该应用程序有效,但矩形下降。我需要那条线才能让他们不这样做,我什至不明白这有什么意义。
【问题讨论】:
【参考方案1】:您正在尝试分配:.isDynamic = false
在对象初始化之前,
你应该移动leftWall.physicsBody!.isDynamic = false
之后 leftWall.physicsBody = SKPhysicsBody(rectangleOf: leftWall.size)
同样:rightWall.physicsBody!.isDynamic = false
,移动它
之后:rightWall.physicsBody = SKPhysicsBody(rectangleOf: rightWall.size)
所以你的代码可能是:
let leftWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 50, height: 300))
leftWall.position = CGPoint(x: 0, y: 100)
leftWall.physicsBody = SKPhysicsBody(rectangleOf: leftWall.size)
leftWall.physicsBody!.isDynamic = false
self.addChild(leftWall)
let rightWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 50, height: 300))
rightWall.position = CGPoint(x: 100, y: 200)
rightWall.physicsBody = SKPhysicsBody(rectangleOf: rightWall.size)
rightWall.physicsBody!.isDynamic = false
self.addChild(rightWall)
【讨论】:
非常感谢你! 很高兴您发现它很有用。是的! :) 所以一件事导致了另一个问题,我觉得您很快就会知道解决方案。如果您有机会,请查看我的新帖子***.com/questions/47946410/…以上是关于从 SKNode 楼层类中移除重力的主要内容,如果未能解决你的问题,请参考以下文章
从 fetchedResultsController fetchedObjects 中移除一个对象
如何从一侧移除高程阴影而不从卡片或材质小部件中移除高程本身?