Marty’s Blog

Marty Wallace is a web developer based near Sydney; passionate about the development of web applications and small games.

Creating instances with new static and new self in PHP

Until Friday, I never knew is was possible to use the new operator with self and static to create instances of the relevant class. This is interesting because it allows you to access instance level members from a static method of a class, e.g.

class Example
    protected $name = 'first';

    public static function test()
	$instance = new self();
        return $instance->name;

In this example, Example::test() will return 'first'. If we extend this class and call test via that instead:

class Extending extends Example
    protected $name = 'second';

Extending::test() will still return 'first', because self refers to the class that defines test. If we want to refer to the calling class (Extending in this case), we can use static instead of self like this:

public static function test()
    $instance = new static();
    //              ^^^^^^ Instead of self.
    return $instance->name;

Meaning that Extending::test() will now return 'second'.

Pretty good bit of information that I came across while trying to work out how Laravel's Eloquent ORM worked.

Xbox controller support for ActionScript 3 games

Yesterday I noticed that Adobe had released a few APIs for dealing with Xbox and OUYA controller support for Flash games, so I went out during lunch and purchased an Xbox controller that I could experiment with.

Xbox controller

I spent a couple hours researching and playing around with the APIs, and eventually wrote something usable that wraps those APIs for working with an Xbox controller. The classes are available here and are pretty straightforward to implement. The 2 important classes are XboxInput and XboxController, used like so:

import exalted.input.XboxInput;
import exalted.input.XboxController;


function update(e:Event):void
    var controller:XboxController = XboxInput.getController(0);

    if(controller !== null)
        // Check for input.

addEventListener(Event.ENTER_FRAME, update);

Once the controller is not null, you can determine whether buttons are currently held down on the controller using .buttonIsDown():

    trace("A is held down.");

You can also access the current pressure applied to the left and right triggers, which ranges from 0 (not pressed at all) to 1 (fully pressed down):


And you can determine the x and y positions of the left and right joysticks, which range from -1 to 1:

trace(controller.leftStick.x, controller.leftStick.y);

I haven't tested this with multiple controllers yet, so I can't comment on how well that works at this stage. I do intend to add more features to my wrapper (e.g. buttonWasPressed() and buttonWasReleased()) and fully test multiple controllers connecting and disconnecting.

pop() vs splice() for unsorted arrays

Often when removing items from an array in JavaScript or ActionScript, splice() will be used to remove the item in question:

var i = array.indexOf(target);
if(i >= 0) array.splice(i, 1);

There is a massive downside to this approach that most inexperienced developers aren't aware of, which is that this approach will need to re-index every other element after the target. This is an extremely slow process, especially if the target is near the start of the array. If the target was the number 3, the process could be visualized as:

1,2,3,4,5 // Starting array
1,2,x,4,5 // First iteration
1,2,4,x,5 // Second iteration
1,2,4,5   // Final iteration

As you can imagine, the process lengthens as there are more items to shift down the array. A method that I use in cases where there are many items that need to be rapidly added and removed, but where a linked list or other simpler data structure isn't adequate, is to use pop().

pop() removes the last element from an array, a process which does not affect other elements' positions in the array. If this element isn't the target, and we know the index of the target that we want to remove, we can replace the target with the result of pop(). This removes the need to re-index the array because the target index is not being emptied, just filled with something new. That code looks like this:

var i = array.indexOf(target);
if(i >= 0)
    if(array.length === 1 || i === array.length - 1)
        // Simply remove the only remaining element.
        // Replace target with last element.
        var tmp = array.pop();
        array[i] = tmp;

This process looks more like this:

1,2,3,4   // pop()
1,2,5,4   // replace target

No matter the length of the array, this process will always remain 2 steps long. Of course, this is not a good idea if the array needs to stay in the same order.

Space trading / RPG concept

I had the chance a few weeks ago to do a little bit of game development. I didn't have anything in particular that I wanted to work on, but I wanted it to be something easy to get up and running, in an attempt to get myself into the flow and potentially start working on an actual complete project.

I only got about 2 full days into it, but I ended up taking the route of a pixelated 2D space game:

Space game

Given the chance to continue development, I would aim add 3 main components:

  1. Mining & gathering.
  2. Trading (travelling between different areas to get better buy & sell prices on goods mined or collected).
  3. RPG elements including ship customization / advancement and a wide range of loot. Also included would be missions / quest which would eventually result in game completion.

I liked where what I had was going, but unfortunately I don't have the time at this stage to continue development. If I do get the chance, I'll post additional progress and likely a gameplay demo.

The game was being developed in ActionScript 3, using Exalted (my own incomplete game engine), which makes use of Starling for high-performance 2D rendering and Box2D for physics.


I've decided to go with a straightforward, plain blog look with minimal styling. Most of the reason is the fact that I'm not a designer, and don't have the talent to make something appealing as a result. Another reason is the fact that I'd like to keep my site fast and working nicely across devices (I've found that it's actually quite an effective decision for mobile).