SKSpriteNode 不会移动

Posted

技术标签:

【中文标题】SKSpriteNode 不会移动【英文标题】:SKSpriteNode Won't Move 【发布时间】:2015-03-20 02:44:52 【问题描述】:

我使用以下代码在 Swift 中以 SKSpriteNode 的形式创建了一个矩形图像:

var screenImage = SKSpriteNode(texture: SKTexture(imageNamed: "\(imageChoices[randomImageChoice].0)"))
screenImage.position = CGPointMake(screen1.position.x, screen1.position.y)
screenImage.size = CGSizeMake(self.frame.size.width * 0.6, self.frame.size.height)
self.addChild(screenImage)

我继续使用以下代码移动图像:

func swipedTrue(sender: UISwipeGestureRecognizer) 

    if gameOver == false && tutorial == false 

        //if you swipe, it checks if you were right, then moves on or GameOver()
        if (wordChoices[randomWordChoice]).1 == true 

            //reset time
            timePerQuestion = 1.0

            //randomize word
            randomWordChoice = Int(arc4random() % 3)
            newImage = SKSpriteNode(texture: SKTexture(imageNamed: "\(wordChoices[randomWordChoice].0)"))

            //randomize color of screens, mid-swipe
            newScreen.fillColor = UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1.0)

            //replace timeBar
            decreaseTimeBlock.fillColor = newScreen.fillColor
            decreaseTimeBlock.position = CGPointMake(self.frame.size.width * 1.5, self.frame.size.height * 0.985)
            timeBarRedValue = 0.0; timeBarGreenValue = 1.0
            newTimeBar.fillColor = UIColor(red: CGFloat(timeBarRedValue), green: CGFloat(timeBarGreenValue), blue: 0.0, alpha: 1.0)

            //actions caused by swipe: it's "bringNewScreen" because if you swipeFalse, the newScreen comes from bottom. If you swipeTrue, it comes from the top.
            var swipeTrueCurrentScreen = SKAction.moveToX(self.frame.size.width * 2, duration: 0.5)
            var bringNewScreen = SKAction.moveToY(self.frame.size.height * 0.5, duration: 0.5)
            var bringNewTimeBar = SKAction.moveToY(self.frame.size.height * 0.985, duration: 0.5)

            //reset the newScreen and word to the top of the screen, to be dropped again
            newScreen.position = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 1)
            newImage.position = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 1)
            newTimeBar.position = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 1.58)

            //swipe word and screen
            currentImage.runAction(swipeTrueCurrentScreen)
            currentTimeBar.runAction(swipeTrueCurrentScreen)
            currentScreen.runAction(swipeTrueCurrentScreen)

            //make swiping noise
            runAction(SKAction.playSoundFileNamed("Swoosh 3-SoundBible.com-1573211927.mp3", waitForCompletion: false))

            //bring in the newScreen
            newScreen.runAction(bringNewScreen)
            newImage.runAction(bringNewScreen)
            newTimeBar.runAction(bringNewTimeBar)

            //increase score
            ++score
            scoreLabel.text = "\(score)"

            //here, switch the currentScreen with the newScreen so that the process can be repeated
            if newScreen == screen1 

                newScreen = screen2
                newImage = screenImage2
                newTimeBar = timeBar2
                currentScreen = screen1
                currentImage = screenImage1
                currentTimeBar = timeBar1

             else 

                newScreen = screen1
                newImage = screenImage1
                newTimeBar = timeBar1
                currentScreen = screen2
                currentImage = screenImage2
                currentTimeBar = timeBar2

            

         else 

            GameOver()

        

    


但是,由于某种原因,图像不会移动,当我尝试在其他情况下移动它时,它会拒绝。我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

除了这里缺少一个括号(但我猜你的代码中不是这种情况),代码没有特别的理由不工作。问题很可能在于您如何使用它。

我的猜测是你正在做这样的事情:

import SpriteKit

class GameScene: SKScene 

    var sprite : SKSpriteNode = SKSpriteNode() // A

    override func didMoveToView(view: SKView) 

        // You are creating another `sprite` variable
        // and not using the (A) sprite you declare above
        var sprite = SKSpriteNode(imageNamed:"Spaceship") // B

        // Here you set the (B) sprite you just created
        sprite.size = CGSizeMake(self.frame.size.width * 0.6, self.frame.size.height)
        sprite.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));

        // Here it's still the (B) sprite you just created
        // that you add to the scene
        self.addChild(sprite)

        // You are calling your action from somewhere else
        self.applyAction()

    

    func applyAction() 
        // You create an action, OK
        let action = SKAction.moveToX(self.frame.size.width * 2, duration: 0.5)

        // You apply the action to the (A) sprite property you have in your class
        // Same as : self.sprite.runAction(action)
        sprite.runAction(action)
    

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) 
        /* Called when a touch begins */
    

    override func update(currentTime: CFTimeInterval) 
        /* Called before each frame is rendered */
    

在这种情况下,您只需要不创建另一个精灵变量。通过删除 // B 行上的 var 关键字。

如果有帮助,请告诉我们。 如果不是这样,请提供更多详细信息(代码,...)。

【讨论】:

不幸的是,情况并非如此。但是,我在问题中添加了信息,因此希望您能够解决它。感谢您的贡献!

以上是关于SKSpriteNode 不会移动的主要内容,如果未能解决你的问题,请参考以下文章

仅当 SKSpriteNode 是 Fruit 类型时才用手指移动 SKSpriteNode(SKSpriteNode 的子类)

使用 CoreMotion 移动 SKSpriteNode

改变移动 SKSpriteNode 的方向?

使用 UIPanGestureRecognizer 连续移动 SKSpriteNode

在屏幕上移动 SKSpriteNode

如何在 Swift 中围绕其起始值上下移动 SKSpriteNode?