Swift& Scort的评分方法产卵物体

时间:2015-01-21 16:41:30

标签: ios swift timer collision scoring

我似乎无法弄清楚如何让我的得分发挥作用......

游戏的概念是鸟类将在右侧进入,当它们到达左侧时我希望我的分数增加1。

在Obj_c中,我会做类似

的事情
if (bird.enter.x < 0) {
    score++
}

但是在快速精灵套件中,我不确定该怎么做......

我遇到的另一个问题是我必须使用计时器来获取我的敌人#34;产卵(鸟从右到左)

(计时器运行产生的敌人功能每1秒钟)但是在Obj_c中,我会在不同的x坐标处产生说3/4,并且当鸟类是&lt; 0 px我会CGPointMake()回到屏幕的另一侧,所以它们处于一个恒定的循环

快速做到这一点的任何想法?我的代码如下:它有点奇怪的布局:

import SpriteKit





class GameScene: SKScene, SKPhysicsContactDelegate {

var bg = SKSpriteNode()             // Create Background
var bird = SKSpriteNode()           // Create User Bird
var scoreLabel = SKLabelNode()      // Create Score Label
var score: Int = 0                  // Create Score Integer
var enemyBird = SKSpriteNode()      // Create Enemy Bird

var birdGroup:UInt32 = 1            // Bird Collision Group
var objectGroup:UInt32 = 2          // Enemy Collision Group
var scoreGroup:UInt32 = 3           // Score Collision Group
var gameOver = 0                    // Game Over function
var movingObjects = SKNode()        // ??
var gameOverLabel = SKLabelNode()   // Game over label
var labelHolder = SKSpriteNode()    // Holds Label - Game Over
var Menu = 0


/* Put Bird in and animate */
var birdTexture = SKTexture(imageNamed: "bird_img_1.png")
var birdTexture2 = SKTexture(imageNamed: "bird_img_2.png")
var birdTexture3 = SKTexture(imageNamed: "bird_img_3.png")
var birdTexture4 = SKTexture(imageNamed: "bird_img_4.png")

var birdDeadTexture = SKTexture(imageNamed: "bird_img_dead.png")

var finalSpawn: CGFloat = 0.0


/* ------------------------------------ Main Setup ------------------------------------------ */


override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    println("Moved to Game Scene")


    movingObjects.speed = 0




    /* Call Functions */
    backgroundImage()
    enemySpawn()



    /* Set up deletates and physics and timer */
    self.physicsWorld.contactDelegate = self
    self.physicsWorld.gravity = CGVectorMake(0, -5)
    self.addChild(movingObjects)
    self.addChild(labelHolder)


    /*  Load Score Text */

    scoreLabel.fontName = "Helvetica"
    scoreLabel.fontSize = 60
    scoreLabel.text = "0"
    scoreLabel.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)+self.frame.size.height*0.3)
    self.addChild(scoreLabel)


    enemyBird.position = CGPointMake(self.frame.width+200, 500)
    movingObjects.addChild(enemyBird)



    bird = SKSpriteNode(texture: birdTexture)
    bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    // Make bird smaller
    bird.size.height = bird.size.height/12
    bird.size.width = bird.size.width/11
    // Animate Bird
    var animation = SKAction.animateWithTextures([birdTexture, birdTexture2, birdTexture3, birdTexture4], timePerFrame: 0.08)
    var makeBirdFlap = SKAction.repeatActionForever(animation)
    bird.runAction(makeBirdFlap)
    // Load Physics
    // birdPhysics()
    // Run bird animation
    bird.zPosition = 10


    self.addChild(bird)


    /* Introduce ground and top into the scene */
    // Create Ground
    var ground = SKSpriteNode()
    ground.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height/100 * 16)
    ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, 1))
    ground.physicsBody?.dynamic = false
    ground.physicsBody?.categoryBitMask = objectGroup
    // Add Ground to scene
    self.addChild(ground)

    // Create Top
    var top = SKSpriteNode()
    top.position = CGPointMake(0, self.frame.size.height)
    top.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, 1))
    top.physicsBody?.dynamic = false
    top.physicsBody?.categoryBitMask = objectGroup
    // Add Top to scene
    self.addChild(top)


}








/* ------------------------------------ Spawning Enemy Birds ------------------------------------------ */








/* Create and Spawn enemy birds */
func enemySpawn() {





    let height = self.frame.size.height                             // screen height as variable
    let width = self.frame.size.width                               // screen width as variable

    var bottomBarHeight = self.frame.size.height*0.16               // bottom bar height
    var spawningPoint = height - bottomBarHeight                    // screen height - 16%

    var randSpawn = arc4random_uniform(UInt32(spawningPoint))       // random number between 0 and 645
    finalSpawn = CGFloat(randSpawn) + bottomBarHeight           // add 16% (bottombarheight to the random number
    println(Int(finalSpawn))





    if (enemyBird.position.y > frame.size.height) {
        enemyBird.position.y = frame.size.height - 100
    }

    if (enemyBird.position.y < self.frame.size.width * 0.16) {
        enemyBird.position.y = self.frame.size.width + 0.18
    }




   // Create enemy bird
    var enemyBirdTexture = SKTexture(imageNamed: "enemy_img_2.png")

   // enemyBird.position = CGPointMake(self.frame.width+200, 500)
    enemyBird = SKSpriteNode(texture: enemyBirdTexture)
    enemyBird.size.height = enemyBird.size.height/12
    enemyBird.size.width = enemyBird.size.width/11
    enemyBird.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(enemyBird.size.width/1.1, enemyBird.size.height/1.1))
    enemyBird.physicsBody?.dynamic = false
    enemyBird.physicsBody?.categoryBitMask = objectGroup

   // BIRD LOOP - ONLY RUNNING ONCE???        

    let startPoint = CGPointMake(self.frame.width+200, finalSpawn)
    let endPoint = CGPointMake(0, finalSpawn)
    let moveToEndAction = SKAction.moveTo(endPoint, duration: 2)
    let resetTostartAction = SKAction.moveTo(startPoint, duration: 0)
    let moveToEndThenStartAgain = SKAction.repeatActionForever(SKAction.sequence([moveToEndAction,resetTostartAction]))

    enemyBird.runAction(SKAction.repeatActionForever(moveToEndThenStartAgain))

}











/* ------------------------------------ Other Parts ------------------------------------------ */








func backgroundImage() {

    /* Put Background Image In */

    // Create Background Texture
    var bgTexture = SKTexture(imageNamed: "bg.png")
    // Link bg variable to texture
    bg = SKSpriteNode(texture: bgTexture)
    // Position thr bg image
    bg.position = CGPoint(x: CGRectGetMidX(self.frame), y: self.frame.height/2)
    bg.size.height = self.frame.height

    // Move background image left
    var moveBg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 9)
    var replaceBg = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
    var moveBgForever = SKAction.repeatActionForever(SKAction.sequence([moveBg, replaceBg]))

    // Keep world never ending
    for var i:CGFloat = 0; i < 3; i++ {

        // Position Background
        bg = SKSpriteNode(texture: bgTexture)
        bg.position = CGPoint(x: bgTexture.size().width/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))

        // Stretch background full height of screen
        bg.size.height = self.frame.height

        // Run the action to move BG
        bg.runAction(moveBgForever)

        self.addChild(bg)
    }
}










/* ------------------------------------ Create Bird Physics ------------------------------------------ */








func birdPhysics() {
    /* Give bird physics */
    // Give bird physics
    bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height/3)
    bird.physicsBody?.mass = 0.2
    bird.physicsBody?.dynamic = true
    bird.physicsBody?.allowsRotation = false
    bird.physicsBody?.categoryBitMask = birdGroup
    bird.physicsBody?.collisionBitMask = objectGroup
    bird.physicsBody?.contactTestBitMask = objectGroup


}









/* ------------------------------------ If contact is found ------------------------------------------ */










func didBeginContact(contact: SKPhysicsContact) {
    if contact.bodyA.categoryBitMask == objectGroup || contact.bodyB.categoryBitMask == objectGroup {


        bird.removeAllActions()
        bird.texture = birdDeadTexture
        let action = SKAction.rotateByAngle(CGFloat(-M_PI), duration:1)
        bird.runAction(SKAction.repeatActionForever(action))


        if gameOver == 0 {
            movingObjects.speed = 0
            gameOver = 1
            movingObjects.removeAllChildren()// Remove all enemies
            gameOverLabel.fontName = "Helvetica"
            gameOverLabel.fontSize = 25
            gameOverLabel.text = "Tap to retry!"
            gameOverLabel.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)*1.5)
            labelHolder.addChild(gameOverLabel)
            gameOverLabel.zPosition = 9


        }
    }
}












/* ------------------------------------ When user touches the screen ------------------------------------------ */






override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */




    if (Menu == 0) {
        movingObjects.speed = 1
        birdPhysics()
        let action = SKAction.rotateByAngle(CGFloat(-M_PI), duration:1)
        bird.runAction(SKAction.repeatActionForever(action))

    }




    if (gameOver == 0) {                                                                            // Runs if game is not over
        bird.physicsBody?.velocity = CGVectorMake(0, 0)
        bird.physicsBody?.applyImpulse(CGVectorMake(0, 80))
        Menu = 1    // Number on right is jump height


    } else {                                                                                        // Runs if game is over
        score = 0                                                                                   // Score int is 0
        scoreLabel.text = "0"                                                                       // Score Label is 0
        bird.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))         // Position Bird in center
        bird.physicsBody?.velocity = CGVectorMake(0,0)                                              // Cannot make bird jump
        labelHolder.removeAllChildren()                                                             // Removes all labels
        gameOver = 0                                                                                // Sets game over to 0 so game will run
        movingObjects.speed = 1
        bird.texture = birdTexture
        // Animate Bird
        var animation = SKAction.animateWithTextures([birdTexture, birdTexture2, birdTexture3, birdTexture4], timePerFrame: 0.08)
        var makeBirdFlap = SKAction.repeatActionForever(animation)
        bird.runAction(makeBirdFlap)

        let height = self.frame.size.height
        let width = self.frame.size.width



            var gameScene: GameScene = GameScene(size: CGSizeMake(width, height))
            var spriteView: SKView = self.view as SKView!
            var trans :SKTransition = SKTransition.crossFadeWithDuration(0.5)
            spriteView.presentScene(gameScene, transition: trans)

    }
}







override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

    if (enemyBird.position.x == 0) {

        score = score + 1
        scoreLabel.text = "\(score)"
    }


    if (enemyBird.position.y > frame.size.height) {
        enemyBird.position.y = frame.size.height - enemyBird.size.height
    }  
}    }

1 个答案:

答案 0 :(得分:2)

你可以像

一样增加分数
if (bird.position.x < 0)
{
    score++
}

要循环敌人的动画,你可以从一端到另一端循环SKAction。例如像这样

let startPoint = CGPointMake(0, 200)
let endPoint = CGPointMake(self.frame.width, 200)

let moveToEndAction = SKAction.moveTo(endPoint, duration: 2)
let resetTostartAction = SKAction.moveTo(startPoint, duration: 0)

let moveToEndThenStartAgain = SKAction.repeatActionForever(SKAction.sequence([moveToEndAction,resetTostartAction]))

node.runAction(SKAction.repeatActionForever(moveToEndThenStartAgain))

每次以随机高度开始,您可以使用

let maxHeight:UInt32 = 400
let startPoint = CGPointMake(0, CGFloat(arc4random() % maxHeight))

let moveToEndAction = SKAction.moveByX(self.frame.width, y: 0, duration: 1.0)
let resetTostartAction = SKAction.runBlock { () -> Void in

    let randomHeight = CGFloat(arc4random() % maxHeight)
    self.playButton.position = CGPointMake(startPoint.x,randomHeight)
}

let moveToEndThenStartAgain = SKAction.repeatActionForever(SKAction.sequence([moveToEndAction,resetTostartAction]))

enemyBird.runAction(SKAction.repeatActionForever(moveToEndThenStartAgain))

您可以根据需要更改maxHeight