2016-05-22 4 views
0

これは基本的に画像や敵を生成しようとしているコードで、タッチした同じ画像の1つだけを一度にタッチして削除するだけです。誰かが知る必要がある場合に備えて、画像も動きます。通常のプロパティに大文字を使用してswift syntax UITouchで画像を削除する

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) { 
    } 

答えて

0

それは悪い態度として考えるの名前、あなたの代わりに敵

の敵を使用する必要があります

は、このアプローチを試してみてください:

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 
      ... 
     } 
} 
関連する問題