0

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.

I am only placing my necessary 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 <= 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";}
4
  • Do you expect each player to have multiple missles? Currently, you have an array and push missles to it, but at the same time you act as if there is only one missle by doing e.g. missles.visible. Commented Sep 7, 2012 at 16:48
  • Well, there is only going to be one player... I'm trying to create a Space Invaders game for class... So the one player is supposed to have missles and i would like more than 1 missle to appear on the screen at a time... Thank you for the fast response! Commented Sep 7, 2012 at 16:53
  • @steveax Please note that the homework tag is now being phased out. D34thSt4lker: if you have a new question, ask it separately. Commented Sep 15, 2012 at 14:23
  • @Gilles noted. I'll help with the cleanup. Commented Sep 15, 2012 at 17:43

1 Answer 1

1

Your player is supposed to have several missles, which you denoted by using an array:

this.missles = new Array();

When using an array, you have to loop (using e.g. for) to apply something to each element. So, doing player.missles.visible = "false" really just sets a property on the array itself, and not on each element. You'd have to do:

for(var i = 0; i < player.missles.length; i++) {
  player.missles[i].visible = "false";  // player.missles[i] is one item on each iteration
}

The same goes for the drawing: fetch and update data of each missle in a loop:

for(var i = 0; i < player.missles.length; i++) {
  if ( player.missles[i].visible == "true" ) {
    // ...
}

Also, note that there are booleans in JavaScript. You can use just true/false, and you can then directly use an if like this:

if(player.missles[i].visible) {
  // ...
Sign up to request clarification or add additional context in comments.

2 Comments

Hey @pimvdb not sure yet how this forum works but i edited my initial question (1 more question) and thought maybe you can help me out again... Thanks!! Much appreciated
Nevermind... I realized that I had my array named "points" and was using "pos" to fill it up... Thank you anyways!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.