PDA

View Full Version : Tile-Based Movement


icio
01-12-2006, 10:26 PM
Updated

There is still that pesky little bug that occurs when you move towards a tile diagonally, but I'm working on it.

1.a - 23 lines
Arrow keys to control character around the tile-based world.
var tileSize:Number = 30;
var map:Array = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 1, 1, 1, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 0, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]];
for (y = 0; y < map.length; y++) {
for (x = 0; x < map.length; x++) {
var tile:MovieClip = _root.createEmptyMovieClip("tile_"+x+"_"+y, _root.getNextHighestDepth());
drawTile(tile, map[y][x]?0:0xF2F2F2);
applyProps(tile, {_x:x*tileSize, _y:y*tileSize}); } }
var player:MovieClip = _root.createEmptyMovieClip("player_mc", _root.getNextHighestDepth());
drawTile(player, 0x99FF33);
applyProps(player, {_x:tileSize, _y:tileSize, xspd:0, yspd: 0});
function onEnterFrame() {
player.xspd += Key.isDown(Key.RIGHT) ? Math.min(tileSize - Math.abs(player.xspd), 1) : (Key.isDown(Key.LEFT) ? Math.max(Math.abs(player.xspd) - tileSize, -1) : (Math.abs(player.xspd) < 0.1 ? -player.xspd : -0.2 * player.xspd));
player.yspd += Key.isDown(Key.DOWN) ? Math.min(tileSize - Math.abs(player.yspd), 1) : (Key.isDown(Key.UP) ? Math.max(Math.abs(player.yspd) - tileSize, -1) : (Math.abs(player.yspd) < 0.1 ? -player.yspd : -0.2 * player.yspd));
var t:Array = [Math.floor(player._x / tileSize), Math.floor((player._x + tileSize - 1) / tileSize), Math.floor(player._y / tileSize), Math.floor((player._y + tileSize - 1) / tileSize)];
var c:Array = [(map[t[2]][t[0] - 1] == 1 || map[t[3]][t[0] - 1] == 1) == false ? Number.NaN : t[0] * tileSize, (map[t[2]][t[1] + 1] == 1 || map[t[3]][t[1] + 1] == 1) == false ? Number.NaN : t[1] * tileSize, (map[t[2] - 1][t[0]] == 1 || map[t[2] - 1][t[1]] == 1) == false ? Number.NaN : t[2] * tileSize, (map[t[3] + 1][t[0]] == 1 || map[t[3] + 1][t[1]] == 1) == false ? Number.NaN : t[3] * tileSize];
player.xspd = player._x + player.xspd < c[0] ? c[0] - player._x : (player._x + player.xspd > c[1] ? c[1] - player._x : player.xspd);
player.yspd = player._y + player.yspd < c[2] ? c[2] - player._y : (player._y + player.yspd > c[3] ? c[3] - player._y : player.yspd);
player._x += player.xspd;
player._y += player.yspd; }
function applyProps(targ, vars) {
for (i in vars) {
targ[i] = vars[i]; } }
function drawTile(tile, color) {
var funcs:Array = [["lineStyle", [0, 0, 0]], ["beginFill", [color, 100]], ["lineTo", [tileSize, 0]], ["lineTo", [tileSize, tileSize]], ["lineTo", [0, tileSize]], ["lineTo", [0, 0]], ["endFill", []]];
for (var i=0; i<funcs.length; i++) {
tile[funcs[i][0]].apply(tile, funcs[i][1]); } }

1.b - 23 Lines
Left and Right arrow keys to move from side to side. Up to jump.
var tileSize:Number = 30;
var map:Array = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 1, 1, 1, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 0, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]];
for (y = 0; y < map.length; y++) {
for (x = 0; x < map.length; x++) {
var tile:MovieClip = _root.createEmptyMovieClip("tile_"+x+"_"+y, _root.getNextHighestDepth());
drawTile(tile, map[y][x]?0:0xF2F2F2);
applyProps(tile, {_x:x*tileSize, _y:y*tileSize}); } }
var player:MovieClip = _root.createEmptyMovieClip("player_mc", _root.getNextHighestDepth());
drawTile(player, 0x99FF33);
applyProps(player, {_x:tileSize, _y:tileSize, xspd:0, yspd: 0});
function onEnterFrame() {
var t:Array = [Math.floor(player._x / tileSize), Math.floor((player._x + tileSize - 1) / tileSize), Math.floor(player._y / tileSize), Math.floor((player._y + tileSize - 1) / tileSize)];
var c:Array = [(map[t[2]][t[0] - 1] == 1 || map[t[3]][t[0] - 1] == 1) == false ? Number.NaN : t[0] * tileSize, (map[t[2]][t[1] + 1] == 1 || map[t[3]][t[1] + 1] == 1) == false ? Number.NaN : t[1] * tileSize, (map[t[2] - 1][t[0]] == 1 || map[t[2] - 1][t[1]] == 1) == false ? Number.NaN : t[2] * tileSize, (map[t[3] + 1][t[0]] == 1 || map[t[3] + 1][t[1]] == 1) == false ? Number.NaN : t[3] * tileSize];
player.xspd += Key.isDown(Key.RIGHT) ? Math.min(tileSize - Math.abs(player.xspd), 1) : (Key.isDown(Key.LEFT) ? Math.max(Math.abs(player.xspd) - tileSize, -1) : (Math.abs(player.xspd) < 0.1 ? -player.xspd : -0.2 * player.xspd));
player.yspd += Key.isDown(Key.UP) && c[3]==player._y && player.yspd == 0? -15 : Math.min(tileSize-player.yspd, 1);
player.xspd = player._x + player.xspd < c[0] ? c[0] - player._x : (player._x + player.xspd > c[1] ? c[1] - player._x : player.xspd);
player.yspd = player._y + player.yspd < c[2] ? c[2] - player._y : (player._y + player.yspd > c[3] ? c[3] - player._y : player.yspd);
player._x += player.xspd;
player._y += player.yspd; }
function applyProps(targ, vars) {
for (i in vars) {
targ[i] = vars[i]; } }
function drawTile(tile, color) {
var funcs:Array = [["lineStyle", [0, 0, 0]], ["beginFill", [color, 100]], ["lineTo", [tileSize, 0]], ["lineTo", [tileSize, tileSize]], ["lineTo", [0, tileSize]], ["lineTo", [0, 0]], ["endFill", []]];
for (var i=0; i<funcs.length; i++) {
tile[funcs[i][0]].apply(tile, funcs[i][1]); } }

See attached SWF files for previews.

freeskier89
01-12-2006, 10:55 PM
That looks really nice :D Amazing for 25 lines. // Following counts as 0 lines of code because Drawing API Functions are not counted, or so I am lead to believe.
Are you sure. I don't know, but maybe kirupa or one of the mods should make a long list of the rules like : for loops count, drawing api counts, do put all of your variables in arrays to make them 1 line... There is so much questioning, it might be good to get it all cleared up.

kirupa
01-12-2006, 11:14 PM
The only things that don't count are the default function/method declarations and closing brackets for those functions and methods. Anything else is counted. For example, any line of code that is ended by a semicolon counts as one line.

icio - your section of code following the comments count as 7 lines :(

:hoser:

kookaburra
01-13-2006, 03:04 AM
Nice work!

virusescu
01-13-2006, 04:26 AM
Nice work
I thought "interlacing" more than one ternary ? : statement per line is not allowed

Seb Hughes
01-13-2006, 05:37 AM
Very nice but too many lines of code :(

icio
01-13-2006, 06:54 AM
OK, so I'll need to do something about those lineTo's. Is that the only problem, kirupa?

Thanks for all the comments - but I guess I'll need to re-jig it a bit when I get home.

:thumb:

icio
01-13-2006, 01:13 PM
25 Lines.

var tileSize:Number = 30;
var map:Array = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 1, 1, 1, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 0, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]];
for (y = 0; y < map.length; y++) {
for (x = 0; x < map.length; x++) {
var tile:MovieClip = _root.createEmptyMovieClip("tile_"+x+"_"+y, _root.getNextHighestDepth());
drawTile(tile, map[y][x]?0:0xF2F2F2);
applyProps(tile, {_x:x*tileSize, _y:y*tileSize}); } }
var player:MovieClip = _root.createEmptyMovieClip("player_mc", _root.getNextHighestDepth());
drawTile(player, 0x99FF33);
applyProps(player, {_x:tileSize, _y:tileSize, xspd:0, yspd: 0});
function onEnterFrame() {
player.xspd += Key.isDown(Key.RIGHT) ? Math.min(tileSize - Math.abs(player.xspd), 1) : (Key.isDown(Key.LEFT) ? Math.max(Math.abs(player.xspd) - tileSize, -1) : (Math.abs(player.xspd) < 0.1 ? -player.xspd : -0.2 * player.xspd));
player.yspd += Key.isDown(Key.DOWN) ? Math.min(tileSize - Math.abs(player.yspd), 1) : (Key.isDown(Key.UP) ? Math.max(Math.abs(player.yspd) - tileSize, -1) : (Math.abs(player.yspd) < 0.1 ? -player.yspd : -0.2 * player.yspd));
var t:Array = [Math.floor(player._x / tileSize), Math.floor((player._x + tileSize - 1) / tileSize), Math.floor(player._y / tileSize), Math.floor((player._y + tileSize - 1) / tileSize)];
var c:Array = [(map[t[2]][t[0] - 1] == 1 || map[t[3]][t[0] - 1] == 1) == false ? Number.NaN : t[0] * tileSize, (map[t[2]][t[1] + 1] == 1 || map[t[3]][t[1] + 1] == 1) == false ? Number.NaN : t[1] * tileSize, (map[t[2] - 1][t[0]] == 1 || map[t[2] - 1][t[1]] == 1) == false ? Number.NaN : t[2] * tileSize, (map[t[3] + 1][t[0]] == 1 || map[t[3] + 1][t[1]] == 1) == false ? Number.NaN : t[3] * tileSize];
var xmove:Number = player._x + player.xspd < c[0] ? c[0] - player._x : (player._x + player.xspd > c[1] ? c[1] - player._x : player.xspd);
var ymove:Number = player._y + player.yspd < c[2] ? c[2] - player._y : (player._y + player.yspd > c[3] ? c[3] - player._y : player.yspd);
player.xspd *= Number(xmove == player.xspd);
player.yspd *= Number(ymove == player.yspd);
player._x += xmove;
player._y += ymove; }
function applyProps(targ, vars) {
for (i in vars) {
targ[i] = vars[i]; } }
function drawTile(tile, color) {
var funcs:Array = [["lineStyle", [0, 0, 0]], ["beginFill", [color, 100]], ["lineTo", [tileSize, 0]], ["lineTo", [tileSize, tileSize]], ["lineTo", [0, tileSize]], ["lineTo", [0, 0]], ["endFill", []]];
for (var i=0; i<funcs.length; i++) {
tile[funcs[i][0]].apply(tile, funcs[i][1]); } }

kirupa
01-13-2006, 01:14 PM
Nice - adding it to the list! :)

JoshuaJonah
01-13-2006, 03:08 PM
oops

icio
01-13-2006, 03:38 PM
Yes I am all to aware of that, thanks.

It's a common tile-based bug which can be fixed easily, but not in 25 lines of code. Not yet anyway, I might try and work something up.

DDD
01-13-2006, 04:14 PM
lol..I was able to do what defective did.....was about to post it buthe beat me to it. Nice work tho

kdd
01-13-2006, 04:16 PM
actually i beat everyone, i pm-ed him yesterday. :D
i didn't want to post it here, because i didn't want to hurt his feelings...

JoshuaJonah
01-13-2006, 04:25 PM
:) sorry..... I have a black shriveled heart.

icio
01-13-2006, 04:44 PM
The first post has been updated with up-to-date code for 1.a and a new sample: 1.b

It's ok, I'm well aware of the bug. The most annoying thing is that I can't apply the fix for it and remain within the 25 line limit. Rest assured, though, that I will keep trying.

McGiver
01-15-2006, 06:39 AM
yay I made it to the top in the juming thing! and then fell down :(

icio
01-15-2006, 09:01 PM
It's easy to get to the top :P

ElectricGrandpa
01-15-2006, 09:35 PM
function applyProps(targ, vars) {
for (i in vars) {
targ[i] = vars[i]; } }

Handy :)

icio
01-15-2006, 09:42 PM
;P

Pattt
01-16-2006, 05:28 PM
It's hard for me to do this in 50 lines, and you did it in 25, woah... :S

icio
01-16-2006, 08:51 PM
23* ;)