2013-06-04 13 views
1

我試圖畫出敵船來自右邊的隨機x的畫布,這個x是畫布的高度,而隨機y是右邊+ 1000.繪製超過1的畫布將無法工作?

這很好,但我試圖爲了使其自動化並運行代碼,它只是在屏幕上不起作用,只繪製了1個?任何更多的信息,你只需要問,這真是油炸我的大腦,我一行一行地排隊了大約3小時,並沒有看到問題。

在我加入這個代碼,並手動就叫一個:http://www.taffatech.com/DarkOrbit.html

後,我加入這個代碼自動:(它還挺看起來像它重疊) http://www.taffatech.com/test.html

全局:

var spawnInterval; 
var totalEnemies = 0; //leave as is 
var enemies = []; //array of enemy objects 
var spawnRate = 2000; //every 2 seconds 
var spawnAmount = 3; //amount of enemies spawned 

然後我的init()調用startLoop:

function startLoop() 
{ 

isPlaying = true; 
Loop(); 
startSpawningEnemies(); 

} 

function stopLoop() 
{ 
isPlaying = false; 
stopSpawningEnemies(); 
} 

function Loop() 
{ 
if (isPlaying == true) 
{ 
Player1.draw(); 
requestAnimFrame(Loop); 
drawAllEnemies(); 

} 

然後他們利用這些功能:

function spawnEnemy(n) //total enemies starts at 0 and every-time you add to array 
{ 
    for (var x = 0; x < n; x++) 
    { 

    enemies[totalEnemies] = new Enemy(); 
    totalEnemies++; 
    } 

} 

function drawAllEnemies() 
{ 

    ClearEnemyCanvas(); 
    for(var i = 0; i < enemies.length; i++) 
    { 
     enemies[1].draw(); 

    } 
} 

function startSpawningEnemies() 
{ 
    stopSpawningEnemies(); 

    spawnInterval = setInterval(function() {spawnEnemy(spawnAmount);}, spawnRate); //this calls spawnEnemy every spawnRate 
    /////////spawn 'spawnAmount' enemies every 2 seconds 




} 


function stopSpawningEnemies() 
{ 

clearInterval(

spawnInterval); 
} 

進而調用敵對階級:

function Enemy() //Object 
{ 

//////Your ships values 
this.EnemyHullMax = 1000; 
this.EnemyHull = 1000; 
this.EnemyShieldMax = 1000; 
this.EnemyShield = 347; 
this.SpaceCrystalReward = 2684; 
this.EnemySpeed = 2; //should be around 6 pixels every-time draw is called by interval, directly linked to the fps global variable 
//////////// 



////Pick Ship 
this.type = "Hover"; 
this.srcX = EnemySrcXPicker(this.type); 
this.srcY = EnemySrcYPicker(this.type); 

this.enemyWidth = EnemyWidthPicker(this.type); 
this.enemyHeight = EnemyHeightPicker(this.type); 

this.drawX = EnemydrawXPicker(this.type); 
this.drawY = EnemydrawYPicker(this.type); 
//// 


} 

Enemy.prototype.draw = function() 
{ 

this.drawX -= this.EnemySpeed; 
ctxEnemy.globalAlpha=1; 
ctxEnemy.drawImage(spriteImage,this.srcX,this.srcY,this.enemyWidth,this.enemyHeight,this.drawX,this.drawY,this.enemyWidth,this.enemyHeight); 

} 



function EnemySrcXPicker(type) 
{ 
if (type == "Hover") 

    { 

     return 906; 
     } 
} 

function EnemySrcYPicker(type) 
{ 
if (type == "Hover") 

    { 
     return 616; 
    } 
} 

function EnemydrawXPicker(type) 
{ 
if (type == "Hover") 

    { 
     return Math.floor(Math.random() * 1000) + canvasWidthEnemy; 
    } 
} 

function EnemydrawYPicker(type) 
{ 
if (type== "Hover") 

    { 
     return Math.floor(Math.random() * (canvasHeightEnemy - 72)); 
    } 
} 


function EnemyWidthPicker(type) 
{ 
if (type == "Hover") 

    { 
     return 90; 
    } 
} 

function EnemyHeightPicker(type) 
{ 
if (type == "Hover") 

    { 

     return 72; 
    } 
} 

回答

2
for(var i = 0; i < enemies.length; i++) 
{ 
    enemies[1].draw(); 
} 

也許應該

for(var i = 0; i < enemies.length; i++) 
{ 
    enemies[i].draw(); 
} 

...或者你會一遍又一遍地畫同一個敵人。

+1

這個答案是那麼好,我甚至都不需要閱讀知道它的權利的問題 – 0xor1

+0

謝謝,我錯過了,我不知道! – Barney

0

在你drawAllEnemies功能,不應該

enemies[1].draw(); 

enemies[i].draw(); 

0

當環路enemies數組,你應該使用索引i你設置,以便它不會不斷地得出同樣的元素:

for(var i = 0; i < enemies.length; i++) 
{ 
    enemies[i].draw(); 
}