Isometric
Hit Testing
        by Danko Kozar : 3 December 2004

I owe you some explanations about previous code. Let's look at the functions.

Note

In my isometric experiments - for some reasons - I didn't go with extending the MovieClip class to create objects. I've rather created "abstract" objects which among other methods and properties hold coordinates for attached MovieClips from the library.

So, at object creation, we have to supply this MovieClip library name (clip) and it's on-screen instance name (instance), as well as coordinates and sizes.

function object(instance, clip, x, y, a, b) {
this.x = x;
this.y = y;
this.a = a;
this.b = b;
this.clip = clip;
this.instance = instance;
...

Initializing the new on-screen MovieClip with the init function:

// object initialization
this.init = function() {
// putting object on list
linkedList.push(this);
_root.level++;
// attaching MovieClip
attachMovie(this.clip, this.instance, _root.level);
// positioning it
setProperty(this.instance, _x, this.x);
setProperty(this.instance, _y, this.y);
};

In the previous code, you see an array called linkedList. It's the list of objects which is filled during populating room's objects, and then processed in every program loop.

linkedList.length is a parameter which is then used in for..next loops.

Note

In later stage depth sorting will be done by changing object's positions in the linked list.

Next function places the MovieClip to the new screen position:

this.placeIt = function() {
// positioning MovieClip
setProperty(this.instance, _x, this.x);
setProperty(this.instance, _y, this.y);
};

Next two functions are not so important for the whole deal - they are just popping up and removing the balloon with "I OVERLAPPED OBJECT" text:

this.say = function(text) {
_root.level++;
eval(this.instance).swapDepths(_root.level);
eval(this.instance).cloud.text = text.toUpperCase();
eval(this.instance).cloud.gotoAndStop("start");
};
this.shutUp = function() {
eval(this.instance).cloud.gotoAndStop("stop");
};
This is the main moving function:
this.moveObject = function() {
hit = false;
if (this.direction == 2) {
this.x -= _root.speed;
}
if (this.direction == 8) {
this.x += _root.speed;
}
if (this.direction == 1) {
this.y += _root.speed;
}
if (this.direction == 4) {
this.y -= _root.speed;
}
// checks linked list elements
for (i=0; i<linkedList.length; i++) {
that = linkedList[i];
// if it isn't the same object (player)
if (that != this) {
// hitTesting
if (hitTest2(this, that)) {
this.say("I OVERLAPPED\n "+that.instance);
hit = true;
}
}
if (!hit) {
// if no overlapping at all
this.shutUp();
}
}
// place it into new coordinates
this.placeIt();
// zeroing direction
this.direction = 0;
};

In moveObject function, hit testing is being performed between main character and all objects on the linked list. If overlapping happens, a little balloon with the text pops up.

The next code shows the creation of objects using the functions above:

// --- initialization ----------------------------
// main character
object1 = new object("player", "MC1", 50, 50, 50, 50);
object1.init();
// other objects
object2 = new object("object1", "MC2", 300, 50, 25, 100);
object2.init();
object3 = new object("object2", "MC3", 250, 250, 75, 25);
object3.init();
object4 = new object("object3", "MC4", 250, 200, 50, 25);
object4.init();
object5 = new object("object4", "MC5", 50, 150, 50, 100);
object5.init();
object6 = new object("object5", "MC7", 150, 150, 50, 50);
object6.init();
Note

A data for populating an isometric space with objects can be contained in a database or in external XML file. More about isometric maps in later tutorials.

The main loop:

// --- main program loop ----------------------------
this.onEnterFrame = function() {
control(object1);
object1.moveObject();
};

This loop is performed once for a program cycle.
Since only a main character (player) can be moved in this example, a moveObject method is performed only on it.

Function control tests the cursor keys and supplies the moveObject function with the direction parameters:

function control(object) {
// controls given object
if (Key.isDown(Key.LEFT)) {
object.direction |= 2;
} else if (Key.isDown(Key.RIGHT)) {
object.direction |= 8;
} else if (Key.isDown(Key.UP)) {
object.direction |= 4;
} else if (Key.isDown(Key.DOWN)) {
object.direction |= 1;
}
}

The numbers 1, 2, 4 and 8 are chosen because of something that is needed in the next tutorial; at this stage it can be any numbers or letters.

You can control more than one object with this function. For example, the following code moves 2 objects at the same time:

this.onEnterFrame = function() {
control(object1);
control(object2);
object1.moveObject();
object2.moveObject();
};

Huh, that was it! Now, let's get back to 3d...

Onwards to the next page!


page 2 of 3


 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.