我正在寻找一个程序,当按下空格键时,该程序将升序排列不同大小的矩形。我尝试排序,但即使排序后也能得到相同的结果。
到目前为止,这是我尝试过的。我正在使用Brackets在JS中进行编码。下面的代码仅用于排序,这是我的主要问题。一次,我得到了解决方案,我可以用任何键对其进行排序。
请帮帮我。
function C_ShapeRect(l_x,l_y,l_w,l_h,l_c)
{
this.Xpos = l_x ;
this.Ypos = l_y ;
this.Width = l_w ;
this.Height = l_h ;
this.Color = l_c ;
this.Draw = function m_drawRect(l_canvas,l_context)
{
l_context.fillStyle = this.Color ;
l_context.fillRect(this.Xpos , this.Ypos , this.Width , this.Height);
};
}
var g_RectArray = new Array(5);
var g_RectArrayLength = g_RectArray.length ;
var g_RectWidth = 20 ;
var g_RectHeight = [35,20,77,10,50] ;
var g_RectColor = ["red","green","blue","yellow","black"] ;
function f_InitRectObject()
{
var l_Xpos = 90;
var l_Ypos = 175;
for(var i=0 ; i<g_RectArrayLength ; i++)
{
g_RectArray[i] = new C_ShapeRect(l_Xpos,l_Ypos,g_RectWidth,-g_RectHeight[i],g_RectColor[i]);
l_Xpos += g_RectWidth ;
}
}
f_InitRectObject() ;
function f_DrawRectObject()
{
for(var i=0 ; i<g_RectArrayLength ; i++)
{
g_RectArray[i].Draw(g_canvas,g_context);
}
}
function f_clearCanvas()
{
g_context.clearRect(0,0,g_canvas.width,g_canvas.height);
g_context.strokeRect(0,0,g_canvas.width,g_canvas.height);
}
var g_tempHeight = [] ;
function f_sortRect(l_array)
{
for(var i=0 ; i<g_RectArrayLength ; i++)
{
for(var j = 0 ; j < (g_RectArrayLength - 1) - i ; j++)
{
if(l_array[j] > l_array[j + 1])
{
g_tempHeight = l_array[j];
l_array[j] = l_array[j+1];
l_array[j+1] = g_tempHeight;
}
}
}
}
function f_GameLoop()
{
f_sortRect(g_RectArray) ;
f_clearCanvas() ;
f_DrawRectObject() ;
}
setInterval(f_GameLoop(),g_timeInterval) ;
catspeake
相关分类