2D Space Shooter Part 2: Background
A bit of set up
Let's begin by setting up our scene.
We want a scrolling starry background. For this we'll use the repeatable texture on the right.
Because it is repeatable, its dimensions must be a power of two. I choosed a width of 512 for the texture, so let's configure our project according to that width.
In your project settings (create a new project for this tutorial if not already done!), go the 'Graphics' tab and set:
- Scale mode to 'Fit Width'
- Logical dimensions to 512 x 1024
- Orientation to portrait
This will ensure that whatever the phone size is, our canvas will have a width of 512. The height doesn't matter actually, you can use anything instead of 1024 but it is convenient to specify something sensible, that is taller than large, since we want our game to be in portrait.
However we'll need to figure out the actual height and logical position of our screen at run time, in order to place our graphics on the stage.
The code below will do what we want, and we'll place it at the very top of our main.lua:
-- Compute top and bottom screen bounds
local _,scr_top,_,scr_bottom=application:getLogicalBounds()
Next step is to create our background and put it on stage. To keep our code clean, let's handle the background image as an object of class Background. We'll define it after.
-- Create the background object with the screen size (width is 512 per project settings)
local background=Background.new(512,scr_bottom-scr_top)
-- Add it to stage
stage:addChild(background)
background:setY(scr_top)
Our background will move as our spaceship progress through the game. We'll make it 'advance' in our game loop:
-- This is our game loop
stage:addEventListener(Event.ENTER_FRAME,function ()
background:advance(1)
end)
The code of our game loop will be called on each animation frame, 60 times per second. In it, we call the 'advance' method of the background object with a parameter telling it by how much we want to 'move'. This will allow us to alter game speed later if we ever need to.
So far our main.lua contains:
-- Compute top and bottom screen bounds
local _,scr_top,_,scr_bottom=application:getLogicalBounds()
-- Create the background object with the screen size (width is 512 per project settings)
local background=Background.new(512,scr_bottom-scr_top)
-- Add it to stage
stage:addChild(background)
background:setY(scr_top)
-- This is our game loop
stage:addEventListener(Event.ENTER_FRAME,function ()
background:advance(1)
end)
You can try to launch your project, but obviously it won't run yet, because we didn't define the 'Background' class.
A basic background object
Create a new lua file and call it 'background.lua'. In this file, we'll define the 'Background' class. Our background object will mostly display an image, so let our Background class be a subclass of Gideros Pixel object:
-- Our Background will be a subclass of Gideros Pixel.
-- Here we pass an empty constructor function so that the pixel is initialized with defaults
-- We'll intilialize it by code later
Background=Core.class(Pixel,function () end)
The second argument to the Core.class function is used to specify the parameters to use when constructing the 'Pixel' we inherit from. Here this is just an empty function, meaning that we don't pass any parameters to the Pixel.new that will be called internally.
Now let's add empty functions for our class so that our code runs.
--Initialize our background with the given width and height
function Background:init(w,h)
end
function Background:advance(amount)
end
The 'init' method will be called by main.lua during 'Background.new', while the 'advance' method is called from our main loop. If you start a Gideros Player and hit play, our code will run, but won't show anything.
Let's add some code to:
- load our texture
- attach it to our background
Add code to your empty 'Background:init' function:
function Background:init(w,h)
local far=Texture.new("gfx/star_far.jpg",true, { wrap=TextureBase.REPEAT })
--Set up the far view
self:setTexture(far)
self:setDimensions(w,h)
end
This tells Gideros to load our image called 'star_far.jpg' from the 'gfx' folder of your project structure, and make it a texture. We also ask gideros to enable filtering ('true') and that the texture should repeat the image ('{ wrap=TextureBase.REPEAT }'). We then assign this texture to our background object. Remember that Background is a subclass of Pixel, so it has all properties and methods of a Pixel object. The final line changes the size of the background to the dimensions given in arguments.
We still need to add the image to the project! Let's do it now:
- Create a folder named 'gfx' under 'Files' in your gideros project
- Download the File on your computer, rename it to 'star_far.jpg', and add it to your project under the 'gfx' folder you just created.