A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: [F5][A2] tile-based engine character movement

  1. #1
    Member
    Join Date
    Aug 2008
    Posts
    75

    [F5][A2] tile-based engine character movement

    Im using the tile-based tutorials created by tonypa. I haven't been able to figure out how to have the char face left when jumping left/falling left.


    here is the code for detect keys:

    function detectKeys() {
    moveTiles();
    var ob = _root.char;
    var keyPressed = false;
    if (Key.isDown(Key.SPACE) and !ob.jump and !ob.climb) {
    if (!ob.jump) {
    //if we were on slope, update
    if (ob.onSlope) {
    ob.y -= ob.addy;
    ob.ytile = Math.floor(ob.y/game.tileH);
    }
    //jump
    ob.onMovingTile = false;
    ob.jump = true;
    ob.jumpspeed = ob.jumpstart;
    }
    }
    if (Key.isDown(Key.RIGHT)) {
    getMyCorners(ob.x-ob.speed, ob.y, ob);
    if (!ob.climb or ob.downleft and ob.upleft and ob.upright and ob.downright) {
    if (!checkMovingTiles(1)) {
    char.onMovingTile = false;
    }
    keyPressed = _root.moveChar(ob, 1, 0);
    }
    } else if (Key.isDown(Key.LEFT)) {
    getMyCorners(ob.x-ob.speed, ob.y, ob);
    if (!ob.climb or ob.downleft and ob.upleft and ob.upright and ob.downright) {
    if (!checkMovingTiles(1)) {
    char.onMovingTile = false;
    }
    keyPressed = _root.moveChar(ob, -1, 0);
    }
    } else if (Key.isDown(Key.UP)) {
    if (!ob.jump and checkUpLadder(ob)) {
    keyPressed = _root.climb(ob, -1);
    }
    } else if (Key.isDown(Key.DOWN)) {
    if (!ob.jump and checkDownLadder(ob)) {
    keyPressed = _root.climb(ob, 1);
    }
    }
    if (Key.isDown(Key.SHIFT) and getTimer()>ob.lastshot+ob.shootspeed) {
    _root.shoot(ob);

    //shoot sound
    var shootSound = new Sound();
    shootSound.attachSound('Yogi-snowflake.mp3');
    shootSound.start();
    }
    //do we jump
    if (ob.jump) {
    keyPressed = _root.jump(ob);
    }
    //walk animation
    if (!keyPressed) {
    ob.clip.char.gotoAndStop(1);
    } else {
    ob.clip.char.play();
    }

    //i added this code not sure if it works right
    if(Key.isDown(key.LEFT) and ob.jump){
    keyPressed = _root.moveChar(ob, -1, 0);


    function moveChar(ob, dirx, diry, jump) {
    //save direction for shooting
    if (dirx<>0) {
    ob.dirx = dirx;
    }
    //are we jumping?
    if (Math.abs(jump) == 1) {
    var speed = ob.jumpspeed*jump;
    } else {
    var 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 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 and !checkMovingTiles(speed*diry)) {
    ob.y += speed*diry;
    } else {
    ob.jump = false;
    if (ob.onMovingTile) {
    ob.y = ob.onMovingTile.y-ob.onMovingTile.height-ob.height;
    } else {
    ob.y = (ob.ytile+1)*game.tileH-ob.height;
    }
    }
    }
    //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) or ob.onSlope) {
    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) or ob.onSlope) {
    ob.x += speed*dirx;
    //check for tile below
    fall(ob);
    } else {
    ob.x = (ob.xtile+1)*game.tileW-ob.width;
    }
    }

    //update char position
    ob.xtile = Math.floor(ob.x/game.tileW);
    ob.ytile = Math.floor(ob.y/game.tileH);
    checkForSlopes(ob, diry, dirx);
    updateChar(ob, dirx, diry);
    return (true);
    }

    function updateChar(ob, dirx, diry) {
    //update char position
    ob.clip._x = ob.x;
    ob.clip._y = ob.y;
    checkForSlopes(ob, diry, dirx);
    if (dirx or diry) {
    //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);
    }


    //check for item on current tile
    var itemname = game["item"+ob.ytile+"_"+ob.xtile];
    if (itemname and ob == _root.char) {
    //add points
    game.points = game.points+itemname.points;
    _root.points = game.points;
    //item pickup sound
    var itemSound = new Sound();
    itemSound.attachSound('Yogi-powerup.mp3');
    itemSound.start();

    //remove item movie clip
    removeMovieClip(itemname.clip);
    //remove item from items array
    game.items[itemname.position] = 0;
    //remove item object
    delete game["item"+ob.ytile+"_"+ob.xtile];
    }

    return (true);
    }

  2. #2
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    I believe it's under his first few movement tutorials. If I remember correctly, I think it uses dirx * char._xscale (which would make it 100 or -100).
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center