Managing isometric depths in Flash
You'll never believe it.
When I was about to close and delete the blog, I got an anonymous donation from a reader to pay the fee and keep the blog up and running.
So, here I am with a big problem... managing isometric depths in Flash... this is intended to be a stand alone tutorial but it's also the 2nd part of Make a Flash game like BoxHead tutorial.
Let me show you the problem. We have a lot of moving objects in an isometric land. You can think about the zombies in BoxHead. We have to manage depths in order to make the closer zombies hide the ones behind them.
I solved the problem in two ways... one is very very stupid and it works only with AS2... but it works... while the other is a little more complex but can be ported in AS3.
Let me show you this actionscript:
-
var blue_array = new Array();
-
number_of_blocks = 10;
-
for (x=1; x<=number_of_blocks; x++) {
-
blue_array.push("blue_"+x);
-
blue = _root.attachMovie("blue", "blue_"+x, x, {_x:Math.random()*500, _y:Math.random()*350});
-
blue.xspeed = 1+Math.random();
-
blue.yspeed = 1+Math.random();
-
blue.onEnterFrame = function() {
-
this._x += this.xspeed;
-
this._y -= this.yspeed;
-
if (this._x>500) {
-
this._x -= 500;
-
}
-
if (this._y<0) {
-
this._y += 350;
-
}
-
this.idiot_depth = Math.floor(this._y*500+this._x);
-
this.swapDepths(this.idiot_depth);
-
};
-
}
Lines 1-16 are the same as the ones I used to populate the screen in Managing multiple collision detection with Flash tutorial, and they just place 10 "zombies" on the stage and make them move at a random direction and speed.
At line 17 I determine the depth in an idiot way, just saying "the closer the zombie to the bottom left of the screen, the highest its depth", then at line 18 I assign this depth to the zombie.
Why am I calling it an idiot way? It works!
Even if it works, and you proudly use it in your games, I'll tell you why you won't use it forever.
The code above leaves a lot of holes between depths... you can have a zombie with a depth of 1345 and the next one with a depth of 54643. In AS3 you cannot have depths, and let me tell you this code sucks!
Let's make things more difficult:
-
var blue_array = new Array();
-
number_of_blocks = 10;
-
for (x=1; x<=number_of_blocks; x++) {
-
blue_array.push("blue_"+x);
-
blue = _root.attachMovie("blue", "blue_"+x, x, {_x:Math.random()*500, _y:Math.random()*350});
-
blue.xspeed = 1+Math.random();
-
blue.yspeed = 1+Math.random();
-
blue.dp = x;
-
blue.onEnterFrame = function() {
-
this._x += this.xspeed;
-
this._y -= this.yspeed;
-
if (this._x>500) {
-
this._x -= 500;
-
}
-
if (this._y<0) {
-
this._y += 350;
-
}
-
this.txt.text = this.getDepth();
-
};
-
}
-
_root.onEnterFrame = function() {
-
for (i=1; i<=number_of_blocks; i++) {
-
for (x=0; x<number_of_blocks-1; x++) {
-
_root[blue_array[x]].idiot_depth = Math.floor(_root[blue_array[x]]._y*500+_root[blue_array[x]]._x);
-
_root[blue_array[x+1]].idiot_depth = Math.floor(_root[blue_array[x+1]]._y*500+_root[blue_array[x+1]]._x);
-
if ((_root[blue_array[x]].idiot_depth>_root[blue_array[x+1]].idiot_depth) and (_root[blue_array[x]].getDepth()<_root[blue_array[x+1]].getDepth())) {
-
_root[blue_array[x]].swapDepths(_root[blue_array[x+1]]);
-
app = _root[blue_array[x]];
-
_root[blue_array[x]] = _root[blue_array[x+1]];
-
_root[blue_array[x+1]] = app;
-
}
-
if ((_root[blue_array[x]].idiot_depth<_root[blue_array[x+1]].idiot_depth) and (_root[blue_array[x]].getDepth()>_root[blue_array[x+1]].getDepth())) {
-
_root[blue_array[x]].swapDepths(_root[blue_array[x+1]]);
-
app = _root[blue_array[x]];
-
_root[blue_array[x]] = _root[blue_array[x+1]];
-
_root[blue_array[x+1]] = app;
-
}
-
}
-
}
-
};
Line 21: Beginning of the function to be executed at every frame
Line 22: Cycle that will be repeated as many times as the number of the zombies on stage
Line 23: Cycle scanning from zero to the number of zombies minus one
Lines 24-25: Calculating the idiot depth of the x-th and the (x+1)-th zombies
Line 26: If the idiot depth of the x-th zombie is higher than the one of the (x+1)-th zombie but the real depth of the x-th zombie is lower than the one of the (x+1)-th zombie...
Line 27: Swapping the x-th and (x+1)-th depths
Lines 28-30: Swapping the x-th and (x+1)-th elements in the array
Lines 32-37: Do the same ff the idiot depth of the x-th zombie is lower than the one of the (x+1)-th zombie but the real depth of the x-th zombie is higher than the one of the (x+1)-th zombie.
Conditions at lines 26 and 32 could be placed in a or but I wrote them this way for a visual purpose.
And that's it... now all zombies have their depth between one and the number of zombies on the stage, without depth holes.
This way to solve the problem is slower, but you can improve it as long as you find a better sorting algorithm
Next time I will focus on collisions in an isometric world, meanwhile download the source codes and give me feedback.
And thank my anonymous reader who saved the blog.
They can be easily customized to meet the unique requirements of your project.
30 Responses to “Managing isometric depths in Flash”
Leave a Reply
Posts
- Rick Triqui: my first PlayCrafter game
- Prototype of a Flash game like Meeblings
- Games for the game developers!
- The art of debugging
- How to embed a text file in Flash
- Create a Flash game in minutes with PlayCrafter
- Upgrade your Flash CS4 to 10.0.2
- Play Mazeroll, my latest Box2D game
- Triqui MochiAds Arcade plugin for WordPress Released!!
- The MochiAds funnel
- Flash game creation tutorial - part 1
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Create a flash draw game like Line Rider or others - part 1
- Create a Flash Racing Game Tutorial
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash artillery game - step 1
- Create a flash draw game like Line Rider or others - part 5
- Flash game creation tutorial – part 5.2




(4.9 out of 5) - Flash game creation tutorial – part 3




(4.86 out of 5) - Creation of a platform game with Flash – step 2




(4.84 out of 5) - Create a survival horror game in Flash tutorial – part 1




(4.82 out of 5) - Create a flash artillery game – step 1




(4.82 out of 5) - Create a Flash Racing Game Tutorial




(4.8 out of 5) - Create a flash artillery game – step 2




(4.75 out of 5) - New tile based platform engine – part 6 – ladders




(4.74 out of 5) - Flash game creation tutorial – part 2




(4.73 out of 5) - The experiment – one year later




(4.7 out of 5)



nice tut
youll never believe it… right i don’t lol
first comment
Brilliant, brilliant news!
I am not done reading your actual tutorial in your post, but I’d like to congratulate you and hope EF keeps on running for a long long time, thank gooodness for that donator!
wow… i guess it wasn’t an april fools joke, sorry for ever doubting you… ;)
So you got like million bucks from some random dude, i doubt that o_O???
well nice tut anyways.
You had me fooled till I got to work, I guess I am happy it’s a joke
I was the random dude, but it wasn’t a donation, it was a loan… You’d better get 3 billion before 1st April, 2009 or I’ll have to close your blog.
Lol RJ now that did make me laugh !
So glad it was a joke though. You had me well and truly fooled ! Lol jk.
In all seriousness never close the blog and keep gold like this coming !
Hi Emanuele,
The subject of 1st April is still disturbing us.No comments about this tutorial only about that.
I was awfully frightened.
KISSES KISSES KISSES!!!
hi emanuele :)
Hey! You’re right!
I will never believe it.
Despite, this is a good tut. I forgot to mention. I never thought of doing it the second way.
Wow. It was true. Didn’t the guy even ask for credit? Nice guy, thanks! :D
fock you stupid joke i was emo!good tut thought
Awesome!
Isometric-ville, here I come!
Also: Did the generous donator just buy you billions of beers? :D
If you liked this post buy me a beer (or a billion)
still confused about whether the closing down thing was a joke or not but anyway I’m a regular follower of your tutes and you’ve helped me learn how to make games for my site, flashbynight.com (no, I didn’t clone tileball!)
HAHA!
Thats so funny.
If you liked this post buy me a beer (or a billion)
Ok… I’ll say… I did the donation… I love your blog…
PS: I own Microsoft (I bought it in 1st April)
damn you had me scared there for a while :P,
but luckily you are still here, now i can start learning some as3 from you :)
sweet i really love this blog! im glad its staying open… though i havnt totaly mastered that much actionscript ima try to make a game like boxhead just totaly diff storyline and gampley b/c i love the 8-way system and useit alot! alsoim going to try to learn this tri-thingymajiggy stuff cuz it sounds like where flash will be heading in the next year so yay! lol ok im so glad about that donator !!!
donator u be awsome!
not tri i meant isometric stuff…sorry
emanuele es muy awsome
YEAH THIS WILL HELP ME FINISH MY GAME. NICE TUTORIAL. IM GOING TO TURN CAPS LOCK OFF NOW.
Emanuele. This might help you on your boxhead tutorial:
http://eagleproductions.wordpress.com/2008/03/27/how-to-make-a-game-like-boxhead/
It helps on the 3D movement.
Cheers!
I would simply use
swapDepths(this._y += (this._x/100))
wow… you had me fooled for a second!
nice joke!!!
(at least i hope its a joke!)
If you liked this post buy me a beer (or a billion)
Made my day.
YEAH YEAH YEAH!!!!!!!! GO EMAUNLE (iknowispelleditwrong) hoorah i saw your leaveing and coming back in one sitting!
yea emaunle!
Sorry about posting this here, as I don’t have your email address. But I just saw and played the game ‘Ball Breaker’ and thought that the engine behind it (if that’s what you’ll call it) is actually very useful, and could be applied in many ways. My first thought was snooker/pool games but I’m sure it could be applied anywhere really.
Here’s the link: http://www.triqui.com/play.php?id=244
So yeah, I wondered what you think and if you could possibly run a tutorial on it sometime soon haha =]
Cheers for everything so far, been a great help!
Sam, I made a game like that called ball escaper.
Youre engine is probably better though
nice tutorial.
short but it shows more then normal posts now i can actualy finish my game properly
You could help the z resolve me-depth in my isometric game? They are years that I am seeking the siluzione and I see that you found it, perhaps I can carry it in my editor.
Thanks for all of the help that you can give me.