https://drive.google.com/file/d/1SA35OP-eGri41LxABmaJlw3196K5NRDX/view?usp=drivesdk
我用For循环创建了四个NSSpriteNodes 但是,它们都与SKSpriteNode具有相同的名称,因此我将它们全部命名为一个循环。 当我根据它们的名称拖放时,它们都是分别拖放的,但是我可以在它们自己的字段中触摸。 如何分别拖放SKSpriteNode
// 1.代码-------------------------------
let touch:UITouch = touches.first!
let positionInScene = touch.location(in: self)
let touchedNode = self.atPoint(positionInScene)
for touch in touches
{
let konum = touch.location(in: self)
if let name = touchedNode.name
{
if name == "a"
{
cevapdolukutu.position.x = konum.x
cevapdolukutu.position.y = konum.y
}
}
}
var xPos = CGFloat(Double("115")!)
let cevapdoluNameArray = ["a","b","c","d","e","f","g","h"]
for i in 0..<randomharf.count {
let cevapdizi = randomharf[i]
self.cevapdolukutu = SKSpriteNode(imageNamed: "cevapdolukutu.png")
self.cevapdolukutu.position = CGPoint(x: xPos, y: self.size.height / 2.2)
self.cevapdolukutu.zPosition = 1
self.cevapdolukutu.setScale(0.25)
self.cevapdolukutu.anchorPoint = CGPoint(x: 0, y: 0)
self.cevapdolukutu.name = cevapdoluNameArray[i]
xPos += 130
self.addChild(self.cevapdolukutu)
self.cevapharf = SKLabelNode(fontNamed: "Avenir")
self.cevapharf.text = cevapdizi
self.cevapharf.fontSize = 70
self.cevapharf.zPosition = 2
self.cevapharf.fontColor = UIColor.white
self.cevapharf.verticalAlignmentMode = .center
self.cevapharf.horizontalAlignmentMode = .center
self.cevapharf.position = CGPoint(x: self.cevapdolukutu.frame.midX, y: self.cevapdolukutu.frame.midY)
self.addChild(self.cevapharf)
}
// 2.代码-------------------------------------
for touch in touches
{
let konum = touch.location(in: self)
cevapdolukutu.position.x = konum.x
cevapdolukutu.position.y = konum.y
}
var xPos = CGFloat(Double("115")!)
for i in 0..<randomharf.count {
let cevapdizi = randomharf[i]
self.cevapdolukutu = SKSpriteNode(imageNamed: "cevapdolukutu.png")
self.cevapdolukutu.position = CGPoint(x: xPos, y: self.size.height / 2.2)
self.cevapdolukutu.zPosition = 1
self.cevapdolukutu.setScale(0.25)
self.cevapdolukutu.anchorPoint = CGPoint(x: 0, y: 0)
xPos += 130
self.addChild(self.cevapdolukutu)
self.cevapharf = SKLabelNode(fontNamed: "Avenir")
self.cevapharf.text = cevapdizi
self.cevapharf.fontSize = 70
self.cevapharf.zPosition = 2
self.cevapharf.fontColor = UIColor.white
self.cevapharf.verticalAlignmentMode = .center
self.cevapharf.horizontalAlignmentMode = .center
self.cevapharf.position = CGPoint(x: self.cevapdolukutu.frame.midX, y: self.cevapdolukutu.frame.midY)
self.addChild(self.cevapharf)
}