SKSpriteNode不会移动 - 斯威夫特

时间:2015-03-25 13:56:26

标签: swift ios8 sprite-kit skspritenode

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

var screenImage1 = SKSpriteNode(); var screenImage2 = SKSpriteNode()

//defining actual images that appear
    screenImage1 = SKSpriteNode(texture: SKTexture(imageNamed: "\(wordChoices[randomWordChoice].0)")); screenImage2 = SKSpriteNode(texture: SKTexture(imageNamed: "\(wordChoices[randomWordChoice].0)"))
    screenImage1.position = CGPointMake(screen1.position.x, screen1.position.y); screenImage2.position = CGPointMake(screen2.position.x, screen2.position.y)
    screenImage1.size = CGSizeMake(self.frame.size.width * 0.6, self.frame.size.height); screenImage2.size = CGSizeMake(self.frame.size.width * 0.6, self.frame.size.height)

self.addChild(screenImage1); self.addChild(screenImage2)

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

//used as placeholders for the reseting the swipe mechanism
var currentScreen = SKShapeNode(); var currentImage = SKSpriteNode(); var currentTimeBar = SKShapeNode()
var newScreen = SKShapeNode(); var newImage = SKSpriteNode(); var newTimeBar = SKShapeNode()

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

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()

        }

    }

}

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

0 个答案:

没有答案