GameView Tutorial Part 1
GameView Tutorial Part 1
Now you have created your new game and played with
the demo script, we need to modify it so we can start
building our own game.
At the top you will see two files, one with the game
name and another called Bounce.js. We don’t need the
Bounce.js file any more as we will create our own new
game file so press the trash can symbol on the tab to
delete it. A popup will ask if you’re sure, click OK.
A game will always need these two files. The first is the
main program that creates the app and launches the
came code, and the second is the file that actually
contains the game code.
The last thing we need to do to set up the game is change the name of the game file in the first script
from “bounce.js” the name of our new file. Here it is “gamefile.js”. We are now ready to start
building a game! All of the following code will be written in the game file.
Lesson 1 – Anatomy of the Game
http://androidscript.org/tutorials/assets/
Lesson 3 – Creating Backgrounds
gfx.CreateBackground(), gfx.AddBackground()
The next step to creating a game is choosing, creating and adding a background. This requires two
lines of code, one in the OnLoad() function like this:
And one in the OnReady() function which adds the previously created background to the GameView,
like this:
The file name must match the name of the Assets we added in the previous step. If you try running
the app now, you should see the background image appear.
Lesson 4 – Scrolling Backgrounds
img.Scroll()
Some games will use a still background, but most will use a scrolling background to give the
impression of the player moving through an environment. The Scroll( x, y ) function is given two
values, of the amount moved in each direction.
All size and position values when building a game are given as fractions of the total screen width; this
is so that it can be played on any device regardless of the screen size and orientation. Here the sky
image is scrolled to the left by 2% of the total screen width, each time the frame is updated.
Add the above code to the “OnAnimate” function and if you run the app, this time you should see
the background scrolling to the left.
Lesson 5 – Adding Characters (Sprites)
gfx.CreateSprite(), gfx.AddSprite()
Characters in the game are known as sprites. These need to be added in the same way as the
background, created in the OnLoad() function and added to the GameView in the OnReady()
function.
The numbers after ‘dino’ when adding the sprite show the x and y position that the image will
appear on the screen. These positions are given as fractions of the screen width and height away
from the top left hand corner. This corner would have a position of 0,0.
gfx.CreateSound( ), snd.Play()
Sounds effects and game music are also created in the OnLoad() function. When you want to play
the sound, you use ‘.Play’. If the sound is game music it can be called in the OnReady() function, or if
it is a sound effect you would call it in the OnAnimate() function. Here is an example of game music:
//Create sounds.
jungle = gfx.CreateSound( "Snd/jungle.mp3" )
}
Sprite Animation
The game engine knows how to split this image up using the details in the file name. For example
the sheet shown here has the filename: ‘dino_fly_783x136x3x1’ The numbers after the name state
that the total image size is 783x136 and the frames are split into 3 columns and 1 row.
//Start animations
dino.Play( 0, 0.25 )
If the filename has no extension (.png) the game engine will assume the image is a sprite sheet.
Lesson 8 – Character Animation – Movement
//Create sounds.
jungle = gfx.CreateSound( "Snd/jungle.mp3" )
}
//Start animations
dino.Play( 0, 0.25 )
croc.Play( 0, 0.05 )
To animate your character, you will need to change its position each time the frame updates. In the
example below the ‘croc’ object is shifted by 0.4% of the screen width to the left ( -x direction ).
An alternative and faster way to write ‘croc.x = croc.x - 0.004’ would be: ‘croc.x -= 0.004’.
Before we are ready to move onto the next feature, we need to test what we have learnt so far. The
following code includes all the features covered so far and should produce a flapping dino in front of
a scrolling background. A croc will also appear and move across the screen when the app is first run.
So far the player does not have any control and there is no way of detecting collisions between the
dino and croc. This will be covered in the next lesson.
//Create sounds.
jungle = gfx.CreateSound( "Snd/jungle.mp3" )
}
//Start animations
dino.Play( 0, 0.25 )
croc.Play( 0, 0.05 )
We also need to add the sound effect that plays when the
dino collides with a croc and change the sprite sheet to the
crashed dino.
//Global variables
var gameOver = false
//Create sounds.
jungle = gfx.CreateSound( "Snd/jungle.mp3" )
crunch = gfx.CreateSound( "Snd/crunch.mp3" )
}
If we try the game now, we can avoid the croc but there is nothing to bring our dino back down
again. To make the game more fun, add this line to the OnAnimate function:
Now our dino will drift downwards towards the ground making it harder to avoid the crocs.
Lesson 12 – Scoring
Now we can avoid the croc we should do two things: add more crocs and keep track of how many
we have avoided. To do this we can detect when the croc has gone off the edge of the screen and
reset its position to the other end of the screen, and when this happens, add 1 to a score variable.
To do this first we need to add some text to the game to display the score. There are two font files,
(Desyrel.png and Desyrel.xml) that must be saved in the same folder as the images. You will need to
select the file type as “All Files” otherwise you will not be able to find it.
The score variable must be outside the function – making it a ‘global variable’ which means any part
of the program can see its value.
//Global variables
var gameOver = false
var score = 0
function OnLoad()
{
//Create background (and stretch it to full screen).
sky = gfx.CreateBackground( "Img/flappy_back.jpg", "stretch" )
//Create sounds.
jungle = gfx.CreateSound( "Snd/jungle.mp3" )
crunch = gfx.CreateSound( "Snd/crunch.mp3" )
}
//Start animations
dino.Play( 0, 0.25 )
croc.Play( 0, 0.05 )
All we then need to do is add the game over image over the top of everything else when the dino
crashes. To do this we create a new function called “GameOver()” do add the image and play the
sound and call this function 1 second after the dino crashes.
The “setTimeout()” function calls another function after a specified delay in milliseconds.
function GameOver()
{
gfx.AddSprite( gameover, 0, 0, 1, 1 )
//Play 'game over' sound after half a second.
end.Play( false, 500 )
}
Everything so far!
By now we should have a very simple playable game. The full code is given here showing everything
we have learned so far:
//Global variables
var gameOver = false
var score = 0
//Handle game loading.
function OnLoad()
{
//Create background (and stretch it to full screen).
sky = gfx.CreateBackground( "Img/flappy_back.jpg", "stretch" )
//Create sounds.
jungle = gfx.CreateSound( "Snd/jungle.mp3" )
crunch = gfx.CreateSound( "Snd/crunch.mp3" )
end = gfx.CreateSound( "Snd/game_over.mp3" )
}
//Start animations
dino.Play( 0, 0.25 )
croc.Play( 0, 0.05 )
function GameOver()
{
gfx.AddSprite( gameover, 0, 0, 1, 1 )
//Play 'game over' sound after half a second.
end.Play( false, 500 )
}
End of Part 1, see Part 2 for more advanced game features such as physics!