重复的背景时,触摸开始Spritkit
问题描述:
我有一个简单的代码,重复的背景永远使用Spritekit,我想发生任何接触之前的背景是静静的,只有启动动画或滚动时的任何触摸开始重复的背景时,触摸开始Spritkit
*// animate the background*
let background = SKTexture(imageNamed: "bg.png")
let moveTheBackground = SKAction.move(by: CGVector(dx: -
background.size().width, dy:0), duration: 10)
let backgroundToOriginalPosition = SKAction.move(by: CGVector(dx:
background.size().width, dy: 0), duration: 0)
let animateTheBackground =
SKAction.repeatForever(SKAction.sequence([moveTheBackground,
backgroundToOriginalPosition]))
*// loop the background animation*
var i:CGFloat = 0
while i < 2 {
bg = SKSpriteNode(texture: background)
bg.position = CGPoint(x: background.size().width * i, y: self.frame.midY)
bg.size.height = self.frame.height
bg.zPosition = -1
bg.run(animateTheBackground)
self.addChild(bg)
i += 1
}
我想这个代码发生时的touchesBegan在SpriteKit, 任何帮助,将不胜感激
答
在任何游戏编程,以显示水平滚动理想的方式是使用2背景图像和一个水平以后添加一个。移动两者,每当background1交叉最左边0(从右到左滚动)将其位置更改为x的宽度。使用主游戏循环来增加两个背景的位置。在你的情况下使用一个名为allowScroll的标志,当touchesBegin设置为true。如果allowScroll设置为true,则增加/减少游戏循环中的背景图像位置。
答
把代码为你touchesMoved函数内采取行动,获得后台相应地移动到触摸
非常感谢大师, 可以说我得到了上面的代码,它的简单,它的工作全部完成 我希望它运行在touchesbegan方法,所以当有人触摸屏幕的背景开始动画从右到左 任何接触发生之前,背景应该仍然,不动,当屏幕触摸背景应该开始移动 我该如何发生?谢谢 – mue
@mue,你在回答你自己的问题。将运行代码放入触摸事件 – Knight0fDragon
Knight0fDragon, 我已经做到了,通过将代码放入touchesBegan方法中,背景消失,因为它在didMove(查看:SKView)函数上不再可用,我想要的是可用的背景游戏加载和当我触摸屏幕背景应该移动到左边无限 – mue