更改position.x会导致skspritenode无法显示

时间:2015-01-13 17:16:19

标签: swift ios8 sprite-kit

嘿大家:我想知道这只是一个xcode错误,还是我错过了一些非常愚蠢的东西。我有一个背景滚动的游戏。每更新一次,我计算自上次更新以来的时间量,将其乘以一个常数并将背景移动该量。

这里有什么奇怪的...下面的代码片段在运行时会导致bg1,bg2,fg1和fg2不显示在场景中。

func updatePos(timeSinceLast:CGFloat)
{
    bgSpeed = BG_TIME_SPEED*timeSinceLast
    fgSpeed = FG_TIME_SPEED*timeSinceLast

    //move both background nodes
    bg1.position.x -= bgSpeed
    bg2.position.x -= bgSpeed

    //move both foreground nodes 
    fg1.position.x -= fgSpeed;
    fg2.position.x -= fgSpeed;

此代码段在运行时运行正常,并显示所有内容:

func updatePos(timeSinceLast:CGFloat)
{
    bgSpeed = 3.97734984755516  // <--- I CHANGED TO A CONSTANT INSTEAD OF A CALCULATION
    fgSpeed = 3.97734984755516

    //move both background nodes
    bg1.position.x -= bgSpeed
    bg2.position.x -= bgSpeed

    //move both foreground nodes 
    fg1.position.x -= fgSpeed;
    fg2.position.x -= fgSpeed;

请帮帮我!我绝对不会发生什么事情。谢谢!

更新:uliwitness,这是printlns:

func updatePos(timeSinceLast:CGFloat)
{
    println("timeSinceLast: \(timeSinceLast)")
    println("BG_TIME_SPEED: \(BG_TIME_SPEED)")

    bgSpeed = BG_TIME_SPEED*timeSinceLast
    fgSpeed = FG_TIME_SPEED*timeSinceLast

    println("bgSpeed: \(bgSpeed)")

    bg1.position.x -= bgSpeed
    bg2.position.x -= bgSpeed

    fg1.position.x -= fgSpeed;
    fg2.position.x -= fgSpeed;

并且继承控制台输出:

timeSinceLast:0.0165845339652151

BG_TIME_SPEED:120.0

bgSpeed:1.99014407582581

更新#2:调用updatePos的代码:

override func update(currentTime: CFTimeInterval) {

    timeSinceLastUpdate = currentTime - lastUpdateTime
    lastUpdateTime = currentTime
    backGroundNode.updatePos(CGFloat(timeSinceLastUpdate))

1 个答案:

答案 0 :(得分:0)

精氨酸!我想到了。第一次更新时,update函数的currentTime属性类似于50000,lastUpdateTime变量初始化为0.0。因此,前景精灵在第一帧移动了50,000个单位。感谢所有的回复! Cocos2D的建议帮助我找到了问题。