Help: width or height properties of a bitmap/image?

Discussion in 'Learning Javascript' started by wrigty12, Jul 6, 2016.

  1. wrigty12

    wrigty12 Veteran Veteran

    Messages:
    533
    Likes Received:
    76
    First Language:
    English
    I am trying to retrieve the value of the width/height of a picture that I import with ImageManager.loadBitmap. When I try the following code in the game console, it works just fine:


    ImageManager.loadBitmap("img/pictures/puzzle/", "Castle").width;


    However, once I try to set the exact same code to a variable within my plugin, I always get 0. This happens even if I set the image without the ".width" portion to a variable, then do variable.width.


    What am I doing wrong? I cannot find any other way to retrieve the width of an image I wish to import.


    Thanks!
     
    #1
  2. Shaz

    Shaz Veteran Veteran

    Messages:
    37,914
    Likes Received:
    11,586
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    Because images are loaded asynchronously, you need to wait a while, until the image is fully loaded, before you can look at the properties.  When you try in the game console, is that AFTER you have tried to do it in the script?  The script has started the load, and by the time you get that typed into the console, the load has completed.


    I don't think a loop would be suitable for this, as it might stop other processing from happening.  It should be in an update function or something like that, that runs within the engine's existing looping cycle.  You can check ImageManager.isReady to determine whether the image is still being loaded.  Once that returns true, you'll be able to access the properties.
     
    #2
    wrigty12 likes this.
  3. wrigty12

    wrigty12 Veteran Veteran

    Messages:
    533
    Likes Received:
    76
    First Language:
    English
    Ah, alright I will keep that in mind and rework my methods. Thanks!
     
    #3
  4. Victor Sant

    Victor Sant Veteran Veteran

    Messages:
    1,694
    Likes Received:
    1,437
    Location:
    Brazil
    First Language:
    Portuguese
    bitmap = ImageManager.loadBitmap("img/pictures/puzzle/", "Castle")
    bitmap.addLoadListener(youFunction.bind(this, bitmap))

    yourFunction = function(bitmap) {
     // do whatever you need to do after the bitmap is loaded
    }




    if what you need to do is on a update you can use


    if (bitmap.isReady()) {
      // do whatver you need with the bitmap
    }


    Obviously, those are just pseudo codes to give you an idea of what can be done, but everything will depend on the context of the what you're trying to do.
     
    #4
    King Gerar, Jonforum, DreamX and 2 others like this.
  5. wrigty12

    wrigty12 Veteran Veteran

    Messages:
    533
    Likes Received:
    76
    First Language:
    English
    Thanks Victor! I didn't even know an addLoadListenter function existed.


    I think I'm going to load up the bitmaps with the plugin's parameters so I can also edit any image filenames, but Ill definitely keep your setup in mind in case that doesn't end up working for my plugin.
     
    #5

Share This Page