【发布时间】: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