我正在创建一个游戏,其中随机形状下降并且每个形状都有数字,并且该数字表示用户必须对图像执行的点击次数,因此图像将被删除并且分数将增加1,但是我在创建图像时遇到问题,当我运行它时,一些图像工作正常,当我点击它的次数时,它会像我想要的那样被删除,但是当同一图像时有时创建,当我点击它时,它打印出一个msg,我已经把“用户没有点击图像”,我已经把我的默认设置放在touchEnded方法的开关stattment中,如下所示:
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
switch(nodeAtPoint(location)){
case self.circle:
if circleCount == 1{
circle.removeFromParent()
println("score")
circleCount = 0
}
case self.rectangle:
if rectangleCount == 2{
println("score")
rectangle.removeFromParent()
rectangleCount = 0
}
case self.square:
if squareCount == 3{
println("score")
square.removeFromParent()
squareCount = 0
}
case self.polygon:
if polygonCount == 4{
println("score")
polygon.removeFromParent()
polygonCount = 0
}
default:
println("user didnt click on an image")
}
}
}
和那是显示其中一个图像的功能之一:
func createRectangle(a: CGFloat){
rectangle = SKSpriteNode()
rectangle = SKSpriteNode(imageNamed: "rectangle")
rectangle.size = CGSize(width: 150, height: 150)
rectangle.physicsBody = SKPhysicsBody(rectangleOfSize: rectangle.size)
rectangle.physicsBody?.dynamic = true
rectangle.physicsBody?.allowsRotation = false
rectangle.position = CGPoint(x: a, y: self.frame.size.height)
self.addChild(rectangle)
}
并且有一个函数可以生成随机数以随机显示形状。
答案 0 :(得分:1)
查看此更新代码:
import UIKit
import SpriteKit
import Darwin
var squareCount = 0
var circleCount = 0
var rectangleCount = 0
var polygonCount = 0
var x = 0
class StartGame: SKScene {
var scoreLabel = SKLabelNode(fontNamed: "cholkDuster")
let bg = SKSpriteNode(imageNamed: "background")
var score = 0
override func didMoveToView(view: SKView) {
addBackGround()
addScoreLabel()
//random number for the shapes
runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(initiateShapes), SKAction.waitForDuration(1.0)])))
self.physicsWorld.gravity = CGVectorMake(0, -0.5)
}
func addScoreLabel(){
scoreLabel.zPosition = 0
self.scoreLabel.text = "0"
self.scoreLabel.fontSize = 42
self.scoreLabel.position = CGPoint(x: CGRectGetMidX(self.frame) , y: CGRectGetMidY(self.frame))
self.addChild(scoreLabel)
}
func addBackGround() {
//background image
bg.position = CGPoint(x: CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
bg.size.width = self.frame.size.width
bg.size.height = self.frame.size.height
bg.zPosition = -10
self.addChild(bg)
}
func initiateShapes() {
let shapeArray = ["circle", "rectangle", "square", "polygon"]
x = Int (arc4random_uniform(4))
let shape = SKSpriteNode(imageNamed: shapeArray[x])
shape.name = shapeArray[x]
shape.size = CGSize(width: 150, height: 150)
let actualX = random(min: shape.size.height/2, max: size.width - shape.size.height/2)
shape.position = CGPoint(x: actualX, y: size.height + shape.size.width / 2)
shape.zPosition = 10
addChild(shape)
let actualDuration = random(min: CGFloat(2.0), max: CGFloat(4.0))
let actionMove = SKAction.moveTo(CGPoint(x: actualX, y: -shape.size.width / 2), duration: 5.1)
let actionMoveDone = SKAction.removeFromParent()
shape.runAction(SKAction.sequence([actionMove, actionMoveDone]))
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
score++
if let touch = touches.first as? UITouch {
let touchLocation = touch.locationInNode(self)
if nodeAtPoint(touchLocation).name == "circle" {
nodeAtPoint(touchLocation).removeFromParent()
} else if nodeAtPoint(touchLocation).name == "rectangle" {
nodeAtPoint(touchLocation).removeFromParent()
} else if nodeAtPoint(touchLocation).name == "square" {
nodeAtPoint(touchLocation).removeFromParent()
} else if nodeAtPoint(touchLocation).name == "polygon"{
nodeAtPoint(touchLocation).removeFromParent()
} else {
println("Nothing")
}
scoreLabel.text = "\(score)"
}
}
func random() -> CGFloat {
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}
func random(#min: CGFloat, max: CGFloat) -> CGFloat {
return random() * (max - min) + min
}
func randomInt(min: Int, max:Int) -> Int {
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
}
}