To actually see the content of a scene in a playground, we use the XCPlayground function XCPShowView
. This function renders the view live in the timeline editor so that the game is visible. From this point on, every change you make will be rendered live and be visible in the timeline editor.
Firing the Cannons
When the cannons fire, let’s add a balloon and move it across the scene. The balloons are sprite nodes, and we'll give each balloon a texture with a random element from our collection of balloon images.
Here, we use the map
function of Swift arrays to create an array of SKTexture
objects from an array of image names. With our array of textures, we can simply generate a random index within its range and then create a sprite node with the texture at that index.