Drawing sprites with For loop help

Discussion in 'Learning Javascript' started by RespectWomen, Apr 21, 2019.

  1. RespectWomen

    RespectWomen Acoustic Child Veteran

    Messages:
    57
    Likes Received:
    14
    Location:
    Brooklyn, New York
    First Language:
    English
    Primarily Uses:
    RMMV
    I'm trying to draw a set of sprites with a for loop, and the part I can't figure out is how to use the index on the sprites themselves.

    Code:
            for (i = 0; i < 5; i++) {
            this._box = new Sprite_Button();
            if (typeof mbox[i] == 'undefined') {this._box.bitmap = ImageManager.loadPicture('Icon Panel')} else {this._box.bitmap = ImageManager.loadPicture($dataActors[mbox[i]].meta.icon)}
            this.addChild(this._box);
            this._box.x = 75 + (161 * i);
            this._box.y = 300;       
            }
    Running that successfully draws 5 sprites but I don't know how to modify them after that, if I try to change properties of this._box it says undefined so they must be under another name.

    I tried this._box+i but it didn't work, what's the correct way to add it?
     
    #1
  2. Poryg

    Poryg Dark Lord of the Castle of Javascreeps Veteran

    Messages:
    3,996
    Likes Received:
    10,114
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    Code:
    this._boxes = []
    for (i = 0; i < 5; i++) {
           this._boxes[i] = new Sprite_Button();
           if (typeof mbox[i] == 'undefined') {this._boxes[i].bitmap = ImageManager.loadPicture('Icon Panel')} else {this._boxes[i].bitmap = ImageManager.loadPicture($dataActors[mbox[i]].meta.icon)}
           this.addChild(this._boxes[i]);
           this._boxes[i].x = 75 + (161 * i);
           this._boxes[i].y = 300;       
           }
    Now you can access them via this._boxes[0-4].
     
    #2
    RespectWomen likes this.

Share This Page