ios – 如何使对象(SKSprite节点)在单击时消失

ios – 如何使对象(SKSprite节点)在单击时消失,第1张

概述我希望点击对象节点消失.对象在屏幕上移动,当您单击屏幕上的任何位置时,当前将被删除.但是,我希望仅在单击对象时才删除它. 代码如下: import SpriteKitimport GameplayKitstruct Physics { static let Enemy: UInt32 = 0x1 << 1 let BorderCategory : UInt32 = 0x1 我希望点击对象时节点消失.对象在屏幕上移动,当您单击屏幕上的任何位置时,当前将被删除.但是,我希望仅在单击对象时才删除它.

代码如下:

import SpriteKitimport GameplayKitstruct Physics {    static let Enemy: UInt32 = 0x1 << 1    let bordercategory : UInt32 = 0x1 << 2    let Bottomcategory : UInt32 = 0x1 << 3    let Ballcategory   : UInt32 = 0x1 << 4}class GameScene: SKScene {    var Enemy = SKSpriteNode()    var gameStarted = Bool()    var gameState = "running"    var destX : CGfloat = 0.0    var destY : CGfloat = 0.0    var score = 0    overrIDe func dIDMove(to vIEw: SKVIEw) {        let borderBody = SKPhysicsBody(edgeLoopFrom: self.frame)        borderBody.friction = 0        self.physicsBody = borderBody        Enemy.name = "Enemys"        Enemy = SKSpriteNode(imagenamed: "red2")        Enemy.size = CGSize(wIDth: 60,height: 70)        Enemy.position = (CGPoint(x: self.frame.wIDth / 6 - Enemy.frame.wIDth,y: self.frame.height / 10))        Enemy.physicsBody = SKPhysicsBody(circleOfRadius: Enemy.frame.height / 2)        Enemy.physicsBody?.categoryBitMask = Physics.Enemy        //Enemy.physicsBody?.categoryBitMask = Physics.Ground | Physics.wall        //Enemy.physicsBody?.contactTestBitMask = Physics.Ground | Physics.wall        Enemy.physicsBody?.affectedByGravity = false        Enemy.physicsBody?.isDynamic = true        Enemy.physicsBody?.restitution = 1        Enemy.physicsBody?.friction = 0        Enemy.physicsBody?.angulardamPing = 0        Enemy.physicsBody?.lineardamPing = 0        self.addChild(Enemy)        let force = SKAction.applyForce(CGVector(dx: 300,dy: 300),duration: 0.1)        Enemy.run(force)    }    overrIDe func touchesBegan(_ touches: Set<UItouch>,with event: UIEvent?) {        let touch = touches.first as UItouch!        let touchLocation = touch?.location(in: self)        let targetNode = atPoint(touchLocation!) as! SKSpriteNode        if(targetNode.name == "Enemys"){        Enemy.removeFromParent()        }    }
解决方法 我个人发现处理这个问题的最简单方法是将SKSpriteNode子类化,并深入研究它是UIResponder的后代.

class AlIEnsprite: SKSpriteNode {    overrIDe func touchesBegan(_ touches: Set<UItouch>,with event: UIEvent?) {        self.removeFromParent()    }}

您还需要在某个实例上设置isUserInteractionEnabled = true.

总结

以上是内存溢出为你收集整理的ios – 如何使对象(SKSprite节点)在单击时消失全部内容,希望文章能够帮你解决ios – 如何使对象(SKSprite节点)在单击时消失所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1003030.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存