Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawing more than 1 to canvas wont work?
    primarykey
    data
    text
    <p>I am trying to draw enemy ships to the canvas that will come from the right at a random x being the height of the canvas and a random y past the right side + 1000.</p> <p>This works fine however I am trying to make it automated and the code runs run it just does not work on screen, only 1 is drawn? Any more info that you need just ask, It's really frying my brain I went line by line for around 3 hours and can't see an issue.</p> <p>Before I added this code and just called one manually: <a href="http://www.taffatech.com/DarkOrbit.html" rel="nofollow">http://www.taffatech.com/DarkOrbit.html</a></p> <p>After I added this code for automatic: (it kinda looks like its overlapping) <a href="http://www.taffatech.com/test.html" rel="nofollow">http://www.taffatech.com/test.html</a></p> <p>Globals:</p> <pre><code>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 </code></pre> <p>Then my init() calls a startLoop:</p> <pre><code>function startLoop() { isPlaying = true; Loop(); startSpawningEnemies(); } function stopLoop() { isPlaying = false; stopSpawningEnemies(); } function Loop() { if (isPlaying == true) { Player1.draw(); requestAnimFrame(Loop); drawAllEnemies(); } </code></pre> <p>then they use these functions:</p> <pre><code>function spawnEnemy(n) //total enemies starts at 0 and every-time you add to array { for (var x = 0; x &lt; n; x++) { enemies[totalEnemies] = new Enemy(); totalEnemies++; } } function drawAllEnemies() { ClearEnemyCanvas(); for(var i = 0; i &lt; 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); } </code></pre> <p>which in turn calls the Enemy class:</p> <pre><code>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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload