[英]How to rotate sprite in sprite kit with swift
嗨,我的問題是關於兩個精靈的旋轉。 當我觸摸屏幕的右半部分時,旋轉開始並移動sprite2和sprite3。 如果我觸摸屏幕的左半部分,則旋轉停止,因為速度-速度=0。如果我再次觸摸左半部分,則旋轉開始。
但是,如果我觸摸屏幕上與當前旋轉方向相對應的一半,速度就會復制。 我希望能夠更改旋轉方向,但要保持速度恆定。
演示問題的視頻:http: //youtu.be/HxLwl1QZiNM
import SpriteKit
class GameScene: SKScene {
let sprite = SKSpriteNode(imageNamed:"bWhite")
let sprite2 = SKSpriteNode(imageNamed:"bBlue")
let sprite3 = SKSpriteNode(imageNamed:"bRed")
let circle = SKSpriteNode(imageNamed:"bCircle")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
backColor = SKColor(red: 0, green: 0, blue: 0, alpha: 1)
self.backgroundColor = backColor
sprite.setScale(1.25)
sprite2.setScale(1)
sprite3.setScale(1)
sprite.position = CGPointMake(self.frame.size.width/2, (self.frame.size.height/2)-200);
circle.position = CGPointMake(self.frame.size.width/2, (self.frame.size.height/2)-200);
sprite3.zRotation = 172.77
sprite2.anchorPoint = CGPointMake(-1.10, 0.5);
sprite3.anchorPoint = CGPointMake(-1.10, 0.5);
self.addChild(sprite)
self.addChild(circle)
sprite.addChild(sprite2)
sprite.addChild(sprite3)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)
let action2 = SKAction.rotateByAngle(CGFloat(-M_PI), duration:1)
if (location.x > self.frame.size.width / 2)
{
sprite2.runAction(SKAction.repeatActionForever(action2))
sprite3.runAction(SKAction.repeatActionForever(action2))
} else {
sprite2.runAction(SKAction.repeatActionForever(action))
sprite3.runAction(SKAction.repeatActionForever(action))
}
}
}
好,三個。 我不確定旋轉何時結束等細節,但是所有部分都應與下面的代碼一起放在適當的位置。 它會:
如果用戶觸摸屏幕的另一半,則切換旋轉角度
import SpriteKit enum rotationDirection{ case clockwise case counterClockwise case none } class GameScene: SKScene { var currentRotationDirection = rotationDirection.none let sprite = SKSpriteNode(color: UIColor.yellowColor(), size: CGSizeMake(100, 100)) override func didMoveToView(view: SKView) { sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size) sprite.physicsBody.affectedByGravity = false sprite.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)) self.addChild(sprite) } override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { let touch : UITouch = touches.anyObject() as UITouch let touchPosition = touch.locationInNode(self) let newRotationDirection : rotationDirection = touchPosition.x < CGRectGetMidX(self.frame) ? .clockwise : .counterClockwise if currentRotationDirection != newRotationDirection && currentRotationDirection != .none{ reverseRotation() currentRotationDirection = newRotationDirection } else if currentRotationDirection == newRotationDirection{ stopRotation() currentRotationDirection = .none } else if (currentRotationDirection == .none){ setupRotationWith(direction: newRotationDirection) currentRotationDirection = newRotationDirection } } func reverseRotation(){ let oldRotateAction = sprite.actionForKey("rotate") let newRotateAction = SKAction.reversedAction(oldRotateAction) sprite.runAction(newRotateAction(), withKey: "rotate") } func stopRotation(){ sprite.removeActionForKey("rotate") } func setupRotationWith(#direction: rotationDirection){ let angle : Float = (direction == .clockwise) ? Float(M_PI) : -Float(M_PI) let rotate = SKAction.rotateByAngle(angle, duration: 1) let repeatAction = SKAction.repeatActionForever(rotate) sprite.runAction(repeatAction, withKey: "rotate") } }
編輯:更改示例以解決問題中的特定需求。 代碼格式有些奇怪,不能完全確定發生了什么。
我建議刪除所有當前操作,然后再添加新操作,因為它們可能會發生沖突。 我仍然不確定您要達到的目標,您能解釋更多嗎?
您是否已經厭倦了輕觸觸摸動作已結束? 在讓對象根據用戶觸摸屏幕的一半左右移動之前,我遇到了類似的問題。 我有一個類似的問題,如果我再次左右觸摸,我的速度將加倍,而如果我觸摸左,我的物體將停下來。
我通過添加touchesEnded方法解決了此問題,並在我放手時刪除了所有操作。 這實際上重置了我的移動動作,因此當我放開對象時,該對象將停止並立即向相反的方向移動,而不必兩次敲擊它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.