使用 UITouch 快速删除图像的语法

Posted

技术标签:

【中文标题】使用 UITouch 快速删除图像的语法【英文标题】:swift syntax deleting an image with UITouch 【发布时间】:2016-05-22 03:05:02 【问题描述】:

这基本上是我试图用来生成图像或敌人的代码,我想删除它们,但不是通过仅触摸被触摸的相同图像之一来一次性删除它们。图像也在移动,以防有人需要知道。

import SpriteKit
import UIKit 

class GameScene: SKScene 
    override func didMoveToView(view: SKView) 
         let myLabel = SKLabelNode(fontNamed:"chalkduster ")
         myLabel.text = "HELLO WORLD"
         myLabel.fontsize = 45
         myLabel.position = CGPoint(x:CGRectGetMidx(self.frame), y:CGRectGetMidy(self.frame)) 
       self.addChild(myLabel) 
    
    func SpawnEnemies()
         let Enemy = SKSpriteNode(imageNamed: "Enemy.png")
         let MinValue = self.size.width /8 
         let MaxValue = self.size.width -158
         let spawnPoint = UInt32(MaxValue- MinValue)
         Enemy.runAction(SKAction.sequence([action, actionDone])) 
         self.addChild(Enemy)
    
    func touchesEnded(touches: NSSet, withEvent event: UIEvent?) 
        for touch in touches  
           _ = touch.locationInNode(self)
           let touch = touches.anyobject() as! UITouch? 
           if let location = touch?.locationInNode(self)
            
              for _ in self.nodeAtPoint(location) 
              
                  if let Enemy.name == (name., "SpawnEnemies" 
                          Enemy.removeFromParent()
                  
              
           
         
    
    func update(currentTime: CFTimeInterval) 
    

【问题讨论】:

【参考方案1】:

通常使用大写的属性名称被认为是一种不好的态度,你应该使用敌人而不是敌人

试试这个方法:

  override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) 

        /* Called when a touch begins */

        let touch = touches.first
        let positionInScene = touch!.locationInNode(self)

        let touchedNode = self.nodeAtPoint(positionInScene)

        if let name = touchedNode.name
        
            if name == "SpawnEnemies1"  // try to get a different name for each of your enemy
                Enemy.removeFromParent()
             else
            if name == "SpawnEnemies666"  // this is the big boss
                // do some awesome animation..
                Enemy.removeFromParent()
             else
            if name == "title"
            
                print("title touched")
                // do whatever you want with title, removing or using it
             else
            if name == "credits"
            
                print("credits touched")
             else
            ...
        

【讨论】:

以上是关于使用 UITouch 快速删除图像的语法的主要内容,如果未能解决你的问题,请参考以下文章

何时使用 UITouch 与 UIScroll

UITouch 不适用于 ios8

让 UITouch 仅在触摸特定颜色时执行任务

删除 UISlider 旋钮或快速更改图像

带有 UIGestureRecognizer 的 UITouch 事件

如何在 iOS 上从 UITouch 找到最近的车站点