0

I want to style the second elment in this array by adding a CSS Property

here is a global variable to define the array

<canvas id="canvas"></canvas>
<script>
    var paddles = [2], // Array containing two paddles
function update() {
    // Update scores

    updateScore(); 
    // Move the paddles on mouse move
    // Here we will add another condition to move the upper paddle in Y-Axis
    if(mouse.x && mouse.y) {
        for(var i = 1; i < paddles.length; i++) {

        p = paddles[i];

        // the botoom paddle
        if (i ==1){
            p.x = mouse.x - p.w/2;
        }else{
        // the top paddle
                    //paddles[2].x = mouse.x - p.w/2;
                    debugger
                 paddles[2].style.backgroundColor="red"; 

    }



        }       
    }

and here what the style I want

         paddles[2].style.backgroundColor="red"; 

when I use the debugger I face this problem

TypeError: paddles[2].style is undefined

enter image description here

7
  • 1
    what is in paddles the actual dom element or just the string id? Commented Sep 19, 2014 at 15:56
  • post more code so we can see context. Commented Sep 19, 2014 at 15:56
  • 1
    You may need document.getElementById(paddles[2]).style.backgroundColor = "red"; Commented Sep 19, 2014 at 15:57
  • @brso05 I'm using an html5 Canvas the code is too long +500 line of code the post will be updated thx for ur reply Commented Sep 19, 2014 at 15:58
  • 2
    From what you've shown us so far, paddles is a single element array, containing the number 2 as the only element. Commented Sep 19, 2014 at 16:00

2 Answers 2

2

UPDATE:

Since it looks like you are creating some kind of "pong" or "breakout" game, I decided to take my first stab at HTML canvas and do it myself for fun. Here is a simple version that shows how to:

  1. draw two paddles on a canvas (original author)
  2. keep track of the "boxes" for the paddles in an array (original author)
  3. use a loop via setInterval to redraw the canvas as it gets updated (original author)
  4. use the keyboard to move shapes around on HTML canvas (my code)

See the fiddle for working demo and full code: http://jsfiddle.net/z4ckpcLc/1/

I will not post the full code because I didn't write most of it.. I used the example from this site for the code for drawing the boxes and for keeping track of them in an array: http://simonsarris.com/project/canvasdemo/demo1.html

The function I added to this example is the arrowKeyMove() handler, wired up to the onkeydown event of document.body via this line: document.body.onkeydown = arrowKeyMove;

function arrowKeyMove(e) {
    var direction = 0; // -1 means left, 1 means right, 0 means no change
    var moveFactor = 10; // higher factor = more movement when arrow keys pressed
    var arrowKeyUsed = false; // to indicate which 'paddle' we are moving
    switch (e.which) {
        case 37:
            // left arrow (upper paddle)
            direction = -1;
            arrowKeyUsed = true;
            break;
        case 39:
            // right arrow (upper paddle)
            direction = 1;
            arrowKeyUsed = true;
            break;
        case 65:
            // "a" key for left strafe (lower paddle)
            direction = -1;
            break;
        case 68: 
            // "d" key for right strafe (lower paddle)
            direction = 1;
            break;
    }
    var boxIndex = 1; // box index defaults to lower paddle
    if (arrowKeyUsed) { // if using arrow keys, we are moving upper paddle
        boxIndex = 0; 
    }
    var maxX = 240; // constrain movement to within 10px of box borders (240 == canvas width minus paddle width minus padding)
    var minX = 20;
    var box = boxes[boxIndex]; // grab the box; we will update position and redraw canvas
    if((direction < 0 && box.x >= minX) || (direction > 0 && box.x <= maxX))
    {
      // move the box in the desired direction multiplied by moveFactor
      box.x = box.x + (direction * moveFactor);
      invalidate(); // invalidate canvas since graphic elements changed
    }
}

ORIGINAL ANSWER:

Array items use zero-based indexing.

If you only have two paddles like you said, you must use index 1, not 2. And if you want to access the first paddle, use 0, not 1. You probably want your for loop to use var i=0 instead, and basically change places you are checking 1 to 0.

For example:

paddles[0].style.backgroundColor="red";  // paddle 1
paddles[1].style.backgroundColor="red";  // paddle 2

Also, var array = [2] does not create a two-array element. It creates a one-array element with an integer value of 2

For DOM elements you may want something like this:

<div id='paddle1'></div>
<div id='paddle2'></div>

<script type='text/javascript'>
var paddles = [];
paddles[0] = document.getElementById('paddle1');
paddles[1] = document.getElementById('paddle2');
paddles[0].style.backgroundColor="red";  // paddle 1 is red
paddles[1].style.backgroundColor="orange";  // paddle 2 is orange
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

thx for ur replay , but I can't use getElementById as I'm using HTML5 Cancas and draw & update methods ... I updated the declaration of the array and the debugger face the same problem.
I've no problem with the indexing of the array more rather than the property undefined .. I tried index 0 as well.
If you are using the canvas, then like you said you are not using individual DOM elements, and you cannot use style. You probably want to use fill() instead. If you google "move objects HTML canvas" (google.com/#q=move+shapes+html+canvas) you can find numerous examples of this. For example, this link may help: simonsarris.com/blog/140-canvas-moving-selectable-shapes
I found this stackoverflow.com/questions/5998924/… explain how to rotate specific object in the canvas .... thx for your Answer I'll work on it.
@Maged: Taking a break from work to make a browser pong game seemed liked a great Friday activity. Check this out, I think this is what you are looking for: jsfiddle.net/z4ckpcLc/1
|
0

I'm not sure, but maybe you can use something like this:

paddles[2].css('background-color','red'); 

edit: now I see you don't use jQuery, so my solution wouldn't work

Comments

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.