我的屏幕每25毫秒重绘一次,图像闪烁,这是我的代码
var FRAME_RATE = 40;
var intervalTime = 1000/FRAME_RATE;
gameLoop();
function gameLoop(){
context.clearRect(0, 0, 640, 640);
renderMap();
window.setTimeout(gameLoop, intervalTime);
}
Run Code Online (Sandbox Code Playgroud)
这里是renderMap()函数
function renderMap(){
var startX = playerX - (screenW / 2);
var startY = playerY - (screenH / 2);
maxX = playerX + (screenW / 2);
maxY = playerY + (screenH / 2);
$.getJSON('mapa3.json', function(json){
for (x = startX; x < maxX; x=x+32){
for (y = startY; y < maxY; y=y+32){
intTile = json.layers[0].data[((y/32)* 100) + (x/32)];
context.putImageData(getTile(intTile - 1), …Run Code Online (Sandbox Code Playgroud) 我有一个函数loadTileSet().此函数必须返回arrTiles(图像数据数组),但函数返回UNDEFINED.我正在使用push将数据放入数组中..
function loadTileSet(){
var canvas = document.getElementById('fakeCanvas');
$('#fakeCanvas').hide();
var ctx = $("canvas")[0].getContext('2d');
var imgTileSet = new Image();
imgTileSet.src = 'tileset.png';
var imageTileNumWidth = 23;
var imageTileNumHeight = 21;
var arrTiles = [];
imgTileSet.onload = function(){
var imageWidth = imgTileSet.width;
var imageHeight = imgTileSet.height;
sndCanvasWidth = imageWidth/imageTileNumWidth;
sndCanvasHeight = imageHeight/imageTileNumHeight;
canvas.width = imageWidth;
canvas.height = imageHeight;
ctx.drawImage(imgTileSet,0,0,imageWidth,imageHeight);
var i=0;
var j=0;
var t=0;
for(i=0;i<imageWidth;i+=sndCanvasWidth){
for(j=0;j<imageHeight;j+=sndCanvasHeight){
var myImageData = ctx.getImageData(j,i,sndCanvasWidth,sndCanvasHeight);
arrTiles.push(myImageData);
}
}
return arrTiles;
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我尝试将数组放入另一个
var arrNew = loadTileSet(); …Run Code Online (Sandbox Code Playgroud)