Note that there are some explanatory texts on larger screens.

plurals
  1. POdisplaying sprites after Collision detection
    primarykey
    data
    text
    <p>So when I shoot the enemies they get wiped from the screen, this works.</p> <p>However what I want to happen is I want to place an explosion (4 pngs one after another) basically where the enemy was. The code for the explosion works on its own but im stuck trying to integrate it with my code.</p> <p>Here is the explosion class, as you can see I am having some trouble with the interval as I have no experience with them. I think the error or wrong logic lies in this object.</p> <p>Also for some reason it wipes the other canvas layers :/ Try it here: <a href="http://www.taffatech.com/DarkOrbit.html" rel="nofollow">http://www.taffatech.com/DarkOrbit.html</a></p> <pre><code>function Explosion() { this.srcX = 0; this.srcY = 1250; this.drawX = 0; this.drawY = 0; this.width = 70; this.height = 70; this.currentFrame =0; this.totalFrames =5; this.hasHit = false; } Explosion.prototype.draw = function() //makes it last 10 frames using total frames { if(this.currentFrame &lt;= this.totalFrames) { this.currentFrame++; Exploder(this.drawX,this.drawY); } else { this.hasHit = false; currentFrame =0; } } function Exploder(srcX,srcY) { whereX = this.srcX; whereY = this.srcY; intervalT = setInterval(BulletExplosionAnimate, 80); } var bulletExplosionStart = 0; var whereX =0; var whereY =0; function BulletExplosionAnimate(intervalT) { var wide = 70; var high = 70; if (bulletExplosionStart &gt; 308) { bulletExplosionStart = 0; clearInterval(intervalT); } else { ctxExplosion.clearRect(0,0,canvasWidth,canvasHeight) ctxExplosion.drawImage(spriteImage,bulletExplosionStart,1250,wide,high,whereX,whereY,wide,high); bulletExplosionStart += 77; } } </code></pre> <p>my Bullet object:</p> <pre><code>function Bullet() //space weapon uses this { this.srcX = 0; this.srcY = 1240; this.drawX = -20; this.drawY = 0; this.width = 11; this.height = 4; this.bulletSpeed = 10; this.bulletReset = -20; this.explosion = new Explosion(); } Bullet.prototype.draw = function() { this.drawX += this.bulletSpeed; ctxPlayer.drawImage(spriteImage,this.srcX,this.srcY,this.width,this.height,this.drawX,this.drawY,this.width,this.height); this.checkHitEnemy(); if (this.drawX &gt; canvasWidth) { this.recycle(); } } Bullet.prototype.fire = function(startX, startY) { this.drawX = startX; this.drawY = startY; } Bullet.prototype.checkHitEnemy = function() { for(var i = 0; i &lt; enemies.length; i++) { if( this.drawX &gt;= enemies[i].drawX &amp;&amp; this.drawX &lt;= enemies[i].drawX + enemies[i].enemyWidth &amp;&amp; this.drawY &gt;= enemies[i].drawY &amp;&amp; this.drawY &lt;= enemies[i].drawY + enemies[i].enemyHeight) { this.explosion.drawX = enemies[i].drawX - (this.explosion.width/2); this.explosion.drawY = enemies[i].drawY; this.explosion.hasHit = true; this.recycle(); //bullet resets after hit enemy enemies[i].recycleEnemy(); //change this soon to have if loop if health is down } } } Bullet.prototype.recycle = function() { this.drawX = this.bulletReset; } </code></pre> <p>In my player object I have a function that checks if it has hit an enemy, it works:</p> <pre><code>Player.prototype.drawAllBullets = function() { for(var i = 0; i &lt; this.bullets.length; i++) { if(this.bullets[i].drawX &gt;= 0) { this.bullets[i].draw(); } if(this.bullets[i].explosion.hasHit) { this.bullets[i].explosion.draw(); } } } </code></pre> <p>Currently when I shoot an enemy they disappear but not explosion happens, I know my interval is not great coding, so I need some help with it, thanks!</p>
    singulars
    1. This table or related slice is empty.
    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.
    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