如何在任何用户交互之前使节点可见

时间:2015-08-19 13:23:41

标签: sprite-kit skspritenode

我采取并修改了一段代码,允许我向各个方向射击子弹。它完美地运作。但是,当玩家触摸结束时,它会产生一个随机的子弹。我有6个不同的子弹,为了我的游戏目的,我需要玩家看到之前他会点击屏幕。我尝试在全局函数中初始化项目符号并将其传递给didMoveToView。它不起作用,子弹产生一次并停留在它的位置。  这是我初始化子弹的代码:

class GameScene: SKScene, SKPhysicsContactDelegate {
var gameOver = false
let cannon = SKSpriteNode(imageNamed: "cannon")
let bulletInCannon = SKSpriteNode()
var endOfScreenRight = CGFloat()
var endOfScreenLeft = CGFloat()

let bmrArray = ["blBl", "magBl", "rBl"]   
let cgyArray = ["cBl", "gBl", "yBl"]   
let yrmArray = ["yBl", "rBl", "magBl"]   
let bulletArray = ["rBullet","magBullet", "blBullet", "cBullet", "gBullet", "yBullet"]

override func didMoveToView(view: SKView) {
 endOfScreenLeft = (self.size.width / 2) * CGFloat(-1)
 endOfScreenRight = self.size.width / 2

 cannon.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
 self.addChild(cannon)

 cannon.zPosition = 0

 var randomBullet = Int(arc4random_uniform(6))
 bulletInCannon.texture = SKTexture(imageNamed: bulletArray[randomBullet])
 bulletInCannon.position = cannon.position
 addChild(bulletInCannon)

 bulletInCannon.zPosition = 1000

 runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(addCgyLine), 
SKAction.waitForDuration(1.0)])))

}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        let bullet = SKSpriteNode(texture: bulletInCannon.texture)
        bullet.position = cannon.position
        addChild(bullet)

        let offset = location - bullet.position
        let direction = offset.normalized()
        let shootAmount = direction * 1000
        let realDestination = shootAmount + bullet.position

        let actionMove = SKAction.moveTo(realDestination, duration: 2.0)
        let actionDone = SKAction.removeFromParent()
        bullet.runAction(SKAction.sequence([actionMove, actionDone]))

        var randomBullet = Int(arc4random_uniform(6))
        bulletInCannon.texture = SKTexture(imageNamed: bulletArray[randomBullet])
        }
}


func addCgyLine () {
    var cgyBlock = SKSpriteNode(imageNamed: "cyanBox")
    cgyBlock.position = CGPointMake(size.width + cgyBlock.size.width/2, CGRectGetMidY(self.frame) + 60) 
    addChild(cgyBlock)
    var randomCGY = Int(arc4random_uniform(3))
    cgyBlock.texture = SKTexture(imageNamed: cgyArray[randomCGY])
    let actionMove = SKAction.moveTo(CGPoint(x: -cgyBlock.size.width/2, y: CGRectGetMidY(self.frame) + 60), duration: 3) 
    let actionDone = SKAction.removeFromParent()
    cgyBlock.runAction(SKAction.sequence([actionMove, actionDone]))
    SKActionTimingMode.EaseOut
}

如何生成子弹然后再拍摄?我很高兴听到任何想法,谢谢!

1 个答案:

答案 0 :(得分:1)

你可以在大炮中生成一个子弹作为全局变量,然后在$('.menu-up').on('click', function(e) { $( ".icon-bar" ).slideDown( "slow", function() { // Animation complete. }); $(this).toggleClass("menu-up menu-down") e.preventDefault(); }); 中创建一个新的子弹,它将是实际从大炮发射的子弹。您将新子弹的纹理设置为全局项目符号的纹理。触发动作后,为全局项目符号指定一个新纹理。如果您需要重新加载时间,请暂时删除全局项目符号,并使用touchesEnded“锁定”加农炮直到时间结束为止。

这是我使用的一些代码。如果大炮移动,那么请确保更新全球子弹的位置。 Boolean的减法给我一个错误,但我正在使用Xcode 6.4。

编辑:通过为CGPoint提供值

,更快捷,更轻松
bulletInCannon