Bomb Item, cont.

Hey guys,

The bomb item is now working!

This one was a little challenging, because the explosion collision to clear the tiles wasn’t behaving the way I wanted. It was clearing the bricks along the outside edges of the explosion.

I added a green debugging square that shows the area of the explosion. As you can see from this screen shot, the bricks just outside the square were also cleared, which was not the intended effect.

After some tinkering, I found that Phaser’s Rectangle class performs intersections a little differently than expected.

It’s when the right and bottom edges of a rectangle are calculated, they’re including an extra one unit, instead of subtracting one.

For example, say you have a rectangle located at x=10, and y=14, and it has a width of 16, and height of 5. The right edge would be located at x + width (10 + 16) = 26, and the bottom edge is at y + height (14 + 5 = 19).

I was expecting the right edge to be at 10 + 16 – 1 = 25, and the bottom edge to be at 14 + 5 – 1 = 18. So that extra offset of one was including the bricks around the explosion edge.

Since I didn’t want to modify Phaser’s internal method when determining if two rectangles intersect (it’s solid for everything else; I just needed a specific behavior here), I used a modified version of it by subtracting the offsets from the right and bottom of both rectangles:

!(rect1.right - 1 < rect2.x ||
rect1.bottom - 1 < rect2.y ||
rect1.x > rect2.right - 1 ||
rect1.y > rect2.bottom - 1);

And vwalla! (:

The result was an explosion that did not clear the bricks just outside the explosion! Look at that snug fit! Keep it tight!

 

And the final look is here:

I’ll polish this up later, but for now, I’m moving on to the next items. Stay tuned!

~ C. out.

[Solved!] Collisions not Working Properly

Hey.

So, while working on Brickout, I was adding a feature that allows the bricks to descend one level after clearing so many of them, much like Atari’s Super Breakout.

Everything was working fine, until I encountered a problem. Eventually, the collision between the ball and bricks wasn’t working. The ball was going right through some of the bricks.

Using Phaser CE 2.10.5, so I double-checked what I was doing when I clear bricks. All the bricks are in a group, and that group is being tested for collision against the a group of balls like this:

arcade.collide(m_ballsGroup, gameMap.bricksGroup, onBallBrickCollide, null, this);

This phenomenon was strange because, all the ball sprite has a physics body on it (using Arcade), as well as every brick sprite, so I wasn’t sure why the ball would collide with some and not others.

I turned on debugging, so I could see the physics bodies, because it looked like somehow, some brick sprites were missing their bodies. And, as you can see here, all the brick sprites have bodies, and the ball was going right through them! Argh!

Ok, I was pretty sure I was doing something simple (and dumb) here, but I needed to jump a bit out of my comfort zone, and take some peeks into the Phaser source code. Since it seems to be a collision issue, I started with that collide call earlier, and eventually found my way into the Phaser.Physics.Arcade.collideSpriteVsGroup function.

Most of the function was just checking bounds checking, which I knew was not what I was looking for. However, there was a preliminary check against for validation for the sprite objects.

...
// Skip duff entries - we can't check a non-existent sprite or one with no body
if (!object1 || !object1.exists || !object1.body)
{
continue;
}
...

This looked interesting. Since I have the full source of Phaser.js, not just the minified, I set a breakpoint on that continue statement to see if the code would ever execute that line. And sure enough, it did! Hmm! Ok, so I’m not writing any code that makes the bricks or ball sprites not exist, and they all “should” have a physics body on them (note that “should”). So, I added additional checks to see which of those three conditions was true. The object wasn’t evaluating to false, and it did exist, but it didn’t have a body on it (the body property was null). I did wrote some additional code to make sure that the object was either the ball sprite, or one of the brick sprites, and it was always one of my brick sprites. So, somehow, some brick sprites were losing their physics body. Dude. WTF!?

Going back to my game code, the only place I’m destroying a physics body on a sprite is when I’m removing it after being hit by a ball. Destroying a physics body does remove the body from the sprite – _however_, I’m also removing the object from the group altogether. So it should no longer be tested for collisions, right? Riiight??

WRONG!

It took me some time – in fact, it was late night, and decided to call it a night and return to this the following morning – but I did finally figure out WHAT THE HELL’S GOIN ON HEA!

When I removed the brick sprite from the bricks group, I wasn’t actually removing it. I was calling Phaser.Group.removeChild, when I should’ve used Phaser.Group.remove.

That validation check above is enclosed in a for loop that iterates an array of all the objects. But the array isn’t the group’s “normal” array – it’s a separate array inside the group called a hash. The hash is used for z-sorting purposes, by the Phaser Arcade physics, but it’s also used in this collision checking.

...
for (var i = 0; i < group.hash.length; i++)
{
  var object1 = group.hash[i];
  // Skip duff entries - we can't check a non-existent sprite or one with no body
  if (!object1 || !object1.exists || !object1.body)
  {
    continue;
  }
...

And when I called Phaser.Group.removeChild, that didn’t remove the object from the hash. It only removes the object from the group. (Phaser.Group inherits from PIXI.DisplayObjectContainer, and it’s PIXI.DisplayObjectContainer.removeChild that was being called, which has nothing to do with hashes.) This is how sprites with no bodies were being checked for collisions when they shouldn’t have. They were never being removed.

However, Phaser.Group.remove does remove the object not only from the group but the has as well. So, once I changed my code to use remove instead of removeChild, everything worked as expected.

Geez. See I told you. It was something simple (and dumb) I was doing. And I learned a little more about how Phaser works too. So win, win!

– C. out.

Adding Text From a Tiled Map to Phaser

Hey.

When I’m building a game, I prefer to have a visual editor when placing as many objects as possible. This includes user interface such as HUDs and text.

So, I’m adding text to my Tiled map to have Phaser’s Tilemap object load it for display. I’m using Phaser.BitmapText, but Tiled uses fonts installed on your system, so showing the text won’t display as easily as, say, tileset tiles.

To add text to a Tiled layer, you need to first create an Object layer, then you can add the text object to it. The text properties of a text object will look something like this:

When the map is exported to a format that Phaser can use – I’m using JSON – the format of the object looks something like:

{
  "height":32,
  "id":18,
  "name":"txtScoreLabel",
  "rotation":0,
  "text": {
    "color":"#ffffff",
    "fontfamily":"Press Start 2P",
    "pixelsize":24,
    "text":"Score:",
    "wrap":true
  },
  "type":"press2p24",
  "visible":true,
  "width":160,
  "x":32,
  "y":32
}

On the Phaser side, I’ve loaded the JSON map with this:

loader.tilemap('brickout', 'media/graphics/maps/brickout.json', null, Phaser.Tilemap.TILED_JSON);

And created a bitmap font using Littera. It’s loaded into Phaser with this:

loader.bitmapFont('press2p24', 'press2p24.png', 'press2p24.fnt');

Looking back at the JSON format above, you’ll see that the type property has the value of press2p24. The type property doesn’t appear to be used by Tiled for anything. On the Tiled’s JSON Map Format page, it states, “String assigned to type field in editor”. So I decided to use the type property as the key that Phaser uses when defining BitmapText objects.

Also, in the JSON format, some other key properties include x, y, text.pixelsize, and text.text.

I first tried to get the text data by creating a Phaser.TilemapLayer from the layer (named ‘text’) like this:

map.createLayer('text');

This is a layer I used exclusively for text objects in Tiled. So, I’d then iterate through all the elements in the ‘objects’ property of the Phaser.TilemapLayer and create my BitmapText objects like this:

game.add.bitmapText(object[i].x, object[i].y, object[i].type, object[i].text.text, object[i].text.pixelSize);

However, the text was not showing up in the game. It seems all the properties were loaded into Phaser except for the text sub-object. The object[i].text sub-object wasn’t being loaded into Phaser. You can see in this screenshot below, when I inspect the Tilemap object look at the objects property, the first element (0) of the text layer does not have a text sub-object in it.

So, Phaser was not loading in that data. When I inspected the code on GitHub (I tracked it down to the src/tilemap/TilemapParser.js.), the text object was eventually being loaded as a Tiled rectangle object, and there is no code for loading the text sub-object.

But, the raw JSON data that Phaser loads does include all the information needed, including the text sub-object, as shown:

I was able to obtain the raw data with this code:

var rawMapData = game.cache.getTilemapData(tileMap.key);

Then I could get the text objects layer from with rawMapData.data.layers. And from there, the text sub-object to retrieve the text and pixelSize properties.

It seems like quite a bit of work, but not really. Going through the learning process was longer than the solution, actually. (: I just needed a way to specify the data in a visual editor and have it supported in code. I’d prefer not hard-code positions and other visual aspects that should be defined in a visual editor.

And If you’re wondering the font is Press Start 2P. (:

– C. out.

Added Ball and Paddle

Hey.

Just a quick update on this Memorial Day weekend.

Made some more progress and got the ball colliding with the walls and the payer’s paddle. Added a repeating background as well.

Next will be the bricks collision. For now, it’s pulling those multi-colored bricks from a tiles layer in the map, but I’ll be converting those to sprites and just using what’s in the map as placeholders.

I noticed there is an example for this type of game in the Phaser 2 Examples. Considering how I’ve already gotten this far on my own, I’d rather keep my own pace with it. Besides, this engine will go beyond the example in terms of function, visuals, feedback, and polish.

More to come later!

– C. out.

Tools I Use

Hey.

I want to fill you in on the tools and resources I use when I’m coding games. This is not an exhaustive list.

Coding Editors/IDEs

  • FlashDevelop
    My current go-to IDE for all things ActionScript 3. I also like how it can download and configure updates of the Adobe AIR and Flex SDKs.
  • Adobe Flash CS/Animate
    I don’t use this for coding, but for managing assets. While I’m not an artist, graphic designer, game designer, I find its visual editor very easy to use and mostly intuitive.
  • Brackets
    Been using this free editor for a little while. I use it for JavaScript when coding HTML5 games using Phaser.

Programming Languages/Engines/SDKs/Runtimes

  • Adobe AIR
    Despite Flash being retired soon, Adobe AIR is still at large. AIR is not the same thing as Flash – they both happen to use ActionScript 3 and many of the same APIs (with AIR having access to many more of them, partly due to it being able to target desktops). Plus, Adobe AIR can be used to target mobile (Android and IOs) from the same code with minor changes.
  • Adobe Flash
    Yes, I know Flash will no longer be updated or distributed by end of 2020, but until then, hey, it’s still being used.
  • Phaser
    I only care enough about HTML5 and JavaScript because of Phaser and it’s capabilities for game making. I don’t give a damn about web development.
  • Unity
    While I haven’t used Unity to make any games yet, I found a good course on Udemy for it.

Graphics

  • Paint.NET
    I use this for editing of bitmap files. Another simple and intuitive tool to use.

Project Management

  • Trello
    A free online tool for keeping track of my progress, especially when working on larger projects. I use it in a Kanban style, and I’ll have three major categories for tasks: Backlog; Work In Progress; and Ready for Testing. Once the client has tested the latest updates and all’s well, I will archive all the tasks in the Ready for Testing category, and start anew.

Source Control

  • GitHub
    Using Git as the version control system.
    You can find me here, hint, hint.
  • Bitbucket
    I also use Git here, but have tried Mercurial.

Audio

  • Wavosaur
    Simple tool for editing audio files.
  • Audacity
    Not as easy to use as Wavosaur, but it has more functions, particularly in terms of effects you can apple to sounds.

Music

 

So, that’s pretty much the jist of what I use. If you have any questions about anything here, let me know in the comments!

– C. out.