-2
嘿所以我现在试图让一架飞机从一个窗口移动到另一个窗口,遵循这个顺序:从左上到右上,从右上到右下,从从右下到左下,然后从左下到左上。该项目被迫从右上角到右下角以及从左下角到左上角的加速移动。我用了一个开关的情况下,就像我这一点在Java,但它似乎没有当飞机撞击窗户最大宽度..任何帮助将不胜感激被停止......一个noob程序员第一次尝试
var ctx;
var img;
var larg = 1200;
var alt = 560;
var xAviao = 0;
var yAviao = 0;
c = 1
// Parâmetros do movimento acelerado
var x0 = 0;
var y0 = 0;
var v0 = 0;
var a = 120; // 120 px/s2
var t = 0;
function init() {
canvas = document.getElementById("cvs");
ctx = canvas.getContext("2d");
canvas.setAttribute("width", larg);
canvas.setAttribute("height", alt);
img = new Image();
img.src = "aviao.png";
gameLoop();
}
function gameLoop() {
window.requestAnimationFrame(gameLoop);
update();
render();
}
function update() {
var direction = 1;
if(xAviao > (larg - img.Width))
{
y0 = 0;
a = 120;
t = 0;
xAviao = (larg - img.Width);
direcao = 2;
}
else if (yAviao > alt - img.Height)
{
yAviao = alt - aviao.Height;
direction = 3;
}
else if (xAviao < 0)
{
xAviao = 0;
t = 0;
y0 = yAviao;
a= -120;
direction = 4;
}
else if (yAviao < 0)
{
yAviao = 0;
direction = 1;
}
switch(direction){
case 1:
xAviao +=10 ;
console.log(c);
break;
case 2:
t += 1/60; // time increases 60 seconds per 60 frames
yAviao = Math.floor(y0 + v0*t + a*t*t);
break;
case 3:
xAviao -= 10;
break;
case 4:
t += 1/60; // time increases 60 seconds per 60 frames
yAviao = Math.floor(y0 + v0*t + a*t*t);
break;
}
}
function render() {
// Retângulo do fundo
ctx.fillStyle = "#FFA060";
ctx.beginPath();
ctx.rect(0,0,larg,alt);
ctx.fill();
// Desenha o avião
if(img != undefined)
ctx.drawImage(img, xAviao, yAviao);
}