[英]Swift Sprite-Kit: How do I increase my game's score when screen is touched?
在我的游戏中,每次触摸会将背景向下移动50个像素,我希望将某个动画期间的每次触摸转换为50分。在我的代码中,我有两个背景节点,使我的背景在每次触摸时向下滚动而没有间隙在后台。 运行绿灯动画时更新分数时出现问题,而在红灯动画期间触摸屏幕时使游戏结束。 我已经创建了分数标签。 先感谢您。
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
if bg.position.y + bg.size.height/2 < 50
{
let diff = bg.position.y + bg.size.height/2 - 50
bg.position.y = self.frame.height + bg.size.height/2 + diff
}
else
{
bg.position.y -= 50
}
if bg2.position.y + bg2.size.height/2 < 50
{
let diff = bg2.position.y + bg2.size.height/2 - 50
bg2.position.y = self.frame.height + bg2.size.height/2 + diff
}
else
{
bg2.position.y -= 50
}
}
您可以为greenLightIsShowing和redLightIsShowing设置布尔值,并根据显示的时间将它们设置为true和false。 然后在bool为真时处理触摸。
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if greenLightIsShowing == true {
if nodeAtPoint(location) == self.view {
score = score + 50
scoreLable.text = "\(score)"
}
}else if redLightIsShowing == true {
if nodeAtPoint(location) == self.view {
//end the game
}
}
尝试这个
class GameScene: SKScene {
var currentLight : SKSpriteNode!
var greenTexture : SKTexture!
var redTexture : SKTexture!
var isGreenLightON : Bool = true
var score : Int = 0
override func didMoveToView(view: SKView) {
/* Setup your scene here */
greenTexture = SKTexture(imageNamed: "green light.png")
redTexture = SKTexture(imageNamed: "red light.png")
currentLight = SKSpriteNode(texture: greenTexture)
currentLight.position = CGPointMake(CGRectGetMidX(self.frame), 650)
let changeToGreenLight : SKAction = SKAction.runBlock({ () -> Void in
self.currentLight.texture = self.greenTexture
self.isGreenLightON = true
})
let changeToRedLight : SKAction = SKAction.runBlock({ () -> Void in
self.currentLight.texture = self.redTexture
self.isGreenLightON = false
})
let changingLights = SKAction.sequence([changeToGreenLight,SKAction.waitForDuration(1, withRange: 4),changeToRedLight,SKAction.waitForDuration(1, withRange: 4)])
currentLight.runAction(SKAction.repeatActionForever(changingLights))
self.addChild(currentLight)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
if isGreenLightON
{
score += 50
}
else
{
score -= 10
}
println(score)
scoreLabel.text = "\(score)"
}
override func update(currentTime: CFTimeInterval) {
// Your Code
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.