0
我想通過我的數組中的3個圖像循環並將其添加到舞臺(使用easelJS)。我也想定位它。當我嘗試訪問數組中的圖像時,出現錯誤,說我無法設置未定義的x。爲什麼不能訪問easeljs位圖的x變量?爲什麼我不能訪問位圖的x變量? (javascript)
function displayPosters() {
getPosters(); //get all images and assign it to designated array
console.log(frontPosters);
console.log(frontPosters.length);
if(currentCat == Category.HOME) { //if current category is HOME
for(var i = 0; i < frontPosters.length; i++) { //loop through posters
frontPosters[i].x = 40; //set x position for testing, also where error occurs
stage.addChild(frontPosters[i]); //add poster to stage
}
}
}
這裏是加載和推送這些圖像到frontPosters arrray的代碼。
var frontPosters = new Array(3);
function getPosters() {
var games = new Image(); //create 3 images
var apps = new Image();
var aboutme = new Image();
games.onload = function() { //add image to frontImages array on load
var gamesPost = new createjs.Bitmap(games);
frontPosters[0] = gamesPost;
};
apps.onload = function() {
var appPost = new createjs.Bitmap(apps);
frontPosters[1] = appPost;
};
aboutme.onload = function() {
var amPost = new createjs.Bitmap(aboutme);
frontPosters[2] = amPost;
};
games.src = "images/assets/posters/games_poster.jpg";
apps.src = "images/assets/posters/apps_poster.jpg";
aboutme.src = "images/assets/posters/aboutme_poster.jpg";
}
問題是,當我做「frontPosters [0] .x = 40;」或類似的東西,錯誤仍然發生。 – kag359six 2013-02-21 23:50:14
那麼只要我們不知道'frontPosters'包含什麼,恐怕不可能幫助你。您可以嘗試在'for'循環之前添加'console.log(frontPosters);'然後打開您的開發工具(或螢火蟲)控制檯並查看該數組包含的內容。如果它沒有澄清事情,請將它添加到您原來的帖子中,至少我們會知道我們正在處理的是什麼。 – iMoses 2013-02-21 23:51:29
我說它包含3張圖片。但如果有幫助,我會發布處理這些圖像的代碼。 – kag359six 2013-02-21 23:53:26