Wednesday, 3 June 2015

Game#4 : Galaxians

I'm back. I've fixed the bug (see later) and I've also uploaded the new .capx to the github account.

As an additional (err....) treat, I've uploaded a built version of the game via ftp, so you can now play it by clicking on this link, keyboards only :)

As this is a bit longer, I'll talk through it bit by bit.

The first bit is relatively simple, it creates the slowly scrolling star backgrounds, which are stars with a setColor effect, so they ripple through colours. They are set to wrap so they keep going round and round.

This is just a single layout, for simplicity, so Event 4 sets everything up on startup and disables firing.

Event 5 enables firing when the "Get Ready" has faded out.

Events 6 through 8 make the galaxian "fleet" move left to right, flipping whenever they hit the edge (nearly) hence the X < 16 or X > LayoutWidth-16

The next section deals with the swooping down bit, where aliens detach from the main fleet. This is done using a timer.(9 onwards). I didn't realise here that timers could be random, so you could have a repeating timer with a random timeout rather than what I do here, fire it once and keep firing it when it fires :)

LaunchDirection here sets whether it attacks left (-1) or right (1) as the ships attack diagonally. 11 picks a ship that attacks.

There is a trick ; as you can see from the picture, aliens can also attack in groups of up to four. To make a whole group of four attack at once, the LaunchCollider is used - this is created and looks like an upside down yellow "T". Collision with this, in 14, sets the "shouldLaunchAttack" flag for each of the ships the LaunchCollider covers.

Event 15 checks to see if any ships have their "shouldLaunchAttack" flag set, and if so they are put into "isAttacking" mode (e.g. they will come down the screen). The bullet is disabled (the bullet is used to do the left/right movement) and the attack direction is stored. Finally it makes a screechy attacking noise.

The attacking is continued in 16 and 17, where it moves according to the attack direction and the speed scalar (how fast the game is running) and it reverses direction if it goes off either edge.

Event 18 is a bit of a cheat. In real Galaxians, if an attacker drops off the bottom, it comes back on the top, dropping down into position. Here, I've faded it into the correct position and set the instance variables/bullet enabling accordingly.

Ah, now how does it know where to go. Well this is what the marker is for. It is a red X at the top left of the "alien pack". The aliens position relative to this is stored in startX and startY (in Event 8) and even if the whole alien pack moves, this relative position to the red "X" (you can see it in the CapX) tells it where to go back to.

This (and the LaunchCollider) are the two tricks that make Galaxians operate. The use of the 'shouldLaunchAttack' instance variable is quite useful too, as it allows more than one action to cause an attack - it can be done individually on the random choice, or through collision with the LaunchCollider.

Beyond that it is fairly straightforwards. Players and Aliens can fire bullets (19-32) and if the player is hit, then he loses a life and the game is over if there are no lives left.

The bug was in Events 26 and 27. Originally the Condition 26 (hitting ships or enemy bullet) fired Event 28 (reduce lives). If the player got hit by more than one thing at the same time, more than one life was lost - it was possible to lose all three lives in the one 'hit'. Using the "playerHit" global works round this - it only occurs once. I was going to use Pick 0th Instance but don't think this works with the "or"

It still has faults ; the speed adjustment stuff doesn't work very well and doesn't reset properly :) Really, I need to move all this into a function, it gets rather fast rather fast.

No comments:

Post a Comment