我正在尝试创建一个视频对象的2D数组。该代码适用于常规数组但是当将其转换为2D时我得到错误 - 未捕获的TypeError:无法读取未定义的属性'tv'。我认为问题在于这行代码:tvObjArray.push(new NewTvObj(createVideo(videoFiles [i] [j]),false,0,0));
代码:
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() {
// 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();
}
}
答案 0 :(得分:0)
请使用此:
var NewTvObj = function(tvObj, playingBool, xposVal, yposVal) {
this.tv = tvObj;
this.playing = playingBool;
this.xpos = xposVal;
this.ypos = yposVal;
}
并确保定义tvObjArray。
答案 1 :(得分:0)
您尚未声明“tvObjArray”。由于您在许多地方访问它,您需要在根目录中声明:
var tvObjArray = [];
您的“tvObjArray”没有“videoFiles”所具有的嵌套功能。当你将你的视频推送到“tvObjArray”时,这只是“tvObjArray”中的一个对象,它没有“i”和“j”的内在链接。我会做这样的事情:
var tvObj1 = new NewTvObj(createVideo(videoFiles[i][j]), false, 0, 0);
tvObj1.tv.loop();
tvObj1.tv.hide();
tvObj1.tv.pause();
tvObjArray.push( tvObj );
这个新对象现在在tvObjArray [tvObjArray.length-1];
此外,for循环的一个方便的快捷方式是:
for (var i in videoFiles) {
for (var j in videoFiles[i]) {
我对p5并不熟悉,所以我尽力帮助。
干杯。