A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [RESOLVED] F8[AS2]Finding coordinates in TBW problem

  1. #1
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    389

    resolved [RESOLVED] F8[AS2]Finding coordinates in TBW problem

    Hi. This is a snippet of code from a way to find object's edges in order to check to see what cell he's in. I have been reading TonyPa's tutorials but I don't get what goes on. Don't know a thing about prototypes which is the way he declares his tiles beeing walkable or not. I am sure there has to be a way to achieve the same purpose the way I expose but I have been thinking about it over and over to the point I've become stuck. This code works fine with my character's movement but only checks for two sides which I can't manage to figure out. I'd like to flip this movement to solve my problem, but like I mentioned, I'm stuck... soemone, please open the attached file to see what I'm trying to say. Thanks in advance...


    function moveCar(dir) {
    if (dir == "up") {
    var tempx:Number = ob.x+ob.radius;
    var tempy:Number = ob.y-ob.radius;
    var cellx:Number = Math.floor(tempx/game.spacing);
    var celly:Number = Math.floor(tempy/game.spacing);
    var tempCell = game["cell"+cellx+"_"+celly];
    if (tempCell.type != 1) {
    return;
    } else {
    ob.x += speedx;
    ob.clip._x = ob.x;
    ob.y -= speedy;
    ob.clip._y = ob.y;
    }
    } if(dir == "down") {
    var tempx:Number = ob.x-ob.radius;
    var tempy:Number = ob.y+ob.radius;
    var cellx:Number = Math.floor(tempx/game.spacing);
    var celly:Number = Math.floor(tempy/game.spacing);
    var tempCell = game["cell"+cellx+"_"+celly];
    if (tempCell.type != 1) {
    return;
    } else {
    ob.y -= speedy;
    ob.clip._y = ob.y;
    ob.x += speedx;
    ob.clip._x = ob.x;
    }
    }
    }
    Attaching Fla example.
    Last edited by capdetrons; 01-09-2012 at 11:43 AM. Reason: wrong post

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    If you have an object moving freely between tiles so it can have any coordinates, you need to check 2 points whenever object attempts to move.

    Lets mark 4 corner points of the object UL, UR, DL, DR (up-left corner, up-right corner down-left corner, down-right corner).

    Now lets assume object wants to move to the right. Since it can cover 2 different tiles vertically, we need to check both corners on its RIGHT side - UR and DR. If both corners would be on walkable tile after movement, object may move in that direction.

    In case object wants to move down, we need to check both DR and DL corners.

    Notice that you can always check all 4 corners for the sake of writing simpler code, just in any movement direction only 2 corners will matter, finding other 2 does not affect the result.

    You need to check corners in advance, it means not checking current coordinates but coordinates where object would be after it moves.

    Lets say current coordinates of object center are ob.x and ob.y, movement values are speedx and speedy, the new center would be at

    newx = ob.x + speedx;
    newy = ob.y + speedy;

    Assuming object is centered, find the limits:

    minY = newy - obHeight/2;
    maxY = newy + obHeight/2;
    minX = newx - obWidth/2;
    maxX = newx + obWidth/2;

    Now set the corners:

    UL.x = minX;
    UL.y = minY;
    UR.x = maxX;
    UR.y = minY;
    DL.x = minX;
    DL.y = maxY;
    DR.x = maxX;
    DR.y = maxY;

    Once you have coordinates of corner points, check if they would end in wall-type tile. If any of the points goes into wall, movement is not allowed.

  3. #3
    Senior Member
    Join Date
    Jan 2007
    Location
    Barcelona
    Posts
    389

    Thanks

    Thanks, Tony. Didn't want to ask you straight away. I did follow your tutorials, but was really confused because of the prototypes... I've got it partially working now I have checked for all 4 edges of my character and I'll just have to figure out a way to let it start without having to change any rotation with left or right cursors...

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