Back in the Swing of Things

It’s been a while, but it’s good to be back.

Finch Masters
6 min readMar 25, 2017

A combination of life getting in the way and working myself to death kept me from staying on track these past couple weeks. Don’t worry about that, it’s all good now. Let’s see what kind of mess I’ve gotten myself into this time.

First and Foremost

Links. They’re always the most important part of the updates. Without further ado, you can view the code here and try playing the game here. Side note: I need to give some props to Vanilla and Flamingosis for providing excellent music to accompany coding recently.

Alright, what’s new?

Remember that promise from the last post? “Next week I’m committing to having at least one enemy type show up on the map…” Yeah that one. Well, I’m happy to report that I’ve got something that barely qualifies as meeting that goal!

Almost cute, isn’t it?

Yep. That’s my little enemy. He’s got a health bar above him and he’ll get hurt if you shoot him.

No, come back!

Watch out though, because he can hurt you too.

Ouch!

There was also this weird bug in the knockdown block I previously implemented:

Don’t leave me!

And here’s how it works now:

That’s right. You stay there and think about what you did.

So how does it all work?

Honestly the implementation of these fixes/features was fairly simple compared to previous attempts. I’m getting the hang of working in this framework and I could rely more on what I already knew as opposed to having to learn new pieces of it for this round.

The first thing I’ll get into is something I mentioned back in the first post about this game: swapping local for world gravity. All I had to do was remove the local gravity that I had set previously on the player and the knockdown block and then enable it for the whole game world.

However, there was at least one unintended consequence of doing this:

That’s not how bullets work. Well it is, but over a very long distance instead of this.

I had totally forgotten that world gravity would, you know, apply to the whole world. Simple fix though. All I had to do was set the sprite’s physics body’s allowGravity to false.

Easy enough, right?

What about the enemy?

Also easy. Remember how I setup a group for blocks and one for bullets? Well I figured I was going to have more than one enemy in a level at once, so I setup a group for enemies as well.

Just like old times.

Of course, I had to make a function to create an enemy so that I could populate that group.

Here I’m setting up the physics exactly like I did with my function to create the player and I’m setting the built in health member as well as my custom maxHealth and healthBar members. This way I can keep everything that the enemy object cares about contained to an instance of that object.

That can’t be all.

You’re right. I had to implement collisions and health manipulation as well. Before we get to that though, I’d like to introduce you to a method that Phaser provides that solved the runaway falling block problem. That method, of course, is called overlap. It is called the same and used in the same way as we have used the collide method, but it only checks whether or not the two sprites that are compared overlap (duh). Basically, I used this to see if a bullet touched a block without the bullet applying any physics to said block. Check out this example:

In the first line, we’re checking to see if a falling block collides with a floor block. If so, we want to apply physics; in particular, we want the falling block to recognize that the floor block is immovable and to stop the falling block from moving.

In the second line, we’re checking to see if any of the bullets hit a block, but we don’t want the bullet to apply any force to the block at all. For this situation, overlap does exactly what we want.

Well that’s nice, but back to that enemy…

Fine, fine. You know patience is a virtue. You should work on that.

Anyway, I needed explain the overlap method first because it came in handy when working on the enemy’s interaction with the player.

Here we can see the collide method being called to make sure the enemy doesn’t fall through the floor as well as overlap being called to handle what happens when the enemy is hit by a bullet and when the player touches the enemy.

First let’s see what happens when a bullet hits an enemy:

Most of that should make perfect sense. Get rid of the bullet with its kill method, decrease the enemy’s health, and get rid of the enemy if it’s dead. The only slightly tricky thing here is shrinking the size of the health bar. To do this, I called the scale.setTo method of the enemy’s healthBar member. The width value I supplied was created by taking the initial scale factor I applied (2) divided by the maxHealth of the enemy to get the size of each increment of health all multiplied by the amount of health left.

The player/enemy overlap handler introduced me to the last bit of new functionality in the Phaser framework that I’ll be going over. That functionality is the repeat event. Let’s check it out and then discuss.

Just like the function for handling bullets hitting the enemy, I’m decreasing the player’s health and scaling the health bar. However, I am creating an event that I want to repeat a set number of times. The parameters I’m providing are (in order): the amount of time from now I would like the events to start, how many times I want to repeat the event, what should happen each time the event occurs, and the context to execute the event within.

In plain English I’m saying:

  • I want to call my custom flashPlayer function 6 times
  • I want it to start in 1/6th of a second
  • I want flashPlayer to execute in the context of this state so that I can access the player object

After I set the events in motion, I want to push the player away from the enemy so I manipulate the player’s x and y velocity values as needed.

All done?

Yep. That’s it for this post. Since the hectic nature of my schedule doesn’t seem like it’s going to slow down any time soon, I’m going to officially change intended release dates from weekly to bi-weekly. Additionally, I haven’t settled on the next feature(s) that I want to implement yet. I’m thinking about setting up health recovery and adding enemy movement but I’ve got quite a few other ideas. I’ve also found at least one new bug to squash so…I guess we’ll see!

If you have any questions, comments, or concerns just let me know. I’ll be happy to respond to anything and everything. Like, share, and follow and eventually, we’ll all be game devs.

--

--