doulei6330 2012-10-30 07:35 采纳率: 0%
浏览 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.

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

报告相同问题?

悬赏问题

  • ¥15 C++ 菜单窗口独立出来,可以随意移动放大缩小。
  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 网友们我该怎么办啊,急
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 目标跟踪,计算机视觉
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?