我可以在转换过程中将SKSpriteNodes显示在不同的场景中

时间:2015-12-21 21:27:26

标签: swift sprite-kit skscene

我正在制作游戏,并且正在进行自定义转换。单击TitleScene上的playButton时,它会移动GameScene。这是它的GIF。

enter image description here

现在我想让这些按钮落在另一个场景,或者在另一个场景的顶部看到。所以基本上按钮需要在GameScene上,即使它们是TitleScene的一部分。这是我的过渡代码:

let transition = SKTransition.revealWithDirection(SKTransitionDirection.Up, duration: 2)
transition.pausesIncomingScene = false
transition.pausesOutgoingScene = false

currentScene.view?.presentScene(futureScene, transition: transition)

我不确定如何做到这一点。需要任何帮助。

更新

我意识到这样做的唯一方法是将TitleScene中的SKSpriteNodes复制到GameScene。检查此代码:

func createTransition(transitionFrom presentScene: SKScene, to futureScene: SKScene, withTransition transition: transitionTypes) {

var yPositions : [CGFloat] = []
for child in presentScene.children as! [SKSpriteNode] {
    yPositions.append(child.position.y)
}
let topSprite = yPositions.maxElement() // finds the sprite closest to the top

for child in presentScene.children as! [SKSpriteNode] {
    if child.position.y == topSprite {
        var vel = CGFloat.random(-1, 1)
        if (vel < 0.3 && vel >= 0) {
            vel += 0.5
        }
        if (vel > -0.3 && vel <= 0) {
            vel -= 0.5
        }
        let fallAction = SKAction.applyTorque(vel, duration: 0.5) // slightly turns the Sprite

        presentScene.physicsWorld.gravity.dx = CGFloat.random(-0.3,0.3)

        child.physicsBody?.affectedByGravity = true // Become affected by gravity and falls

        child.runAction(fallAction, completion: {
            for otherChild in presentScene.children as! [SKSpriteNode] {

                if otherChild.name != "background" { //copies all Sprites except the background 

                    let copiedChild = otherChild.copy() as! SKSpriteNode
                    copiedChild.position.y += futureScene.frame.size.height

                    futureScene.physicsWorld.speed = presentScene.physicsWorld.speed
                    futureScene.physicsWorld.gravity = presentScene.physicsWorld.gravity //makes it so they have the same physicsWorlds

                    futureScene.addChild(copiedChild) 
                }
            }
        })

        let transition = SKTransition.pushWithDirection(SKTransitionDirection.Up, duration: 2)
        transition.pausesIncomingScene = false
        transition.pausesOutgoingScene = false

        presentScene.view?.presentScene(futureScene, transition: transition)
    }
}

}

这就是它的样子:

enter image description here

你可以看到他们没有正确排队,有人知道我错过的东西吗?

2 个答案:

答案 0 :(得分:1)

场景1:一个大场景

+------------+   }
|            |   }
|   Title    |   }
|            |   }
|            |   }
|            |   }   Initially visible half of full scene.
| btn    btn |   }
|    Play    |   }
| btn    btn |   }
+------------+   }  }
|            |      }
|            |      }
|            |      }
|            |      }
|   game     |      }   Half of full scene visible after play button is pressed
|            |      }    (the game)
|            |      }
|            |      }
+------------+      }

按下Play时,这个大矩形(14x19)向上移动,以便可以看到底部矩形而不是顶部矩形。因为这在技术上是一个大场景,按钮​​/标题可以在这两个场景中自由移动&#39; (这真的只是一个大场景。)

场景2:两个场景&#39;重叠

             +------------+   }
             |            |   }
             |   Title    |   }
             |            |   }
             |            |   }
             |            |   }   Initially visible half of big scene.
             | btn    btn |   }     z-pos of the rectangle on the right is higher than
             |    Play    |   }      the z-pos of the one on the left, thereby allowing
             | btn    btn |   }     its contents to be visible above those of the gamescene.
+------------+            |   }  }
|            |            |      }
|            |            |      }
|            |            |      }
|            |            |      }
|   game     |            |      }   Half of scene and gamescene visible after play button 
|            |            |      }    is pressed (the game)
|            |            |      }
|            |            |      }
+------------+------------+      }

这些场景重叠,最右边的场景的下半部分与最左边的场景匹配。按下播放按钮时,两个场景以相同的速率向上移动,直到只有底部场景可见。实际上,最右边场景的下半部分仍位于较小场景的顶部。

较大场景的alpha0。对于大场景的上半部分,您可以添加背景节点;此设置允许游戏可见,按钮仍然落在游戏之上。

为了确保不存在无法与游戏场景交互的问题,因为技术上有一个场景(可能会阻碍交互),删除较大的场景或将其z位置更改为低于gamescene。这可能没有必要,但万一是......你知道。

请记住,您无法真正嵌入场景(至少根据this),因此您无法在场景2中使用两个场景。您也可以将游戏场景置于SKNode或类似的场景中这是一种解决方法。

答案 1 :(得分:0)

您应该创建按钮的副本并将其添加到GameScene

GameScene上按钮的初始位置设置为TitleScene上按钮的最后位置。

希望效果将是您正在寻找的效果。