斯威夫特3子弹射击延迟

时间:2017-01-24 20:12:34

标签: swift sprite-kit delay projectile

在我的游戏中,您点按屏幕上的任意位置,然后子弹向该方向移动。唯一的问题是你可以尽可能快地拍摄。有没有办法在每次拍摄后添加延迟。所以我想拍摄,等待1秒然后拍摄。这是我在touchesEnded中的代码:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    guard let touch = touches.first else {
        return
    }
    let touchLocation = touch.location(in: self)

    //Set up initial location of bullet and properties
    let bullet = SKSpriteNode(imageNamed: "bullet")
    bullet.name = "Bullet"
    bullet.position = player.position
    bullet.setScale(0.75)
    bullet.zPosition = 1
    bullet.physicsBody = SKPhysicsBody(circleOfRadius: bullet.size.width/2)
    bullet.physicsBody?.isDynamic = true
    bullet.physicsBody?.categoryBitMask = PhysicsCategory.Projectile
    bullet.physicsBody?.contactTestBitMask = PhysicsCategory.Monster
    bullet.physicsBody?.collisionBitMask = PhysicsCategory.None
    bullet.physicsBody?.usesPreciseCollisionDetection = true

    //Determine offset of location to bullet
    let offset = touchLocation - bullet.position

    //Stops Bullet from shooting backwards
    if (offset.y < 0) { return }

    addChild(bullet)

    //Get the direction of where to shoot
    let direction = offset.normalized()

    //Make it shoot far enough to be guaranteed off screen
    let shootAmount = direction * 1000

    //Add the shoot amount to the current position
    let realDest = shootAmount + bullet.position

    //Create the actions

    if currentGameState == gameState.inGame {
    let actionMove = SKAction.move(to: realDest, duration: 1.0)
    let actionMoveDone = SKAction.removeFromParent()
    bullet.run(SKAction.sequence([actionMove, actionMoveDone]))
    }

}

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

这是一种更简单的方法,基于Date的使用:

var EA:Array = [energyFlame1,energyFlame2,energyFlame3,energyFlame4,energyFlame5];

for (var i:int = 0; i < 5; i++)
{
    var EFi:MovieClip = EA[i];
    EFi.gotoAndStop((i >= counterEnergy)? 2: 1);
}

只需根据您的需要进行修改:]

答案 1 :(得分:0)

您可以查看Throttle RxSwiftlet timeIntervalSinceLast: RxTimeInterval if let lastSendingTime = _lastSentTime { timeIntervalSinceLast = now.timeIntervalSince(lastSendingTime) } else { timeIntervalSinceLast = _parent._dueTime } let couldSendNow = timeIntervalSinceLast >= _parent._dueTime if couldSendNow { self.sendNow(element: element) return } 实施方案,了解一种可能的解决方案。 Throttle用于限制在定义的时间间隔内创建的事件数:

messages.foreachRDD(parseAndSaveToFiloDb)

private static Function<JavaPairRDD<String, String>, Void> parseAndSaveToFiloDb = initialRdd -> {
        final List<RowWithSchema> parsedMessages = parseMessages(initialRdd.collect());
        final JavaRDD<Row> rdd = javaSparkContext.parallelize(createRows(parsedMessages));
        final DataFrame dataFrame = sqlContext.createDataFrame(rdd, generateSchema(rawMessages);

        dataFrame.write().format("filodb.spark")
                .option("database", keyspace)
                .option("dataset", dataset)
                .option("row_keys", rowKeys)
                .option("partition_keys", partitionKeys)
                .option("segment_key", segmentKey)
                .mode(saveMode).save();
        return null;
    };

答案 2 :(得分:0)

您可以使用操作键执行此操作。操作键是一个使操作可识别的字符串。

在这种情况下如何使用它?

正如我在评论中已经说过的那样,你将发射一颗子弹,然后在一个特定节点上用一个键运行一个动作,该动作将持续一秒钟。此键/动作的存在意味着武器被锁定。因此,每次尝试触发子弹时,都会检查特定节点上是否存在此密钥。操作完成后,密钥也将自动删除。这是代码:

import SpriteKit

let kLockWeaponActionKey = "kLockWeaponActionKey"

class GameScene: SKScene {


    func shoot(atPoint targetLocation:CGPoint){

        // 1 check if weapon is unlocked, or return
        guard self.action(forKey: kLockWeaponActionKey) == nil else {

            print("Weapon locked")

            return
        }

        let bullet = SKSpriteNode(color: .purple, size: CGSize(width: 20, height: 20))

        addChild(bullet)

        let shoot = SKAction.move(to: targetLocation, duration: 3)

        //2 shoot
        bullet.run(shoot)

        //3 lock weapon
        self.run(SKAction.wait(forDuration: 1), withKey: kLockWeaponActionKey)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        if let touch = touches.first {

            let targetLocation = touch.location(in: self)

            self.shoot(atPoint:targetLocation)

        }
    }
}

如果您尝试快速发送垃圾邮件,您会在控制台中看到一条日志:“武器锁定”。