PDA

View Full Version : Direction while Jumping (tile based game)



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!

DangerousDan
February 15th, 2007, 09:31 PM
What about saving his direction in a seperate variable. like jumpdir, when jumping use that direction.

rhine
February 16th, 2007, 12:34 AM
What about saving his direction in a seperate variable. like jumpdir, when jumping use that direction.

Good call, I'll try that. Maybe every possible keystroke combination will have a different frame. Forgot about that :) Cheers

Nich
February 16th, 2007, 08:08 AM
I would base your direction solely on the dirx variable. Unless you have a specific image you want to show while jumping, the direction you're facing should only matter on the x axis. I thinks that's what causes the change of image when you jump: suddenly, diry becomes negative, making your positive dirx negative, hence you face in the negative x direction.

rhine
February 16th, 2007, 04:52 PM
I would base your direction solely on the dirx variable. Unless you have a specific image you want to show while jumping, the direction you're facing should only matter on the x axis. I thinks that's what causes the change of image when you jump: suddenly, diry becomes negative, making your positive dirx negative, hence you face in the negative x direction.

Cheers - this makes sense to me. I messed around and it's still not making any sense why the sprites are still not functioning correctly.

I altered the following formula so every possible direction has a different frame within the ob movie clip:

ActionScript Code:

ob.clip.gotoAndStop(dirx+diry*3+5);





Here are my calculations:

Right-facing = 1 + (0*3) + 5 = frame 6
Left-facing = -1 + (0*3) + 5 = frame 4

Right-jumping = 1 + (-1*3) + 5 = frame 3
Right-falling = 1 + (1*3) + 5 = frame 9

Left-jumping = -1 + (-1*3) + 5 = frame 1
Left-Falling = -1 + (1*3) + 5 = frame 7

I checked that all the frames match the appropriate sprite states, yet the falling and jumping states are always facing left! HALP!

FLA & SWF: http://www.rhine.ca/flash/

Nich
February 17th, 2007, 10:01 AM
Why not a simple if statement:

if(dirx < 0)
{
//goto the picture facing left
}
if(dirx > 0)
{
//goto the picture facing right
}



It's just easier that way, no?

rhine
February 18th, 2007, 05:03 PM
Why not a simple if statement:

if(dirx < 0)
{
//goto the picture facing left
}
if(dirx > 0)
{
//goto the picture facing right
}



It's just easier that way, no?

You are a god. Thanks - sometimes it's that simple.