locked
Using assetManager in template with images

    Question

  • Hi,

    In the JavaScript game sample i need to apply the same image to a bunch of objects in an array so that each displays the same image but different copies of it in different locations on the screen. I have the the image loaded in the asset manager but how do i get it from the asset manager to each object and display it on the gameContext?

    My class of objects is:

    var Roach = WinJS.Class.define(function () {
        this.position = { x: Math.random() * gameCanvas.width + 1, y: Math.random() * gameCanvas.height + 1 },
        this.squished = false;
    // How do i set the image to be a property of the orach object???
        this.image = GameManager.assetManager.assets.roachImage;
    },
    {
        move: function (speed) {
            // Move x
        },
        squish: function() {
            this.squished = true;
        }
    });

    in my assetManager i have:

    var assets = {
                sndBounce: { object: null, fileName: "/sounds/bounce.wav", fileType: AssetType.audio, loop: false },
                roachImage: { object: null, fileName: "/images/roach.png", fileType: AssetType.image },

    then im my draw function i loop through an array of these roach objects and move the images to the new positions for each roach:

    for (var i = 0; i < this.state.roaches.length; i++) {
                    if (!this.state.roaches[i].squished) {
                        this.state.roaches[i].image.x = this.state.roaches[i].position.x;
                        this.state.roaches[i].image.y = this.state.roaches[i].position.y;   
                    }
                }

    Any pointers?

    thanks,

    Nat

    Thursday, November 1, 2012 9:46 PM