- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对函数 removeFromParent
的行为真的很奇怪
lazy var buttonAds: SKSpriteNode = {
let n = SKSpriteNode(imageNamed: "ButtonAds")
n.position = CGPoint(x: size.width / 2, y: 600)
n.zPosition = 100
n.setScale(1.4)
return n
}()
在 didMove(...)
中使用 addChild(buttonAds)
添加此按钮,后者在 touchesBegan
中:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
if buttonAds.contains(touch.location(in: self)) {
// ...
doAds()
buttonAds.removeFromParent()
}
}
如果你点击广告按钮,将被删除,但如果再次点击那个地方,这将再次调用函数 doAds()
......很奇怪,buttonAd 不存在场景。
初始:
点击后:
谢谢
最佳答案
你要做的是检查你触摸的节点是否属于它应该的类型。将您的代码更改为:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
if nodeAtPoint(touch.locationInNode(self)) == buttonAds {
doAds()
buttonAds.removeFromParent()
}
}
这应该可以解决问题!
编辑:至于为什么会这样,你正在从场景中移除节点,但它仍然是内存中的一个对象(否则你将无法使用 buttonAds.contains(...)
在上面)所以它也仍然存储着它的位置。
关于swift - removeFromParent() 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49029726/
我有一个方法可以创建一条应该增加分数的线,并且该方法会间隔调用,只要有两条或更多条相同的线,只要英雄接触到离他最近的线,我就会运行removeFromParent Action 和最远的线被移除。但是
我对函数 removeFromParent 的行为真的很奇怪 lazy var buttonAds: SKSpriteNode = { let n = SKSpriteNode(imageName
我正在为我的一个项目创建一个 JTree,一些节点的名称会动态更改,并且我正在使用此函数更新树: public void updateTree() { for (Sprite spr : sp
我在我的 GameScene 中的随机位置加载了多个 SpriteNode,但实际上是同一个 SpriteNode 添加了多次。我在 touchesEnded 中有一个函数,一旦在与 SpriteNo
我想在我点击场景中的按钮时删除 Sprite 。该按钮仅在玩家与元素发生碰撞时启用。这是添加和删除 Sprite 的代码: var imageShown = false var imageForRid
我注意到我的 Swift 项目中有一个奇怪的行为,并以这种方式在一个空的 SpriteKit 项目中重现了它: class GameScene: SKScene { override func
我的 SpriteKit 游戏中有一个随机生成对象副本的循环,我想使用 removeFromParent() 来删除接触到的对象。但是,当我将它添加到代码中时,该对象并没有被删除。这是生成我的对象的循
阅读 Apple 关于 removeFromParent 的文档说: “从其父节点中删除接收节点。” 这是否意味着节点被销毁了?我是否需要将节点设置为 NULL 以释放其内存? 最佳答案 当没有强引用
我的应用程序根据传入事件动态创建多个 SKNode 和 SKSpriteNode。当我尝试清理过时的节点及其包含的子节点时,我使用node.removeFromParent()。 但是,一旦我尝试在所
我有一个 for 循环实例化 5 个 Sprite for enemy in 1...5 { negativeOnComing = SKSpriteNode(imageNamed: "Enem
在 Swift 中有什么方法可以告诉我什么时候 SKSpriteNode 实际上已经从场景中移除了吗?我不认为它实际上是在调用 removeFromParent 时完成的,但我认为它是稍后完成的,当
subview Controller 可以“解雇自己”吗? ..... 你有一个 View Controller “RedMessage”。你有一个普通的 strong 属性...... @prope
在下面的代码中,如果我按下 removeButton 按钮会消失,但它仍然有效。也许我应该让 if 语句为假,但我不知道该怎么做。 class GameScene : SKScene { va
在我的游戏中,我希望每次我的玩家触摸一个物体时,屏幕都会变白约 0.5 秒,然后恢复正常。 我的代码是: private var crash: SKSpriteNode? func crashadd(
方法 .removeFromParent() 不会移除 Sprite 。怎么了? override func touchesBegan(touches: Set, withEvent event: U
我有一个 SKShapeNode 和一个子 SKEmitterNode。我附加了一个 SKAction 序列,其中最后一个操作是 removeFromParent。节点在没有发射器的情况下表现正确,执
我已将 iPad Air 更新到 7.1,将 Xcode 更新到 5.1。 Xcode 想将我的项目更新为推荐的设置,我同意了。 之后,当我从父节点中删除节点时,我的游戏开始在几个地方崩溃。 这对我来
本文整理了Java中com.ait.lienzo.client.core.shape.wires.WiresShape.removeFromParent()方法的一些代码示例,展示了WiresShap
我有一个 test.c 文件,其中有这个函数调用: functiontest(2,x); 我想删除这个函数调用(通过 llvm 传递),当我尝试像这样使用 removeFromParent() 函数时
我是一名优秀的程序员,十分优秀!