在动作已经运行的情况下停止动作SpriteKit

时间:2018-03-21 16:56:36

标签: ios swift sprite-kit

我有一个太空游戏,我现在每天都在扩展,我遇到了一个问题,我无法理解。现在我为我的宇宙飞船添加了一个PowerUp,它给了它一个盾牌。 Shield PowerUps产生的持续时间为37.5,范围为15.这对我来说是完美的。我怎么想弄清楚(或显示)我如何才能使这个盾牌只持续一定数量的秒。

现在它一直持续到它被击中,这是失去它的一种方法,但我也希望有一个计时器。

这是我的功能,可以激活与PowerUp碰撞时调用的Shield:

func activateShield() {

    let shield1 = SKTexture(imageNamed: "shield-1")
    let shield2 = SKTexture(imageNamed: "shield-2")
    let shield3 = SKTexture(imageNamed: "shield-3")
    let shield4 = SKTexture(imageNamed: "shield-4")
    let shield5 = SKTexture(imageNamed: "shield-5")
    let shield6 = SKTexture(imageNamed: "shield-6")

    let animateShield = SKAction.sequence([
        SKAction.wait(forDuration: 0, withRange: 0),
        SKAction.animate(with: [shield1,shield2,shield3,shield4,shield5,shield6], timePerFrame: 0.10)])
    let animateRepeatShield = SKAction.repeatForever(animateShield)

    shield = SKSpriteNode(texture: shield1)
    shield.name = "ShieldActive"
    shield.setScale(1.5)
    shield.position = player.position
    shield.zPosition = 3
    shield.physicsBody = SKPhysicsBody(rectangleOf: shield.size)
    shield.physicsBody!.affectedByGravity = false
    shield.physicsBody!.categoryBitMask = PhysicsCategories.ShieldActive
    shield.physicsBody!.collisionBitMask = PhysicsCategories.Enemy
    shield.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy | PhysicsCategories.Life
    shield.physicsBody!.isDynamic = true
    shield.physicsBody!.density = 0
    self.addChild(shield)
    shield.run(animateRepeatShield)

    let joint = SKPhysicsJointFixed.joint(withBodyA: player.physicsBody!, bodyB:shield.physicsBody!, anchor:player.position)
    self.physicsWorld.add(joint)
}

以下是我的代码中包含SKActions的其他部分:

    let spawnShield = SKAction.run(spawnAShield)
    let waitToSpawnShield = SKAction.wait(forDuration: 37.5, withRange: 15) //Spawn time between 30 - 45 seconds
    let spawnSequenceShield = SKAction.sequence([waitToSpawnShield, spawnShield])
    let spawnForeverShield = SKAction.repeatForever(spawnSequenceShield)
    self.run(spawnForeverShield, withKey: "spawningShieldPowerUps")

2 个答案:

答案 0 :(得分:0)

你可以添加一个持续“休眠”的动作,然后执行适当的代码来“移除”盾牌,如:

<!DOCTYPE html>
<html>

<head>
    <title>The Beckwood - Scunthorpe</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css?family=Satisfy" rel="stylesheet">

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="assets/css/style.css">

</head>

<body>
<div id="wrap">
    <div class="header-main">

        <nav class="navbar navbar-default">
            <div class="container">
                <a class="navbar-brand" href="index.html" >
                     <img src="assets/img/logo.png">

                </a>
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                </div>
                <div class="collapse navbar-collapse" id="myNavbar">
                    <ul class="nav navbar-nav navbar-right">
                        <li><a id="activenav" href="index.html">HOME</a></li>
                         <li><a href="menu.html">MENU</a></li>
                          <li><a href="gallery.html">GALLERY</a></li>
                           <li><a href="about.html">ABOUT</a></li>
                            <li><a href="book.html">BOOK A TABLE</a></li>
                            <li><a href="contact.html">CONTACT</a></li>
                        </ul>

                    </div>
            </div>
        </nav>
        <div class="hero">
            <h1 id="welcome">Welcome to The Beckwood.</h1>
            <p>The Beckwood is a family run pub located in scunthorpe offering quality homemade food, carverys, events and live sports.</p>
            <div class="btn btn-primary"><a href="menu.html">View Menu</a></div>
            <div class="btn btn-primary"><a href="menu.html">Book a table</a></div>
        </div>
    </div>

</div>

<div class="container">
    <div class="intro">
        <h1> Quality Food, Drinks and Atmosphere! </h1>
        <p>Welcome to the Beckwood! Here at the beckwood we specialise in authentic home made dishes hand cooked by our chefs.<br> Not only do we offer beautiful food we also provide entertainment, live sports and great quality beer.<br> Not only do we offer beautiful food we also provide entertainment.</p>
    </div>
</div>


<!--about-->
  <section id="about">
    <div class="container">
      <div class="row">
        <div class="col-md-12 text-center marb-35">

        </div>
        <div class="col-md-1"></div>
        <div class="col-md-10">
          <div class="col-md-6 col-sm-6">
            <div class="about-info">
              <h2 class="heading">A small heading goes here</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Libero impedit inventore culpa vero accusamus in nostrum dignissimos modi, molestiae. Autem iusto esse necessitatibus ex corporis earum quaerat voluptates quibusdam dicta!</p>
               <h2 class="heading">A small heading goes here</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Libero impedit inventore culpa vero accusamus in nostrum dignissimos modi, molestiae. Autem iusto esse necessitatibus ex corporis earum quaerat voluptates quibusdam dicta!consectetur adipisicing elit. Libero impedit inventore culpa vero accusamus in nostrum dignissimos modi, </p>

            </div>
          </div>
          <div class="col-md-6 col-sm-6">
            <img id="beckwoodabout" src="assets/img/image1.jpeg" alt="" class="img-responsive">
          </div>
        </div>
        <div class="col-md-1"></div>
      </div>
    </div>
    <br>
  </section>
  <!--/about-->

  <div class="homebar">
            <div class="container">
                <div class="row">
                    <div class="col-md-4 margin-b-30">
                        <h2>Our Carvery</h2>
                        <img id="carvpic"  src="assets/img/carvery.jpg"><br><br>
                        <div class="overflow-hidden">

                            <p>lorem ipsum and beckwood this is a test.lorem ipsum and beckwood this is a test.
                            lorem ipsum and beckwood this is a test.
                            lorem ipsum and beckwood this is a test.
                            lorem ipsum and beckwood this is a test.</p>
                            <button type="button" class="btn btn-warning"><a href="book.html">Book Now</a></button>
                        </div>
                    </div>
                    <div class="col-md-4 margin-b-30">
                        <h2>Events</h2>
                        <img id="carvpic"  src="assets/img/events.jpg"><br><br>
                        <p>lorem ipsum and beckwood this is a test.lorem ipsum and beckwood this is a test.
                            lorem ipsum and beckwood this is a test.
                            lorem ipsum and beckwood this is a test.
                            lorem ipsum and beckwood this is a test.</p>

                            <button type="button" class="btn btn-warning"><a href="book.html">Book Now</a></button>
                        <div class="overflow-hidden">


                        </div>
                    </div>
                    <div class="col-md-4 margin-b-30">
                        <h2>Function Room</h2>
                         <img id="carvpic"  src="assets/img/functionroom.jpg"><br><br>
                        <div class="overflow-hidden">
 <p>lorem ipsum and beckwood this is a test.lorem ipsum and beckwood this is a test.
                            lorem ipsum and beckwood this is a test.
                            lorem ipsum and beckwood this is a test.
                            lorem ipsum and beckwood this is a test.</p>
                            <button type="button" class="btn btn-warning"><a href="book.html">Book Now</a></button>

                        </div>
                    </div>
                </div>
            </div>
        </div>

<br><br>



<iframe id="maps"
  width="100%"
  height="300px"
  padding="0px"
  margin="0px"
  frameborder="0" style="border:0"
  src="https://www.google.com/maps/embed/v1/place?key=AIzaSyB0d_sYwCWD5owhYY4UYb-i7VlOOnx2_o4

    &q=The Beckwood,Scunthorpe" allowfullscreen>
</iframe>

<footer id="myFooter">
        <div class="container">
            <div class="row">
                <div class="col-sm-3">
                    <img id="footerlogo" src="assets/img/logo.png">



                </div>
                <div class="col-sm-4">
                    <h5>Opening Times</h5>

                             <p class="opening hours"></p>
                    <p>Tue-Thu ~ 12:00 - 23:00</p>
                    <p>Friday ~ 12:00 - 00:00</p>
                    <p>Saturday ~ 12:00 - 00:00</p>
                    <p>Sunday ~ 12:00 - 22:30</p>


                </div>
                <div class="col-sm-1">
                    <h5>Navigation</h5>
                    <ul>
                        <li><a href="index.html">Home</a></li>
                        <li><a href="menu.html">Menu</a></li>
                        <li><a href="gallery.html">Gallery</a></li>
                        <li><a href="about.html">About</a></li>
                        <li><a href="book.html">Book</a></li>
                        <li><a href="contact.html">Contact</a></li>
                    </ul>
                </div>

                <div class="col-sm-4">
                    <div class="social-networks">
                        <a href="#" class="twitter"><i class="fa fa-twitter"></i></a>
                        <a href="#" class="facebook"><i class="fa fa-facebook"></i></a>
                        <a href="#" class="google"><i class="fa fa-instagram"></i></a>
                    </div>

<img class="center" src="assets/img/hygeine.png">


                </div>

            </div>
        </div>
        <div class="footer-copyright">
            <p>© 2018 The Beckwood.</p>
        </div>
    </footer>



<script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<script src="validator.js"></script>
<script src="contact.js"></script>

</body>

</html>

答案 1 :(得分:0)

与Jean-Baptiste的答案类似,您也可以运行等待操作并在完成闭包中执行取消激活。一旦等待持续时间结束,关闭就会触发。

  

编辑以显示可以使用不同持续时间调用的func中的代码

var duration: Double = 5 //you can change this to any value. you could even change the value via code for different shields durations
deactivateShield(afterDuation: duration)

func deactivateShield(afterDuation: Double) {
    shield.run(.wait(forDuration: duration)) {
        self.deactivateShield() 
    }
}
  

编辑2

func activateShield() {

    let shield1 = SKTexture(imageNamed: "shield-1")
    let shieldImages = [shield1, SKTexture(imageNamed: "shield-2"), SKTexture(imageNamed: "shield-3"), SKTexture(imageNamed: "shield-4"), SKTexture(imageNamed: "shield-5"), SKTexture(imageNamed: "shield-6")] 

    let animateShield = SKAction.animate(with: shieldImages, timePerFrame: 0.10)])
    let animateRepeatShield = SKAction.repeatForever(animateShield)

    shield = SKSpriteNode(texture: shield1)
    shield.name = "ShieldActive"
    shield.setScale(1.5)
    shield.position = player.position
    shield.zPosition = 3
    shield.physicsBody = SKPhysicsBody(rectangleOf: shield.size)
    shield.physicsBody!.affectedByGravity = false
    shield.physicsBody!.categoryBitMask = PhysicsCategories.ShieldActive
    shield.physicsBody!.collisionBitMask = PhysicsCategories.Enemy
    shield.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy | PhysicsCategories.Life
    shield.physicsBody!.isDynamic = true
    shield.physicsBody!.density = 0
    self.addChild(shield)
    shield.run(animateRepeatShield)

    let joint = SKPhysicsJointFixed.joint(withBodyA: player.physicsBody!, bodyB:shield.physicsBody!, anchor:player.position)
    self.physicsWorld.add(joint)

    //turns off the shield in between 5-10 seconds
    shield.run(.wait(forDuration: 7.5, withRange: 2.5)) {
        self.shield.removeFromParent() 
    }
}