为什么我不能访问位图的x变量? (JavaScript的)

时间:2013-02-21 23:40:54

标签: javascript arrays position easeljs

我正在尝试遍历我的数组中的3个图像并将其添加到舞台上(使用easelJS)。我也想定位它。当我尝试访问数组中的图像时,我得到一个错误,说我不能设置未定义的x。为什么不能访问easeljs Bitmap的x变量?

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";

}

1 个答案:

答案 0 :(得分:1)

使用for(poster in frontPosters)是不好的做法,因为你实际上是迭代数组对象而不是值(a.k.a.Array本身)。请改用for(var i=0; i<frontPosters.length; i++)。这是最简单的解决方案,IMO。

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
}

修改

我认为你正在处理竞争条件。在所有图像加载之前,您将遍历阵列。通过设置var frontPosters = new Array(3);,您可以自动将三个值设置为undefined,这些值将被推送到新数组中。

在继续编写脚本之前,您应检查所有图像是否已加载。

这是给你的一个想法。设置一个仅在加载第三个图像后才会运行的回调。

var frontPosters = new Array(3);

function getPosters(callback) {

    var games   = new Image(),
        apps    = new Image(),
        aboutme = new Image(),
        loaded  = 0;

    games.onload = function() {
        frontPosters[0] = new createjs.Bitmap(this);
        if (++loaded === 3) callback();
    };

    apps.onload = function() {
        frontPosters[1] = new createjs.Bitmap(this);
        if (++loaded === 3) callback();
    };

    aboutme.onload = function() {
        frontPosters[2] = new createjs.Bitmap(this);
        if (++loaded === 3) callback();
    };

    games.src   = "images/assets/posters/games_poster.jpg"; 
    apps.src    = "images/assets/posters/apps_poster.jpg";
    aboutme.src = "images/assets/posters/aboutme_poster.jpg";

}

function displayPosters() {

    getPosters(function() {
        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
            }
        }
    }); //get all images and assign it to designated array, only after all images were loaded

}