create a 2D array of video objects - Uncaught TypeError

I am trying to create a 2D array of video objects. The code works for a regular array however when turning it into a 2D I get the error - Uncaught TypeError: Cannot read property 'tv' of undefined. I think the issue is in this line of code but haven't been able to work it out: tvObjArray.push(new NewTvObj(createVideo(videoFiles[i][j]), false, 0, 0));

full code:

var tvObjArray = [[],[]];
var videoFiles = [
  ["videos/cityDream.mp4", "videos/cityDream.mp4"],
  ["videos/cityDream.mp4", "videos/cityDream.mp4"]
];

function setup() {
  createCanvas(1000, 576);
  // create a new tv object for each screen that needs to appear
  for (var i = 0; i < videoFiles.length; i++) {
    for (var j = 0; j < videoFiles[i].length; j++) {
      tvObjArray.push(
        new NewTvObj(createVideo(videoFiles[i][j]), false, 0, 0));
      tvObjArray[i][j].tv.loop();
      tvObjArray[i][j].tv.hide();
      tvObjArray[i][j].tv.pause();
    }
  }
}

function NewTvObj(tvObj, playingBool, xposVal, yposVal) {
  this.tv = tvObj;
  this.playing = playingBool;
  this.xpos = xposVal;
  this.ypos = yposVal;
}

function draw() {
  background(255, 100, 100);

  // locate tvs on building
  for (var i = 0; i < videoFiles.length; i++) {
    for (var j = 0; j < videoFiles[i].length; j++) {
  image(tvObjArray[i][j].tv, tvObjArray[i][j].xpos + i * 320, 
    tvObjArray[i][j].ypos + j * 200);
}

drawSprites();
  }
}

Answers

  • edited December 2015

    ah, deleted.

  • At the moment I see no need for a 2d array; except to make your life difficult... Show us your working code for the 1d array and justify the need for a 2d array...

    I see two likely issues:

    tvObjArray isn't declared anywhere so you're pushing to a non-existent array with this:

    tvObjArray.push(/*...*/)

    I'd expect that to throw an error - check the console: hit F12 in your browser...

    Note that even if you declare tvObjArray as a 2d array push() will not work as you seem to expect. That means all your 2d array references such as tvObjArray[i][j] are probably throwing errors too or just returning undefined... To confirm add console.log(tvObjArray); and check the console with F12: Chrome will let you navigate the contents of an array output to log: the best you can hope for with the current code is that you get a 1d array...

    I can't suggest any more than that since I can't easily test your code without finding video files to test with...

  • edited December 2015

    @blindfish, just seen you were 1 more victim of the "tone" speaking extermination squad there! 8-|

  • :D

    I think I can cope with a canned response; and to be honest it was fair enough: I was rather abrupt >:)

  • Hi @blindfish thank you for your feedback. I found the forum after the github and realized too late I posted in the wrong place. It won't happen again.

    I'm not sure why you're questioning my need for a 2d matrix, and whether my code works for a 1d matrix - clearly I am getting the outcome I want and that justifies that it is working? The reason why I need a 2d matrix is because I need a visual 2d matrix output of videos and so I logically thought of making a 2d array of objects. My code with the 1d array is a lot more intense with a bunch of other array objects and I accidentally deleted the wrong array declaration when cleaning up the code for this post, many apologies for that.

    Anyway the suggestion that @lmccart gave on the github worked perfect so thank you very much for that! Appreciate the quick responses from all, i've been on a plane for the last 2 days and only just was able to sit down with wifi.

Sign In or Register to comment.