2017-04-02 48 views
-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); 
} 

回答

0

我認爲你有如果在update函數中使用statememt,則在第一個簡單的輸入錯誤中。 direcao = 2應該不是direction = 2