doulei6330 2012-10-30 07:35
浏览 40
已采纳

基于PHP Tile的地图 - >从左上角绘制

I am having trouble drawing a tile-based map :( the map however is only going to show what is VISIBLE to the player I have a canvas element on which i draw one layer. On the canvas, i use javascript to attach an image 32x32px. The image's position is based on: $tr $tc

as shown below:

for($tr = 0; $tr < count($mapArray)-1; $tr++) {
for($tc = 0; $tc < count($mapArray[$tr])-1; $tc++) {
    if($mapArray[$tr][$tc]!== null){
        echo "attachImage(" . $mapArray[$tr][$tc]. "," . $tc . "," . $tr . ",context);";
    }
}
}

I don't know how I would go about making the it draw tiles from 0,0. it always starts drawing tiles from the same location on the array (ex 3,2). Maybe i'm over thinking this or im too tired... i can't find out any way :(

here's the javascript part

function attachImage(tile, x, y, canvasContext)
{
  var base_image = new Image();
  base_image.onload = function(){
    canvasContext.drawImage(base_image, 32*(x-0),32*(y-0));
  }
  base_image.src = 'Images/tiles/'+(tile-1)+'.png';
}

to reiterate, i want it to start drawing from 0px,0px because i want to only display the tiles around the player.

  • 写回答

1条回答 默认 最新

  • dsnpjz6907 2012-10-30 08:03
    关注

    i dont know how to delete this question, but i found out the answer

    $c = 0;
    for($tr = $drawy; $tr < $drawy2; $tr++) {
        $r = 0;
        for($tc = $drawx; $tc < $drawx2; $tc++) {
            if($mapArray[$tr][$tc] !== null)
            {
                $tile = $mapArray[$tr][$tc];
                echo "attachImage(" . $tile . "," . $r . "," . $c . ",context);";
            }
            $r++;
        }
        $c++;
    }
    
    for($tr = 0; $tr < count($mapArrayy)-1; $tr++) {
        for($tc = 0; $tc < count($mapArrayy[$tr])-1; $tc++) {
            if($mapArrayy[$tr][$tc]!== null){
                echo "attachImage(" . $mapArrayy[$tr][$tc]. "," . $tc . "," . $tr . ",context2);";
            }
        }
    }
    

    i added $c and $r in the first loop. that's all. now it draws from 0px,0px.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!