我试图让一个分数标签在我的游戏中起作用,我希望每当"英雄"时,分数会增加1。通过"墙"我已经得到了它,所以数字0在得分所在的角落里是最高的,当我开始游戏时,它会计算但速度太快。因为在墙上甚至还没到,我已经在示例中:67分。关于如何获得它的任何建议,以便当英雄通过墙壁时它给出1点或者如果墙壁到达点x我得到1点?
它有点像飞鸟!
这是代码
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var movingGround: CSMovingGround!
var hero: CSHero!
var cloudGenerator: CSCloudGenerator!
var wallGenerator: CSWallGenerator!
var wall: CSWall!
var scoreLabel = UILabel()
var score = Int()
var isStarted = false
var heroCategory: UInt32 = 1<<1
var wallCategory: UInt32 = 1<<2
var groundCategory: UInt32 = 1<<2
var invisCategory: UInt32 = 1<<3
let walls = CSWall()
override func didMoveToView(view: SKView) {
backgroundColor = UIColor(red: 159.0/255.0, green: 201.0/255, blue: 244.0/255.0, alpha: 1.0)
/*
let backgroundTexture = SKTexture(imageNamed: "background.png")
let backgroundImage = SKSpriteNode(texture: backgroundTexture, size: view.frame.size)
backgroundImage.position = view.center
addChild(backgroundImage)
*/
// add ground
movingGround = CSMovingGround(size: CGSizeMake(view.frame.width, kCSGroundHeight))
movingGround.position = CGPointMake(0, view.frame.size.height/2)
self.addChild(movingGround)
// add hero
hero = CSHero()
hero.position = CGPointMake(70, movingGround.position.y + movingGround.frame.size.height/2 + hero.frame.size.height/2)
hero.physicsBody = SKPhysicsBody(rectangleOfSize: hero.size)
hero.physicsBody?.dynamic = true
hero.physicsBody?.allowsRotation = false
hero.physicsBody!.collisionBitMask = heroCategory | wallCategory
hero.physicsBody!.contactTestBitMask = wallCategory | heroCategory | groundCategory
self.addChild(hero)
hero.breathe()
// add cloud generator
cloudGenerator = CSCloudGenerator(color: UIColor.clearColor(), size: view.frame.size)
cloudGenerator.position = view.center
addChild(cloudGenerator)
cloudGenerator.populate(7)
cloudGenerator.startGeneratingWithSpawnTime(5)
// add wall generator
wallGenerator = CSWallGenerator(color: UIColor.clearColor(), size: view.frame.size)
wallGenerator.position = view.center
wallGenerator.physicsBody = SKPhysicsBody(edgeLoopFromRect : wallGenerator.frame)
wallGenerator.physicsBody?.dynamic = false
wallGenerator.physicsBody?.categoryBitMask = wallCategory
wallGenerator.physicsBody!.collisionBitMask = wallCategory | heroCategory | invisCategory
wallGenerator.physicsBody!.contactTestBitMask = invisCategory
self.addChild(wallGenerator)
let ground1 = SKSpriteNode(color: UIColor.clearColor(), size: CGSizeMake(view.frame.size.width, 20))
ground1.position = view.center
ground1.physicsBody = SKPhysicsBody(rectangleOfSize: ground1.size)
ground1.physicsBody!.dynamic = false
ground1.physicsBody!.affectedByGravity = false
ground1.physicsBody!.categoryBitMask = groundCategory
ground1.physicsBody!.collisionBitMask = groundCategory | heroCategory
self.addChild(ground1)
let ground2 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(view.frame.size.width, 20))
ground2.position = CGPointMake(284, 98)
ground2.physicsBody = SKPhysicsBody(rectangleOfSize: ground2.size)
ground2.physicsBody!.dynamic = false
ground2.physicsBody!.affectedByGravity = false
ground2.physicsBody!.categoryBitMask = groundCategory
ground2.physicsBody!.collisionBitMask = groundCategory | heroCategory
self.addChild(ground2)
let ground3 = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(20, 500))
ground3.position = CGPointMake(100, 100)
ground3.physicsBody = SKPhysicsBody(rectangleOfSize: ground3.size)
ground3.physicsBody?.dynamic = false
ground3.physicsBody?.categoryBitMask = invisCategory
ground3.physicsBody!.collisionBitMask = invisCategory | wallCategory
ground3.physicsBody!.contactTestBitMask = wallCategory
self.addChild(ground3)
physicsWorld.contactDelegate = self
scoreLabel.text = "\(score)"
scoreLabel = UILabel(frame: CGRect(x: 10, y: 10, width: 100, height: 20))
scoreLabel.backgroundColor = UIColor(red: 0.6, green: 0.1, blue: 0.1, alpha: 0)
scoreLabel.textColor = UIColor.blackColor()
self.view?.addSubview(scoreLabel)
}
func start() {
isStarted = true
hero.stop()
hero.startRunning()
movingGround.start()
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
if !isStarted {
start()
score = 0
scoreLabel.text = "0"
wallGenerator.startGeneratingWallsEvery(0.5)
} else {
hero.flip()
}
}
func addScore() {
score++
scoreLabel.text = "\(score)"
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == invisCategory && secondBody.categoryBitMask == wallCategory || firstBody.categoryBitMask == wallCategory && secondBody.categoryBitMask == invisCategory {
self.addScore()
}
if (firstBody.categoryBitMask & UInt32(heroCategory)) != 0 && (secondBody.categoryBitMask & UInt32(wallCategory)) != 0 {
wallGenerator.removeFromParent()
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameOverScene(size: self.size, won: false)
self.view?.presentScene(scene, transition: reveal)
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
CSWall代码(墙)
import Foundation
import SpriteKit
class CSWall: SKSpriteNode {
let WALL_WIDTH: CGFloat = 30.0
let WALL_HEIGHT: CGFloat = 50.0
let WALL_COLOR = UIColor.blackColor()
override init() {
super.init(texture: nil, color: WALL_COLOR, size: CGSizeMake(WALL_WIDTH, WALL_HEIGHT))
startMoving()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func startMoving() {
let moveLeft = SKAction.moveByX(-300, y: 0, duration: 1)
runAction(SKAction.repeatActionForever(moveLeft))
}
}
在游戏中生成墙的代码
import SpriteKit
class CSWallGenerator: SKSpriteNode {
var generationTimer: NSTimer?
func startGeneratingWallsEvery(seconds: NSTimeInterval) {
generationTimer = NSTimer.scheduledTimerWithTimeInterval(seconds, target: self, selector: "generateWall", userInfo: nil, repeats: true)
}
func generateWall() {
var scale: CGFloat
let rand = arc4random_uniform(2)
if rand == 0 {
scale = -1.0
} else {
scale = 1.0
}
let wall = CSWall()
wall.position.x = size.width/2 + wall.size.width/2
wall.position.y = scale * (kCSGroundHeight/2 + wall.size.height/2)
wall.physicsBody = SKPhysicsBody(rectangleOfSize: wall.size)
wall.physicsBody?.dynamic = false
addChild(wall)
}
}