ARRAYCEPTION Update - Feb 23, 2012, 12:50pm

In my previous post I talked about how I know that I should be sticking away from global arrays…in this post I’m going to talk about how I now have a global array of global arrays. Yes, I am facepalming right now.

I mean, it’s working. It’s doing what I need it to do (as far as what I’ve built so far - it actually needs to do much, much more), but it feels extremely messy. I have no doubt that I’m probably making this more convoluted than it has to be.

So here’s what I have:

The rules

If the player wants to delete part of a path They have to click on the nodes in backwards order (so first in last out) to deselect the parts they want to change.

If the player wants to delete the entire path They can click on their selected base, which both deselects the base, removes it from the base array, and clears its entire path array.

How I’m currently doing it

Clicking on the base

When the player clicks on a base entity, the empty path array gets created in that base entity. At the same time, that base is pushed to the base array, which is a global array in the controller class. This is done like this:

[crayon title=“base.js”] clicked: function () { if (this.clickcount == 0 ) { this.arrayname = [this]; // Create path array; // Add this base to the base array this.selected = true; console.log(; this.clickcount++; }

else if (this.clickcount == 1 ){
    // Remove coordinates of clicked base from path array
    // Iterate through base array
    for(var i =; i >= 0; i--){
        // Test base for match
        if([i] == this){
            // Splice base from array when match is found
            this.selected = false;
            this.clickcount = 0;
    // Clear entire path array (base is required for the path to remain)
    this.arrayname = []; // NOTE: Remember to try clearing this properly by changing to this.arrayname.length = 0; instead.

}, [/crayon]

Selecting the path

When a base is selected, the player can draw a path from that base along existing nodes in node.js:

[crayon title=“node.js”] clicked: function() { // Get name of first path array from array of selected bases (base array) var path_array = eval([0].arrayname); // if at least one base is selected and the current node is inactive, do this: if (this.clickcount == 0 && > 0) { // Add this node to the path array path_array.push(this); this.currentAnim = this.anims.selected; this.clickcount++; }

// If this node is already in the path array, do this:
else if (this.clickcount == 1) {
    // If this is the last node in the path array, do this:
    if (this == path_array[path_array.length-1]) {
        // Remove clicked node from path array
        this.currentAnim = this.anims.deselected;
        console.log ('Time to pop!');
        // Reset click count to 0 so that node can be selected again if re-clicked
        this.clickcount = 0;

}, [/crayon]

The player can then select the destination in the same way as above, except the destination node is its own entity (in destination.js) and there is only one instance of it on the screen.

Moving along the path

When the player has a complete path that starts from a base entity and finishes at the destination entity in the path array, they can click Go in Gobutton.js:

[crayon title=“Gobutton.js”] clicked: function() { var path_array = eval([0].arrayname); if (path_array[path_array.length-1] == { // Call the ‘spawn’ function of the first item in the path array, which is always the base path_array[0].spawn(); } }, [/crayon]

The first item (the base element) is then shifted off of the path array, followed by the entities being spawned moving through the remaining nodes.

Array naming and stuff…

The problem here is that I’m doing this dodgy roundabout way of defining the path array. In the base.js init function:

[crayon] = ‘base_50px’ +; // Set what the name of this array is going to be var arrayname = ‘’ +; [/crayon]

So I’m kind of naming it like I would define a global array, and then using eval to actually let myself refer to it from other entities…

So I’m calling this thing ‘arrayname’ in base.js, but in other entities I then have to use arrayname along with the global base array to set a new name for it:

[crayon]this.path_array = eval([0].arrayname)[/crayon]

I can then push(), pop(), etc as required by referring to the array as this.path_array. However, this means I now have two different names for the same array in different places - in base.js where the array is set it’s “arrayname”. Everywhere else it turns into “path_array”. This also means I have to define path_array from scratch each time I want to use it in a new entity. It works in the sense that it’s functional, but it seems all kinds of screwed up.

In addition

In addition, James and Simon suggested some different ways to move the entities from node to node when Go is clicked, which I’m also itching to try out. I’ve been holding off for now as I want to get the rest of this path follow stuff done and the range checking I’m doing now is working for the time being, but it’s something I have to remember to go back to and try.


Feb 23, 2012 12:50pm

I modified this somewhat at lunch. I am now doing this in the Controller class:

[crayon] = eval([0].arrayname)[/crayon]

And then simply referring to whenever I need to use the path array. I think originally I had this screwed up attempt to not use a global array for this, but it ended up being a roundabout way to refer to a global array anyway…this is just easier. I’m hoping I’ll still find a way around having to do this in the future, but now I can just refer to and when needed.

I’ve also created a new function in node.js to clear all nodes when the base is deselected. I’m having some weird problems with the animation not changing over for this, but otherwise it’s working. Will have a proper look at the animation problem tonight.

© - 2021 · Liza Shulyayeva ·