任何輸入都非常感謝!長話短說,我需要弄清楚如何使用HTML5/CSS3按鈕來控制畫布上的精靈動畫。按鈕分別控制精靈的方向和速度。我有兩個數組,代表兩種類型的精靈控件,每個精靈控件都有不同程度的索引(1 w/3個索引和1 w/8索引)。我應該採用什麼方法將按鈕鏈接到它們各自的「精靈」變形?我的精靈在8索引陣列中平鋪和索引,因此傳統上轉換圖像是不必要的。我需要單擊每個按鈕將特定數組更改爲特定索引,然後將其插入到drawImage()和clearRect()公式中以創建動畫。每次點擊只能改變一個數組和索引號,而其他所有內容都是一樣的。 。 。我是否正確地將速度和方向值放在數組中?有任何想法嗎?如何將控件鏈接到Canvas精靈?
HTML
股利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>
EXT。 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[];
}
你可以發佈你現在的代碼的例子嗎?基本上你想要做的是讓你的按鈕編輯一些與繪製項目相關的數據,然後根據編輯後的數據重新繪製項目。從我所看到的,你正在朝着正確的方向思考。 – Cerbrus
@Cerbrus感謝您的回覆。我已經更新了包含代碼的帖子。我應該使用