Swift 斯威夫特| I';我试着旋转一个精灵,并在每次点击时改变旋转方向
我想无限期地旋转一个精灵,每次我点击按钮,我想精灵以相反的方向旋转(从顺时针到逆时针来回等等) 以下是我的代码:Swift 斯威夫特| I';我试着旋转一个精灵,并在每次点击时改变旋转方向,swift,sprite-kit,Swift,Sprite Kit,我想无限期地旋转一个精灵,每次我点击按钮,我想精灵以相反的方向旋转(从顺时针到逆时针来回等等) 以下是我的代码: 你的问题是你没有删除旧的SKAction试图旋转你的SKSpriteNode。要做到这一点,你需要跟踪你的sprite旋转的方式。如果我要实现这一点,我将子类化SKSpriteNode,如下所示: class RotatingSprite: SKSpriteNode { // This is used to keep track of which way the sprit
你的问题是你没有删除旧的
SKAction
试图旋转你的SKSpriteNode
。要做到这一点,你需要跟踪你的sprite旋转的方式。如果我要实现这一点,我将子类化SKSpriteNode
,如下所示:
class RotatingSprite: SKSpriteNode {
// This is used to keep track of which way the sprite is rotating.
enum Direction {
case Left, Right
mutating func inverse() {
switch self {
case .Left : self = .Right
case .Right: self = .Left
}
}
}
// These names will be the keys used when running an action.
// This will allow you to stop the rotate-left or rotate-right actions.
static let rotateLeftName = "RotateLeftAction"
static let rotateRightName = "RotateRightAction"
var rotationDirection: Direction? {
didSet {
if let r = rotationDirection {
switch r {
// Checks the sprite isn't already rotating to the left.
// If it isn't, make the sprite rotate to the left.
case .Left where oldValue != .Left:
rotateLeft()
case .Right where oldValue != .Right:
rotateRight()
default:
break
}
}
}
}
private func rotateLeft() {
// Remove the action rotating the sprite to the right.
self.removeActionForKey(RotatingSprite.rotateRightName)
// And start the action rotating the sprite to the left.
let rotateAction = SKAction.rotateByAngle(-CGFloat(M_PI), duration: 1.0)
self.runAction(SKAction.repeatActionForever(rotateAction), withKey: RotatingSprite.rotateLeftName)
}
private func rotateRight() {
self.removeActionForKey(RotatingSprite.rotateLeftName)
let rotateAction = SKAction.rotateByAngle(CGFloat(M_PI), duration: 1.0)
self.runAction(SKAction.repeatActionForever(rotateAction), withKey: RotatingSprite.rotateRightName)
}
}
现在您可以像这样使用RotatingSprite
:
class GameScene: SKScene {
let rotatingSprite = RotatingSprite(texture:bgTexture)
override func didMoveToView(view: SKView) {
rotatingSprite.position = CGPoint(x: frame.midX, y: frame.midY)
self.addChild(rotatingSprite)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
// If the sprite isn't turning you've got to set it off.
if rotatingSprite.rotationDirection == nil {
rotatingSprite.rotationDirection = .Left
// If it is turning, change its direction.
} else {
rotatingSprite.rotationDirection!.inverse()
}
}
}
希望有帮助!实现这一点非常容易。试试这个
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
[sprite removeAllActions];
SKAction *action;
if (isClockWise)
{
action = [SKAction rotateByAngle:M_PI duration:1];
}
else
{
action = [SKAction rotateByAngle:-M_PI duration:1];
}
isClockWise = !isClockWise;
[sprite runAction:[SKAction repeatActionForever:action]];
}
当sprite为SkSpritEndode时,根据您的初始移动方向,按顺时针方向启动为是或否。执行此操作的快捷方法如下:
import SpriteKit
class GameScene: SKScene {
var center = SKSpriteNode()
var bg = SKSpriteNode()
var bigCircle = SKSpriteNode()
let counterClockwise = SKAction.rotateByAngle(CGFloat(3.14), duration:1)
let clockwise = SKAction.rotateByAngle(CGFloat(-3.14), duration:1)
var spin = SKAction()
// this is used to identify which direction we are going in. When we change it spin is changed as well
var isClockwise: Bool = true {
didSet {
if isClockwise {
spin = clockwise
} else {
spin = counterClockwise
}
}
}
let actionKey = "spin" // this is used to identify the SKAction
override func didMoveToView(view: SKView) {
//Background
var bgTexture = SKTexture(imageNamed: "images/bg.png")
bg = SKSpriteNode(texture:bgTexture)
bg.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(center)
//Center Circle
var bigCircleTexture = SKTexture(imageNamed: "images/bigCircle.png")
bigCircle = SKSpriteNode(texture:bigCircleTexture)
bigCircle.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(bigCircle)
//Center Triangle
var centerTexture = SKTexture(imageNamed: "images/center.png")
center = SKSpriteNode(texture:centerTexture)
center.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(center)
isClockwise = true // set the initial direction to clockwise
center.runAction(SKAction.repeatActionForever(spin), withKey: actionKey)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
// remove the existing spin action
center.removeActionForKey(actionKey)
// reset the direction (this will automatically switch the SKAction)
isClockwise = !isClockwise
center.runAction(SKAction.repeatActionForever(spin), withKey: actionKey)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
导入SpriteKit
类游戏场景:SKScene{
var center=SKSpriteNode()
var bg=SKSpriteNode()
var bigcirle=SKSpriteNode()
逆时针方向=SKAction.rotateByAngle(CGFloat(3.14),持续时间:1)
顺时针=SKAction.rotateByAngle(CGFloat(-3.14),持续时间:1)
var spin=SKAction()
//这是用来确定我们前进的方向。当我们改变它时,自旋也会改变
变量isClockwise:Bool=true{
迪塞特{
如果逆时针{
旋转=顺时针
}否则{
旋转=逆时针
}
}
}
让actionKey=“spin”/
覆盖func didMoveToView(视图:SKView){
//背景
var bgTexture=SKTexture(ImageName:“images/bg.png”)
bg=SKSpriteNode(纹理:bgTexture)
bg.position=CGPoint(x:CGRectGetMidX(self.frame),y:CGRectGetMidY(self.frame))
self.addChild(中心)
//中心圆
var bigCircleTexture=SKTexture(ImageName:“images/bigcirle.png”)
bigCircle=SKSpriteNode(纹理:bigCircleTexture)
bigCircle.position=CGPoint(x:CGRectGetMidX(self.frame),y:CGRectGetMidY(self.frame))
self.addChild(大圆圈)
//中心三角形
var centerTexture=SKTexture(ImageName:“images/center.png”)
中心=SKSpriteNode(纹理:中心纹理)
center.position=CGPoint(x:CGRectGetMidX(self.frame),y:CGRectGetMidY(self.frame))
self.addChild(中心)
isClockwise=true//将初始方向设置为顺时针
center.runAction(SKAction.repeatActionForever(旋转),带键:actionKey)
}
覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
//移除现有的旋转动作
center.removeActionForKey(操作键)
//重置方向(这将自动切换SKAction)
isClockwise=!isClockwise
center.runAction(SKAction.repeatActionForever(旋转),带键:actionKey)
}
覆盖函数更新(currentTime:CFTimeInterval){
/*在渲染每个帧之前调用*/
}
}
在应用新操作之前,需要删除该操作-可以通过调用runAction(操作:,withKey:)有选择地删除该操作。这使您能够使用相同的键删除相同的操作。更改
spin
的逻辑位于isClockwise
var声明的didSet
中。只需在每个if语句中放置center.removeAllActions即可,以确保当方向为suppos时精灵当前未移动ed需要更改。您使用的是sprite套件吗?到目前为止您尝试了什么?抱歉,忘记发布我的当前代码。现在开始。问题是您在didMoveToView中设置的操作从未停止过吗?我确实希望它永远旋转…当然,直到按下按钮。然后我希望它永远改变方向并旋转,直到按钮被按下为止essed。按下按钮时停止didMoveToView中的操作会有帮助吗?这无关紧要,因为我在第一次按下按钮后不需要它,因为它也在TouchsStart中运行。哇,这比我想象的要复杂得多。我通读了你的代码并理解了大部分内容。我想看看它是如何工作的,但需要一些修改ason我收到一个错误,说静态属性只允许在结构和枚举中使用,它告诉我把它放在类属性中。但它看起来已经在类中了。啊,好吧,你的问题是你使用的是旧版本的Xcode,试着更新到Xcode 6.3,并将NSSet
更改为Set
,一切都应该正常。是的,这会变得更复杂,但这是课堂上的全部内容;这样可以保持场景整洁。这会起作用,但重要的是,如果你的精灵正在执行任何其他动作,触摸屏幕也会阻止它们。我尝试了上述解决方案,我得到了一个“预期声明”错误,指向(void)旁边的“-”@ABakerSmith在所提供的代码中,没有对sprite执行任何其他操作。@StudioSevenDesigns在界面文件BOOL isClockWise;
和didMoveToView
中包含此isClockWise=YES;
import SpriteKit
class GameScene: SKScene {
var center = SKSpriteNode()
var bg = SKSpriteNode()
var bigCircle = SKSpriteNode()
let counterClockwise = SKAction.rotateByAngle(CGFloat(3.14), duration:1)
let clockwise = SKAction.rotateByAngle(CGFloat(-3.14), duration:1)
var spin = SKAction()
// this is used to identify which direction we are going in. When we change it spin is changed as well
var isClockwise: Bool = true {
didSet {
if isClockwise {
spin = clockwise
} else {
spin = counterClockwise
}
}
}
let actionKey = "spin" // this is used to identify the SKAction
override func didMoveToView(view: SKView) {
//Background
var bgTexture = SKTexture(imageNamed: "images/bg.png")
bg = SKSpriteNode(texture:bgTexture)
bg.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(center)
//Center Circle
var bigCircleTexture = SKTexture(imageNamed: "images/bigCircle.png")
bigCircle = SKSpriteNode(texture:bigCircleTexture)
bigCircle.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(bigCircle)
//Center Triangle
var centerTexture = SKTexture(imageNamed: "images/center.png")
center = SKSpriteNode(texture:centerTexture)
center.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
self.addChild(center)
isClockwise = true // set the initial direction to clockwise
center.runAction(SKAction.repeatActionForever(spin), withKey: actionKey)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
// remove the existing spin action
center.removeActionForKey(actionKey)
// reset the direction (this will automatically switch the SKAction)
isClockwise = !isClockwise
center.runAction(SKAction.repeatActionForever(spin), withKey: actionKey)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}