15-选择拼板显示
4.4.3 选择拼板显示
下面使用animationFrames数组的frameIndex参数计算来自要复制到画布上的拼图的32 × 32的源矩形。首先,找到将要复制的拼板左上角的x坐标和y坐标。为此,在drawScreen()函数中为每一帧创建本地变量,以此计算它在拼图上的位置。sourceX变量将储存左上角的x坐标,sourceY变量储存左上角的y坐标。
下面计算sourceX的伪代码。
sourceX = integer(current_frame_index modulo
the_number_columns_in_the_tilesheet)* tile_width
模运算可以得出除法运算的余数。下面是这项运算的实际代码。
var sourceX = Math.floor(animationFrames[frameIndex] % 8)*32;
计算sourceY值的方法类似,只不过这里使用除法而不使用模运算。
sourceY = integer(current_frame_index divided by
the_number_columns_in_the_tilesheet)*tile_height
下面是运算的实际代码。
var sourceY = Math.floor(animationFrames[frameIndex] / 8)*32;