rhine
February 15th, 2007, 07:22 PM
Hey Everyone,
This seems like a simple question, but I can't figure it out. I've been playing with Tonypa's tile-based game system (http://www.tonypa.pri.ee/tbw/start.html) and can't figure out how to make the character sprite face the direction he jumps or falls in face the right direction.
Here's the game and source files (http://www.rhine.ca/flash/).
And here's the source:
ActionScript Code:
function moveChar(ob, dirx, diry, jump) {
// are we jumping?
if (Math.abs(jump) == 1) {
speed = ob.jumpspeed*jump;
} else {
speed = ob.speed;
}
// vertical movement
// where are our edges?
// first we look for y movement, so x is old
getMyCorners(ob.x, ob.y+speed*diry, ob);
// move got dammit... and check for collisions.
// going up
if (diry == -1) {
if (ob.upleft and ob.upright) {
// no wall in the way, move on
ob.y += speed*diry;
} else {
// hit the wall, place char near the wall
ob.y = ob.ytile*game.tileH+ob.height;
// are we jumping around and have hit the tile above?
ob.jumpspeed = 0;
}
}
// if going down
if (diry == 1) {
if (ob.downleft and ob.downright) {
ob.y += speed*diry;
} else {
ob.y = (ob.ytile+1)*game.tileH-ob.height;
ob.jump = false;
}
}
// horisontal movement
// changing x with speed and taking old y
getMyCorners(ob.x+speed*dirx, ob.y, ob);
// if going left
if (dirx == -1) {
if (ob.downleft and ob.upleft) {
ob.x += speed*dirx;
// check for tile below
fall(ob);
} else {
ob.x = ob.xtile*game.tileW+ob.width;
}
}
// if going right
if (dirx == 1) {
if (ob.upright and ob.downright) {
ob.x += speed*dirx;
// check for tile below
fall(ob);
} else {
ob.x = (ob.xtile+1)*game.tileW-ob.width;
}
}
// update char position
ob.clip._x = ob.x;
ob.clip._y = ob.y;
// face the direction
ob.clip.gotoAndStop(dirx+diry*2+3);
// calculate the tile where chars center is
ob.xtile = Math.floor(ob.clip._x/game.tileW);
ob.ytile = Math.floor(ob.clip._y/game.tileH);
// check for door
if (game["t_"+ob.ytile+"_"+ob.xtile].door and ob == _root.char) {
// make new map
changeMap(ob);
}
return (true);
}
function jump(ob) {
// gravity control
ob.jumpspeed = ob.jumpspeed+ob.gravity;
if (ob.jumpspeed>game.tileH-char.height) {
ob.jumpspeed = game.tileH-char.height;
}
if (ob.jumpspeed<0) {
// going up
moveChar(ob, 0, -1, -1);
} else if (ob.jumpspeed>0) {
// going down
moveChar(ob, 0, 1, 1);
}
return (true);
}
Thanks guys!
This seems like a simple question, but I can't figure it out. I've been playing with Tonypa's tile-based game system (http://www.tonypa.pri.ee/tbw/start.html) and can't figure out how to make the character sprite face the direction he jumps or falls in face the right direction.
Here's the game and source files (http://www.rhine.ca/flash/).
And here's the source:
ActionScript Code:
function moveChar(ob, dirx, diry, jump) {
// are we jumping?
if (Math.abs(jump) == 1) {
speed = ob.jumpspeed*jump;
} else {
speed = ob.speed;
}
// vertical movement
// where are our edges?
// first we look for y movement, so x is old
getMyCorners(ob.x, ob.y+speed*diry, ob);
// move got dammit... and check for collisions.
// going up
if (diry == -1) {
if (ob.upleft and ob.upright) {
// no wall in the way, move on
ob.y += speed*diry;
} else {
// hit the wall, place char near the wall
ob.y = ob.ytile*game.tileH+ob.height;
// are we jumping around and have hit the tile above?
ob.jumpspeed = 0;
}
}
// if going down
if (diry == 1) {
if (ob.downleft and ob.downright) {
ob.y += speed*diry;
} else {
ob.y = (ob.ytile+1)*game.tileH-ob.height;
ob.jump = false;
}
}
// horisontal movement
// changing x with speed and taking old y
getMyCorners(ob.x+speed*dirx, ob.y, ob);
// if going left
if (dirx == -1) {
if (ob.downleft and ob.upleft) {
ob.x += speed*dirx;
// check for tile below
fall(ob);
} else {
ob.x = ob.xtile*game.tileW+ob.width;
}
}
// if going right
if (dirx == 1) {
if (ob.upright and ob.downright) {
ob.x += speed*dirx;
// check for tile below
fall(ob);
} else {
ob.x = (ob.xtile+1)*game.tileW-ob.width;
}
}
// update char position
ob.clip._x = ob.x;
ob.clip._y = ob.y;
// face the direction
ob.clip.gotoAndStop(dirx+diry*2+3);
// calculate the tile where chars center is
ob.xtile = Math.floor(ob.clip._x/game.tileW);
ob.ytile = Math.floor(ob.clip._y/game.tileH);
// check for door
if (game["t_"+ob.ytile+"_"+ob.xtile].door and ob == _root.char) {
// make new map
changeMap(ob);
}
return (true);
}
function jump(ob) {
// gravity control
ob.jumpspeed = ob.jumpspeed+ob.gravity;
if (ob.jumpspeed>game.tileH-char.height) {
ob.jumpspeed = game.tileH-char.height;
}
if (ob.jumpspeed<0) {
// going up
moveChar(ob, 0, -1, -1);
} else if (ob.jumpspeed>0) {
// going down
moveChar(ob, 0, 1, 1);
}
return (true);
}
Thanks guys!