I added Score Label in my game. But It doesn't run. When I touch the ball, Score should be increase. I couldn't connect ball with score label. Can anyone help me? Here is my code :
我在游戏中添加了Score Label。但它没有运行。当我触球时,分数应该增加。我无法用分数标签连接球。谁能帮我?这是我的代码:
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
borderBody.friction = 0
self.physicsBody = borderBody
physicsWorld.gravity = CGVectorMake(5, 5)
let ball = childNodeWithName(BallCategoryName) as! SKSpriteNode
ball.physicsBody!.applyImpulse(CGVectorMake(10, -10))
scoreLabel.fontName = "Helvetica Neue Light"
scoreLabel.text = "Score: 0"
scoreLabel.fontSize = 40
scoreLabel.color = SKColor.redColor()
scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left
scoreLabel.verticalAlignmentMode = SKLabelVerticalAlignmentMode.Top
scoreLabel.position = CGPoint(x: 250, y: size.height - 100)
addChild(scoreLabel)
}
And there is touch code :
并且有触摸代码:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch:AnyObject in touches{
let location = (touch as! UITouch).locationInNode(self)
let nodeTouched = nodeAtPoint(location)
let ball = childNodeWithName(BallCategoryName) as! SKSpriteNode
//Detect if the ball is touched
if(nodeTouched == ball){
physicsWorld.gravity = CGVectorMake(-30, 30)
}
}
1 个解决方案
#1
1
Try this instead:
试试这个:
if ((nodeTouched.name?.containsString(BallCategoryName)) != nil) {
//Update Score
}
Keep in mind that we don't know exactly what BallCategoryName is or how you set it up, so you have to make sure it isn't nil.
请记住,我们并不确切知道BallCategoryName是什么或如何设置它,所以你必须确保它不是零。
Also, if your current method in touchesBegan
is running your physicsWorld.gravity = CGVectorMake(-30, 30)
line successfully, then we would know that something is wrong with your scoreLabel
property and not the touch detection method.
另外,如果你在touchesBegan中的当前方法成功运行了physicsWorld.gravity = CGVectorMake(-30,30),那么我们就会知道你的scoreLabel属性出了问题,而不是触摸检测方法。
If it was me, I would just make ball
a property of your scene and then test it like so in touchesBegan
:
如果是我,我会把球作为你场景的属性然后在touchesBegan中进行测试:
if self.ball.containsPoint(touchLocation) {
//Update Score
}
#1
1
Try this instead:
试试这个:
if ((nodeTouched.name?.containsString(BallCategoryName)) != nil) {
//Update Score
}
Keep in mind that we don't know exactly what BallCategoryName is or how you set it up, so you have to make sure it isn't nil.
请记住,我们并不确切知道BallCategoryName是什么或如何设置它,所以你必须确保它不是零。
Also, if your current method in touchesBegan
is running your physicsWorld.gravity = CGVectorMake(-30, 30)
line successfully, then we would know that something is wrong with your scoreLabel
property and not the touch detection method.
另外,如果你在touchesBegan中的当前方法成功运行了physicsWorld.gravity = CGVectorMake(-30,30),那么我们就会知道你的scoreLabel属性出了问题,而不是触摸检测方法。
If it was me, I would just make ball
a property of your scene and then test it like so in touchesBegan
:
如果是我,我会把球作为你场景的属性然后在touchesBegan中进行测试:
if self.ball.containsPoint(touchLocation) {
//Update Score
}