【问题标题】:Increasing the size of a spriteNode using spriteKit in update function (Swift)在更新函数中使用 spriteKit 增加 spriteNode 的大小(Swift)
【发布时间】:2019-12-23 12:29:58
【问题描述】:

我在动画、物理和类似的东西上遇到了麻烦。

无论如何,我要做的是为一个球体创建一个脉冲效果,其中脉冲本身能够与屏幕上的元素发生碰撞,所以我创建了另一个我想要缩放的 spriteNode(球体的子节点)连续,不依赖于触摸。

我读到在这些情况下最好不要使用 SKActions。

我的问题是:如何通过更新函数缩放或增加 spriteNode 的大小?

例如,代码代表了我到目前为止是如何工作的。

import SpriteKit
import GameplayKit

class GameScene: SKScene {

 var entities = [GKEntity]()
 var graphs = [String: GKGraph]()

 var movingSquareLv1 = SKSpriteNode()
 var pulsingSphereLv2 = SKSpriteNode()

 var lv2SpherePulse = SKSpriteNode()


 var xVeloicty: CGFloat = 100


 override func sceneDidLoad() {

  movingSquareLv1 = self.childNode(withName: "movingSquare") as!SKSpriteNode
  movingSquareLv1.physicsBody = SKPhysicsBody(rectangleOf: movingSquareLv1.size)
  movingSquareLv1.physicsBody ? .affectedByGravity = false

  pulsingSphereLv2 = self.childNode(withName: "pulsingSphere") as!SKSpriteNode
  pulsingSphereLv2.physicsBody ? .affectedByGravity = false

  lv2SpherePulse = pulsingSphereLv2.childNode(withName: "lv2SpherePulse") as!SKSpriteNode
  lv2SpherePulse.physicsBody ? .affectedByGravity = false


 }


 func touchDown(atPoint pos: CGPoint) {

 }

 func touchMoved(toPoint pos: CGPoint) {

 }

 func touchUp(atPoint pos: CGPoint) {

 }

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

  for t in touches {
   self.touchDown(atPoint: t.location( in: self))
  }
 }

 override func touchesMoved(_ touches: Set < UITouch > , with event: UIEvent ? ) {
  for t in touches {
   self.touchMoved(toPoint: t.location( in: self))
  }
 }

 override func touchesEnded(_ touches: Set < UITouch > , with event: UIEvent ? ) {
  for t in touches {
   self.touchUp(atPoint: t.location( in: self))
  }
 }

 override func touchesCancelled(_ touches: Set < UITouch > , with event: UIEvent ? ) {
  for t in touches {
   self.touchUp(atPoint: t.location( in: self))
  }
 }


 override func update(_ currentTime: TimeInterval) {

  rectMovement()


 }

 func rectMovement() {

  if movingSquareLv1.frame.maxX >= self.size.width / 2 {

   xVeloicty = -100

  } else if movingSquareLv1.frame.minX <= -self.size.width / 2 {
   xVeloicty = 100
  }

  let rate: CGFloat = 0.5
  let relativeVelocity: CGVector = CGVector(dx: xVeloicty - movingSquareLv1.physicsBody!.velocity.dx, dy: 0)
  movingSquareLv1.physicsBody!.velocity = CGVector(dx: movingSquareLv1.physicsBody!.velocity.dx + relativeVelocity.dx * rate, dy: 0)

 }

}

这是由两个精灵组成的对象,我要处理的是最大的一个。

【问题讨论】:

  • 您可以在您尝试执行此操作的位置添加代码吗?现在很难回答你的问题
  • 如果你想有一个圆形碰撞,你应该使用skshapenode,因为精灵节点只有矩形碰撞。
  • @AlexandruVasiliu 我很确定圆形物理体是可用的,并且在可能的情况下建议将其作为碰撞和接触的最有效形状。
  • 我指的是通过相交的基本 spritekit 碰撞,而不是物理体碰撞。
  • 我将编辑帖子,添加代码和我拥有的精灵。哦,无论如何,我什至可以使用 spriteNodes,我只需要使用圆形 physicBody

标签: swift xcode sprite-kit collision skspritenode


【解决方案1】:

告诉你不要使用 SKActions 的人是错误的。这正是您想要使用 SKAction 的时候,如果您有多个圈子在跳动,则对所有圈子使用相同的操作。

您确实希望使更新功能尽可能小。相信苹果的优化,因为他们比你更容易访问 API。

【讨论】:

  • 我也同意这一点。一旦掌握了动作和物理原理,您可能会发现大部分游戏只是让模拟自行运行。
  • 谢谢你们,我正在与 SKActions 合作,一切顺利。
  • 是的,您不希望在您的场景中有未运行的 SKAction。如果您说 500 个圆圈,并且只有 1 个旋转,那么您不想让 500 个旋转动作同时进行 499 个暂停。也许告诉您不要使用 SKActions 的人误解了该建议。
【解决方案2】:

我不知道为什么你读到的任何东西都说动作不合适,但是如果你想明确地从update 更改节点的大小,那么你可以设置它的缩放。

myNode.setScale(2.5)  // 2.5x as big as normal
myNode.setScale(0.5)  // half size

myNode.xScale = 1.5
myNode.yScale = myNode.xScale * 2  // Set x and y separately for non-uniform scaling

请参阅SKNode 的“缩放和旋转”部分中的文档: https://developer.apple.com/documentation/spritekit/sknode

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    相关资源
    最近更新 更多