私は html5+canvas が初めてで、私の問題に関する詳細情報が見つからないため、ここで質問することにしました...
キャンバス 400x300px で 2800x1500px の大きな地図をスクロールし、キャンバスの外側にある「見えない領域」で衝突を検出するのに助けが必要です。
このような:
私のコードからのいくつかの関数
function Map()
{
this.img = new Image();
this.img.src = "img/map.jpg"; //map picture on main canvas
this.gimg = new Image();
//map with opacity on "ghost" canvas for collision detecting
this.gimg.src = "img/gmap.png";
this.draw = function(ctx,gctx)
{
ctx.drawImage(this.img,-offset.x,-offset.y);
gctx.drawImage(this.gimg,-offset.x,-offset.y);
}
}
function init()
{
var gameLoop = setInterval(function() {
draw(ctx,gctx);
}, 1000/fps);
}
function draw(ctx,gctx)
{
ctx.save();
gctx.save();
ctx.clearRect(-offset.x,-offset.y,2800,1500);
gctx.clearRect(-offset.x,-offset.y,2800,1500);
map.draw(ctx,gctx);
ctx.translate(offset.x,offset.y); //scrolling canvas
gctx.translate(offset.x,offset.y); //scrolling ghost canvas
ctx.restore();
gctx.restore();
}
//collision detecting function
function hitTest(obj,gctx)
{
var imageData = gctx.getImageData(obj.x,obj.y,1,1);
if( imageData.data[3]==255)
{
return true;
}
else return false;
}
マップをスクロールするには、その例を使用します:
http://jsfiddle.net/hKrrY/
私のプロジェクト:
http://game.com.hostinghood.com/