使用html5时,我试图在画布上编写游戏,但是一旦我将动画打开约5-10分钟,垃圾收集就会变得如此频繁,以至于我无法玩游戏,因为整个游戏画布将开始完全故障。
我正在使用ezpz.js @ ezpzjs.org。 ezpz.js尝试重新创建codekulptor的魔力,但是codekulptor只能在弹出窗口中运行。
这不是一个相同的程序,但是原则上它与更多的对象移动非常相似。
在此处看到了实际操作:http://www.ezpzjs.org/PicExplore.html
<script>
// var load;
var ww = "background.jpg";
var yy = "cards_jfitz.png";
ImageLoad(yy);
ImageLoad(ww);
var Pos = [1493, 675];
var XVel = 0;
var YVel = 0;
// name, width, height
canvas("myCanvas", 500, 500);
// Function From Timer
function MadAlerts()
{
// name, loc, x-Cen-Src, Y-Cen-Src,widnip,htnip, x placement, y place, wid of place, ht of place, roatation
canvasComplexImage("myCanvas", ww, Pos[0], Pos[1], 500, 500, 250, 250, 500, 500, 0);
canvasComplexImage("myCanvas", yy, 36, 48, 72, 96, 250, 250, 72, 96, 0);
Pos[0] = Pos[0] + XVel;
Pos[1] = Pos[1] + YVel;
}
window.onkeydown = KeydownControl;
window.onkeyup = KeyupControl;
// Keydown Handler
function KeydownControl(e)
{
if (KeyFinder(e.keyCode) == "Right")
{
XVel = 4;
}
if (KeyFinder(e.keyCode) == "Left")
{
XVel = -4;
}
if (KeyFinder(e.keyCode) == "Up")
{
YVel = -4;
}
if (KeyFinder(e.keyCode) == "Down")
{
YVel = 4;
}
}
// Keyup Handler
function KeyupControl(e)
{
if (KeyFinder(e.keyCode) == "Right")
{
if (XVel == -4)
{
XVel = -4;
}
else
{
XVel = 0;
}
}
if (KeyFinder(e.keyCode) == "Left")
{
if (XVel == 4)
{
XVel = 4;
}
else
{
XVel = 0;
}
}
if (KeyFinder(e.keyCode) == "Up")
{
if (YVel == 4)
{
YVel == 4;
}
else
{
YVel = 0;
}
}
if (KeyFinder(e.keyCode) == "Down")
{
if (YVel == -4)
{
YVel = -4;
}
else
{
YVel = 0;
}
}
}
// Timer Name, Timer Var, Incremental (similar to codeskulptor), function to call
TimerInterval("MyTimer", "TimerVar", 15, MadAlerts);