任何人都可以帮助我,我试图制作一个简单的应用程序,你触摸屏幕,随机显示4个图像。问题是,当我点击屏幕时,我没有得到任何图像,我也没有错误。救命啊!
这是我的代码:
import SpriteKit
class GameScene: SKScene {
var num1 = SKSpriteNode(imageNamed: "num1")
var num2 = SKSpriteNode(imageNamed: "num2")
var num3 = SKSpriteNode(imageNamed: "num3")
var num4 = SKSpriteNode(imageNamed: "num4")
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVectorMake(0, -9.8)
num1.size = CGSize(width: 150, height: 150)
num2.size = CGSize(width: 150, height: 150)
num3.size = CGSize(width: 150, height: 150)
num4.size = CGSize(width: 150, height: 150)
let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
sceneBody.friction = 0
self.physicsBody = sceneBody
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
_ = touch.locationInNode(self)
let random = arc4random_uniform(4)
switch random {
case 0:
self.addChild(num1)
print("number 0")
case 1:
self.addChild(num2)
print("number 1")
case 2:
self.addChild(num3)
print("number 2")
default:
self.addChild(num4)
print("default")
}
}
}
}
答案 0 :(得分:1)
您没有设置每个精灵应该出现的位置
在每个self.addChild(...)
之前你应该执行类似这样的事情
numX.position = location
并重新排列你的touchesBegan方法
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
var location
for touch in touches {
location = touch.locationInNode(self)
}
let random = arc4random_uniform(4)
switch random {
case 0:
num1.position = location
self.addChild(num1)
print("number 0")
case 1:
num2.position = location
self.addChild(num2)
print("number 1")
case 2:
num3.position = location
self.addChild(num3)
print("number 2")
default:
num4.position = location
self.addChild(num4)
print("default")
}
}
答案 1 :(得分:0)
class GameScene: SKScene {
var handbag = SKSpriteNode(imageNamed: "HandBag")
var bible = SKSpriteNode(imageNamed: "Bible")
var watchtower = SKSpriteNode(imageNamed: "Watchtower")
var awake = SKSpriteNode(imageNamed: "Awake")
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVectorMake(0, -9.8)
handbag.size = CGSize(width: 150, height: 150)
bible.size = CGSize(width: 150, height: 150)
watchtower.size = CGSize(width: 150, height: 150)
awake.size = CGSize(width: 150, height: 150)
let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
sceneBody.friction = 0
self.physicsBody = sceneBody
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
}
let random = arc4random_uniform(4)
switch random {
case 0:
handbag.position = location
self.addChild(handbag)
print("number 0")
case 1:
bible.position = touch
self.addChild(bible)
print("number 1")
case 2:
watchtower.position = touch
self.addChild(watchtower)
print("number 2")
default:
awake.position = touch
self.addChild(awake)
print("default")
}
}
}