我试图在它离开场景后删除对象,这样我就可以减少公羊消耗
我应该把我试过的代码但我不知道如何开始所以我没有什么可以放在这里抱歉
修改
或许我应该检测视图中是否有对象并删除它,如果不知道我是怎么知道对象是否在视图内?
答案 0 :(得分:1)
有一种方便的方法可以检查两个CGRect是否相互交叉
你可以做这样的事情
if( CGRectIntersectsRect(object.frame, view.frame) ) {
// Don't delete your object
} else {
// Delete your object as it is not in your view
}
我希望这会有所帮助:)
答案 1 :(得分:1)
如果节点在屏幕外,您可以以不同的方式检查,这取决于您移动节点的方式。
第一种方法:
if (!intersectsNode(yourNode)) {
// node is off-screen
}
要枚举您可以使用的节点: - enumerateChildNodesWithName:usingBlock:要访问节点树中的所有节点,请阅读this。
另一种方法是使用动作:
let move = SKAction.moveTo(location: offScreenLocation, duration: 5)
let remove = SKAction.runBlock({yourNode.removeFromParent()})
let sequence = SKAction.sequence([move,remove])
yourNode.runAction(sequence, withKey:"moving") //Use action with key, to cancel the action if needed
第三种方法是使用contact detection。