我在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()
}
}
}
然而,由于某种原因,图像不会移动,当我尝试在其他情况下移动它时,它会拒绝。我该如何解决这个问题?
答案 0 :(得分:0)
除了一个缺少的括号(但我想你的代码中的情况并非如此),代码没有特别的理由不起作用。问题很可能在于你如何使用它。
我的猜测是你正在做这样的事情:
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 */
}
}
在这种情况下,您只需要创建另一个sprite变量。删除var
行上的// B
关键字。
让我们知道它是否有帮助。 如果情况并非如此,请提供更多详细信息(代码,......)。