代码如下:
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节点)在单击时消失所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)