WDI-SEA / game-project-issues

0 stars 0 forks source link

Randomizing enemy shots #52

Closed cory-yonomi closed 3 years ago

cory-yonomi commented 3 years ago

What's the problem you're trying to solve?

I'd like a random alien to fire a projectile every few seconds.

Post any code you think might be relevant (one fenced block per file)

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

I can either have one shoot immediately at the start then no more, or a continuous stream of shots from random aliens, making it look like a blizzard

What is your best guess as to the source of the problem?

How my animation/game loop is being built

What things have you already tried to solve the problem?

Different locations for the alien.shoot() function with a setInterval

DoireannJane commented 3 years ago

Are you still working on this issue?

cory-yonomi commented 3 years ago

I am

tkolsrud commented 3 years ago

Can you post the code block? Either for the single shot/blizzard or both?

cory-yonomi commented 3 years ago

`//fire a missile this.shoot = function () { let randomAlien = Math.floor(Math.random()*alienFleet.length) shootMissile(alienFleet[randomAlien]) } let animationId const animate = () => { animationId = requestAnimationFrame(animate) // clear the canvas ctx.fillStyle = 'rgba(0, 0, 0, 0.1)' ctx.clearRect(0, 0, game.width, game.height) // render our player player.render() for (i = 0; i < barriers.length; i++){ barriers[i].render() } if (alienFleet.length == 0 && player.points <= 0) { createFleet() } else if (alienFleet.length == 0 && player.points > 1) { player.win = true displayWin() cancelAnimationFrame(animationId) } else { for (i = 0; i < alienFleet.length; i++){ alienFleet[i].render() } } aliensRemaining.innerText = alienFleet.length alien.move() missiles.forEach((missile) => { missile.render() missile.update() })

detectMissileHit()

} `

Sorry, was going back and forth between so much didn't know what to share. This fires off one missile at the very start, and no more.

cory-yonomi commented 3 years ago

Jason helped me out, got it working.