Inline function parameter syntax

● ARCHIVED · READ-ONLY
Started by Trilaterus 7 posts View original ↗
  1. Not sure if I'm using the correct terminology here but I can't seem to figure out how to add parameters to an inline function.
    Code:
    attackBitmap.addLoadListener((function(startX, startY)
    {
    screenSizedBitmap.blt(
    attackBitmap,
    0,
    0,
    attackBitmap.width,
    attackBitmap.height,
    startX,
    startY);
    })(this._attackX, this._attackY));

    For example if I do it this way I get the error: Uncaught TypeError: listener is not a function

    This makes me think that the position of my parameters is causing it to think I'm trying to add parameters to the wrong function.

    Variants and their errors:
    Removed the inline function's surrounding braces, I was looking at this: https://www.w3schools.com/js/tryit.asp?filename=tryjs_function_expression_self and thought I needed to make it self invoking(?)
    Code:
    attackBitmap.addLoadListener(function(startX, startY)
    {
    screenSizedBitmap.blt(
    attackBitmap,
    0,
    0,
    attackBitmap.width,
    attackBitmap.height,
    startX,
    startY);
    })(this._attackX, this._attackY);
    Gives the error: attackBitmap.addLoadListener(..) is not a function


    Added the parameters straight after the function

    Code:
    attackBitmap.addLoadListener(function(startX, startY)
    {
    screenSizedBitmap.blt(
    attackBitmap,
    0,
    0,
    attackBitmap.width,
    attackBitmap.height,
    startX,
    startY);
    }(this._attackX, this._attackY));
    Gives the error: Uncaught TypeError: listener is not a function
  2. the correct syntax to add a load listener to a bitmap is:
    Code:
    this._main_image.bitmap.addLoadListener(function(bitmap){
        // do stuff here:..
    });

    But from the look of what your trying to do, thats the least of your problems :D
    You cannot change the arguments passed to the function that is called when your adding a load listener, you would have to edit the bitmap class to pass your new parameters/arguments when calling the load listener function.

    edit: try this:
    Code:
    attackBitmap.addLoadListener(b => {
        screenSizedBitmap.blt(b,0,0,b.width,b.height,this._attackX,this._attackY);
    });
  3. I am calling addLoadListener from a bitmap so I'm unsure what's wrong there. I'll post the full function so there's no misunderstanding of scope:
    Code:
    HP_Attack.prototype.initialize = function(attackBitmapPath)
    {
    // Coordinates for bitmap space
    this._attackX = 0;
    this._attackY = 0;
    
    // Setup attack bitmap by copying the attack bitmap into a blank bitmap the size of the screen
    var screenSizedBitmap = new Bitmap(HP_BITMAP_WIDTH(), HP_BITMAP_HEIGHT());
    var attackBitmap = ImageManager.loadHPBitmap(attackBitmapPath);
    attackBitmap.addLoadListener(function(startX, startY)
    {
    screenSizedBitmap.blt(
    attackBitmap,
    0,
    0,
    attackBitmap.width,
    attackBitmap.height,
    startX,
    startY);
    }(this._attackX, this._attackY));
    
    this._attackBitmap = attackBitmap;
    this._sprite = new Sprite(screenSizedBitmap);
    this._sprite.scale.x = HP_SCREEN_BITMAP_SCALE;
    this._sprite.scale.y = HP_SCREEN_BITMAP_SCALE;
    }
    Annoyingly HP_SCREEN_BITMAP_SCALE is declared outside of this function so ignore that for now >.>

    Code:
    this._main_image.bitmap.addLoadListener(function(bitmap){
       // do stuff here:..
    });
    This is interesting though, wasn't sure that you needed to pass in the bitmap back into the function, does this mean that my load listener was never really working? Wait a minute I'm using variables outside of the addLoadListener like attackBitmap... How was this working at all? :S
  4. it would seem you are the one misunderstanding the scope :p

    TBH, the formatting of your code probably doesnt help. its easier to envision scope when things are ordered properly.

    You do not 'need' to pass the bitmap back into the function. you simply dont have a choice. the Bitmap class is designed so that when it calls the functions that have been added with the 'addLoadListener' function, it passes itself as THE ONLY argument to the function. so all bitmap load listener functions have 1 argument by default - the bitmap that was loaded.

    due to how scoping works, your attackBitmap is defined outwith the scope of the function you are passing to add load listener, which means it has access to all the variables defined in its 'parent scope'

    This is a much neater implimentation fo your code. But i would recommend you study more on how javascript works in general, because I can spot a number of things 'wrong' with how your defining things and using them.
    Code:
    HP_Attack.prototype.initialize = function(attackBitmapPath){
        this._attackX = 0;
        this._attackY = 0;
    
        var screenSizedBitmap = new Bitmap(HP_BITMAP_WIDTH(), HP_BITMAP_HEIGHT());
        this._attackBitmap = ImageManager.loadHPBitmap(attackBitmapPath);
    
        let startX = this._attackX;
        let startY = this._attackY;
        this._attackBitmap.addLoadListener(function(bitmap_that_was_loaded){
            let w = bitmap_that_was_loaded.width;
            let h = bitmap_that_was_loaded.height;
            screenSizedBitmap.blt(bitmap_that_was_loaded,0,0,w,h,startX,startY);
        });
        this._sprite = new Sprite(screenSizedBitmap);
        this._sprite.scale.x = HP_SCREEN_BITMAP_SCALE;
        this._sprite.scale.y = HP_SCREEN_BITMAP_SCALE;
    };
  5. I've moved this thread to Javascript Support. Thank you.

  6. it passes itself as THE ONLY argument to the function
    I wouldn't have guessed that from: https://kinoar.github.io/rmmv-doc-web/classes/bitmap.html#addloadlistener which looks like it just takes in a function? Oh but I guess the function that it takes in, that has to have the parameter of the bitmap... I don't think I'd have been able to figure that out looking at the documentation and the code :S

    You're right though there's more pressing mistakes to fix. I had no idea of the keyword 'let' but I've read up on it on W3 schools and that is super useful. I'm not sure why you left screenSizedBitmap defined as var whereas the others use let, couldn't we just use let for everything in this case? Also I can't think of any cases where you'd need to use var over let.

    If there are other pressing issues here I'd rather know earlier than later so if I'm copying the bitmap into sprite incorrectly or basically using/defining things wrong then, if you have the time, could you let me know the problems?

    Other than that I understand your fix and it's working, thanks for bearing with me!

    Also mb for posting in the wrong forum :o
    And sorry for not formatting my code properly :p
  7. yes, the let keyword would work fine in this use case. :)

    nothing else really stands out as being a concern, most things is just my personal preference for how the code should be formatted and such. :)