Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help in JavaScript working with arrays and objects
    text
    copied!<p>I am sort of new to JavaScript (Just started using it in school) and I have been looking around to get this program done for class. For some reason I just cant get it to work properly (with whatever I currently am working on) I am trying to fill up an Player Object with an array of Missle Objects... Before switching my code to this use of objects, I was able to display the "missles" but now I'm stuck.</p> <p>I am only placing my necessary code:</p> <pre><code>function Player(){ this.x = c.width/2; this.y = c.height-20; this.w = 50; this.h = 10; this.dx = 30; this.score = 0; this.missles = new Array();} function Missle(x, y){ this.x = x; this.y = y; this.dy = 10; this.w = 8; this.h = 8; this.visible = "false";} function init1P(){ if (playing == "true"){ player = new Player(); animate1P(); }} function animate1P(){ cntxt.clearRect( 0, 0, c.width, c.height ); cntxt.fillStyle="#000000"; cntxt.fillRect(0,0,c.width,c.height); cntxt.fillStyle="#ffffff"; cntxt.font="34px Verdana"; cntxt.fillText(player.score, 10, c.height-10); cntxt.fillStyle="#ffffff"; cntxt.fillRect(player.x,player.y,player.w,player.h); //cntxt.fillRect(comp.x,comp.y,comp.w,comp.h); if ( player.missles.visible == "true" ) { cntxt.fillStyle = "#FF0000"; cntxt.fillRect( player.missles.x, player.missles.y, player.missles.w, player.missles.h ); //isHit(); if ( hit == "false" ) { player.missles.y -= player.missles.dy; if ( player.missles.y &lt;= 0 ) { player.missles.visible = "false"; } } } // request new frame if ( playing == "true" ) { requestFrame( function() { animate1P(); } ); } //moveComp(); checkBoundaries(player); //checkBoundaries(comp); //isHit(player); //isHit(comp); } function shoot() { player.missles.push(new Missle(player.x + player.w/2, player.y + 10)); player.missles.visible = "true"; hit = "false";} </code></pre>
 

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