使用 SkSpriteNode 旋转对象 [英] Turning Object with SkSpriteNode
问题描述
我有一个名为Ghost"的图像,它在整个屏幕上移动.我想让 SpriteNode 在不被按下的情况下不断转动.
I have an image named "Ghost" and it moves throughout the screen. I want to have the SpriteNode constantly turning without being pressed on.
Ghost = SKSpriteNode(imageNamed: "Ghost1")
Ghost.size = CGSize(width: 50, height: 50)
Ghost.position = CGPoint(x: self.frame.width / 2 - Ghost.frame.width, y: self.frame.height / 2)
Ghost.physicsBody = SKPhysicsBody(circleOfRadius: Ghost.frame.height / 1.4)
Ghost.physicsBody?.categoryBitMask = PhysicsCatagory.Ghost
Ghost.physicsBody?.collisionBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall
Ghost.physicsBody?.contactTestBitMask = PhysicsCatagory.Ground | PhysicsCatagory.Wall | PhysicsCatagory.Score
Ghost.physicsBody?.affectedByGravity = false
Ghost.physicsBody?.isDynamic = true
Ghost.zPosition = 2
self.addChild(Ghost)
我在 Stack Overflow 上寻找了一些答案,但我似乎找不到任何有同样问题的人,请帮忙.
I have looked for some answers on Stack Overflow but I can't seem to find any one with this same question, please help.
推荐答案
您可以施加一个角度类型的力,以旋转主体,并关闭阻尼使其永远继续旋转:
You could apply a force of an angular type, so as to rotate the body, and turn off damping so it continues to rotate forever:
https://developer.apple.com/reference/spritekit/skaction/1417775-applyangularimpulse
首先,通过将其设置为 true 来确保它能够旋转:
Firstly, make sure it’s able to rotate by setting this to true:
https://developer.apple.com/reference/spritekit/skphysicsbody/1519986-允许旋转
确保没有角度阻尼,以免旋转速度减慢:
Make sure there’s no angular damping so that it doesn’t slow in rotation rate:
https://developer.apple.com/reference/spritekit/skphysicsbody/1519913-angulardamping
现在通过施加扭矩使其旋转:
Now make it spin by applying a torque:
https://developer.apple.com/reference/spritekit/skphysicsbody/1519588-applytorque
https://developer.apple.com/reference/spritekit/skphysicsbody/1519766-角速度
这篇关于使用 SkSpriteNode 旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!