简体   繁体   English

如何快速旋转精灵工具包中的精灵

[英]How to rotate sprite in sprite kit with swift

Hi my problem is regarding the rotation of two sprites. 嗨,我的问题是关于两个精灵的旋转。 When I touch the right half of the screen the rotation starts and moving sprite2 and sprite3. 当我触摸屏幕的右半部分时,旋转开始并移动sprite2和sprite3。 If I touch the left half of the screen the rotation stops because velocity-velocity = 0. If I touch the left half again the rotation begins. 如果我触摸屏幕的左半部分,则旋转停止,因为速度-速度=0。如果我再次触摸左半部分,则旋转开始。

However, if I touch the half of the screen corresponding with the current rotational-direction the velocity is duplicated. 但是,如果我触摸屏幕上与当前旋转方向相对应的一半,速度就会复制。 I want to be able to change the direction of the rotation, but for the speed to remain constant. 我希望能够更改旋转方向,但要保持速度恒定。

Video demonstrating the problem: http://youtu.be/HxLwl1QZiNM 演示问题的视频: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))
            }     
        }
    }

OK, take three. 好,三个。 I'm not 100% sure about the specifics of when the rotation should end etc. but all the pieces should be in place with the below code. 我不确定旋转何时结束等细节,但是所有部分都应与下面的代码一起放在适当的位置。 It will: 它会:

  • start rotation clockwise or counterclockwise based on the position of the first touch 根据第一次触摸的位置开始顺时针或逆时针旋转
  • stop the rotation if the user touches for the same direction again 如果用户再次触摸相同方向,则停止旋转
  • switch the rotation if the user touches on the other half of the screen 如果用户触摸屏幕的另一半,则切换旋转角度

      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") } } 

Edit: Changed example to cater the specific needs in the question. 编辑:更改示例以解决问题中的特定需求。 Something odd with the code formatting not quite sure what's going on there. 代码格式有些奇怪,不能完全确定发生了什么。

I suggest removing all current actions before adding new, since they may conflict. 我建议删除所有当前操作,然后再添加新操作,因为它们可能会发生冲突。 I'm still not sure what you are trying to achieve, can you explain more? 我仍然不确定您要达到的目标,您能解释更多吗?

Have you just tired removing the actions at touchesEnded? 您是否已经厌倦了轻触触摸动作已结束? I had a similar problem before where I wanted my object to move left to right based on which half of the screen the user touched. 在让对象根据用户触摸屏幕的一半左右移动之前,我遇到了类似的问题。 I had a similar issue where if I touched right and right again my velocity would double, and if I touched left my object would just stop. 我有一个类似的问题,如果我再次左右触摸,我的速度将加倍,而如果我触摸左,我的物体将停下来。

I fixed this issue by adding the touchesEnded method and just removed the actions whenever I let go. 我通过添加touchesEnded方法解决了此问题,并在我放手时删除了所有操作。 This essentially reset my move action so the object would stop when I let go and would go to the opposite direction right away instead of having to tap it twice. 这实际上重置了我的移动动作,因此当我放开对象时,该对象将停止并立即向相反的方向移动,而不必两次敲击它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM