MZ [Solved] TypeError - Failed to execute 'getImageData' on 'CanvasRenderingContext2D': Value is not of type 'long'.

Hello,

I have a little problem with a script-call. It's quite long, so I should probably think about making a plugin out of it, but I'd like to have the base finished/successful testing first.

I have a while-loop to move a ball one pixel at a time and another while-loop in it that checks every pixel around the ball, if there is a collision.

The script short and simplified:

JavaScript:
const COLLISION_Picture_ID = 1;
const sprite = SceneManager._scene._spriteset._pictureContainer.children[COLLISION_Picture_ID - 1];
const ax = sprite.anchor.x * sprite.width;
const ay = sprite.anchor.y * sprite.height;

while (!$gameSwitches.value(BallCanMove)) {
    //move ball 1 pixel to x & y
    while (!$gameSwitches.value(CollisionChecked)) {
        //get a pixel and check for collision
        if (sprite) {
            const pt = sprite.worldTransform.applyInverse(new Point(BallX, BallY));
            const alpha = sprite.bitmap.getAlphaPixel(pt.x + ax, pt.y + ay);
            if (alpha > 150) {
                //do something
                $gameSwitches.setValue(CollisionChecked, true);
            };
        };
        //do '$gameSwitches.setValue(CollisionChecked, true);' at some point
    };
    //do '$gameSwitches.setValue(BallCanMove, true);' at some point
};

It's working fine the first 1-5 seconds but then I get a crash with this error message:

TypeError - Failed to execute 'getImageData' on 'CanvasRenderingContext2D': Value is not of type 'long'.

Console-Log:
1660675647897.png


I've commented out a few lines to be sure and it's always crashing after some time at the setting of the variable "alpha".
So the issue is ' const alpha = sprite.bitmap.getAlphaPixel(pt.x + ax, pt.y + ay); ' ?

Does anyone have an idea why and a solution / work-around or idea what I can do?
Okay I fixed it. ^^ I just added some ' Math.round() ' and also instead of storing the alpha value in the variable "alpha", I query the value directly, additionally beforehand whether "sprite" = true.

No idea what it was exactly, but it works:

JavaScript:
const COLLISION_Picture_ID = 1;
const sprite = SceneManager._scene._spriteset._pictureContainer.children[COLLISION_Picture_ID - 1];
const ax = sprite.anchor.x * sprite.width;
const ay = sprite.anchor.y * sprite.height;

while (!$gameSwitches.value(BallCanMove)) {
    //move ball 1 pixel to x & y
    while (!$gameSwitches.value(CollisionChecked)) {
        //get a pixel and check for collision
        if (sprite && Math.round(BallX) >= 0 && Math.round(BallX) <= 620 && Math.round(BallY) >= 0 && Math.round(BallY) <= 1008) {
            const pt = sprite.worldTransform.applyInverse(new Point(Math.round(BallX), Math.round(BallY)));
            if (sprite && sprite.bitmap.getAlphaPixel(Math.round(pt.x + ax), Math.round(pt.y + ay) > 150) {
                //do something
                $gameSwitches.setValue(CollisionChecked, true);
            };
        };
        //do '$gameSwitches.setValue(CollisionChecked, true);' at some point
    };
    //do '$gameSwitches.setValue(BallCanMove, true);' at some point
};

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-typeerror-failed-to-execute-getimagedata-on-canvasrenderingcontext2d-value-is-not-of-type-long.150502/