非常感谢任何输入!
长话短说,我需要弄清楚如何使用 HTML5/CSS3 按钮来控制画布上的精灵动画。这些按钮分别控制精灵的方向和速度。
我有两个数组,它们代表两种类型的精灵控件,每个控件都有不同程度的索引(1 个 w/三个索引和 1 个 w/8 个索引)。我应该用什么方法将按钮链接到我的精灵各自的“转换”?我的精灵在 8 索引数组中平铺和索引,因此传统上转换图像是不必要的。我需要单击每个按钮将特定数组更改为特定索引,然后将其插入到 drawImage() 和 clearRect () 公式中以创建动画。每次单击应该只更改一个数组和索引号,其他一切都保持不变。. . 我是否正确地将速度和方向值放入数组中?有任何想法吗?
HTML
Div controlPanel 在画布上方以 z 为索引,并包含所有控制按钮。
<div id="controlPanel">
<div id="rightButtons">
<div id="fast">
<button type="button" class="speed" name="fast" value="2"></button>
</div>
<div id="medium">
<button type="button" class="speed" name="medium" value="1"></button>
</div>
<div id="slow">
<button type="button" class="speed" name="slow" value="0"></button>
</div>
</div>
<div id="bottomButtons">
<div id="H0">
<button type="button" class="heading" name="H0" value="0"></button>
</div>
<div id="H1">
<button type="button" class="heading" name="H1" value="1"></button>
</div>
<div id="H2">
<button type="button" class="heading" name="H2" value="2"></button>
</div>
<div id="H3">
<button type="button" class="heading" name="H3" value="3"></button>
</div>
<div id="H4">
<button type="button" class="heading" name="H4" value="4"></button>
</div>
<div id="H5">
<button type="button" class="heading" name="H5" value="5"></button>
</div>
<div id="H6">
<button type="button" class="heading" name="H6" value="6"></button>
</div>
<div id="H7">
<button type="button" class="heading" name="H7" value="7"></button>
</div>
</div>
分机。JAVASCRIPT
var heading = [180,210,0,30,60,90,120,150] //x-coordinates of the sprite tile
var speed = [5,10,20];
document.getElementById("plane").onload=function(){
var canvasTwo = document.getElementById("canvasTwo");
var cxt = canvasTwo.getContext("2d");
var plane = document.getElementById("plane");
animatePlane();
}
function animatePlane(){
setInterval(drawPlane,200);
}
var plane = document.getElementById("plane");
var dy = speed;
var dx = s;
var x = 400;
var y = 475;
function drawPlane(){
y = y+dy;
x = x + dx;
cxt.save();
cxt.clearRect(x-dx,y-dy,30,30);
cxt.drawImage(plane,heading,0,30,30,x,y,30,30);
cxt.restore();
}
//This is where dx and dy get alittle funky. The value of dx and dy depend on the heading. Quite frankly I don't know where to place this block of code.
//to determine the value of dx
if (heading[]= 0){
dx= speed[];
dy= 0;
}
if (heading[]= 30){
dx= speed[];
dy= speed[]*-1;
}
if (heading[]= 60){
dx= 0;
dy= speed[]*-1;
}
if (heading[]= 90){
dx= speed[]*-1;
dy= speed[]*-1;
}
if (heading[]= 150){
dx= speed[]*-1;
dy= speed[];
}
if (heading[]= 180){
dx= 0;
dy= speed[];
}
if (heading[]= 210){
dx= speed[];
dy= speed[];
}