Hello, I've just started using RPG Maker MZ.I'm currently developing a fairly simple plugin to implement the necessary features for my game.
The plugin's content is as follows: creating message bubbles and playing animations
However I got a major issue. There are no problems with creating message bubbles and playing animations, but the error occurs when the menu screen is opened while the animation is playing :"Uncaught TypeError: Cannot read property 'position' of null"
I believe this is likely because the Sprite created in the current Scene loses its target when the Scene transitions to the Menu.
To be honest, I'm a complete beginner in coding(I think you can easily see that in the codes), and I have no idea about the exact reason or even a possible solution for this error. I would greatly appreciate your help on this matter.
Also, if possible, explaining to me why this happen would really glad too.
Thank you!
The plugin's content is as follows: creating message bubbles and playing animations
JavaScript:
/*:
* @target MZ
* @plugindesc Displays a message with background image and sound effect on the top right of the screen.
* @help
*
* @param BackgroundImage
* @text Background Image
* @desc The background image filename (placed in img/pictures)
* @default BackgroundImage
* @type file
* @dir img/pictures
*
* @param BackgroundImageBig
* @text Background Image Big
* @desc The background image filename (placed in img/pictures)
* @default BackgroundImage
* @type file
* @dir img/pictures
*
* @param SoundEffect
* @text Sound Effect
* @desc The sound effect filename (placed in audio/se)
* @default SoundEffect
* @type file
* @dir audio/se
*
* @param AnimationSpeed
* @text Animation Speed
* @desc The speed of the animation when messages move up/down (higher is faster)
* @type number
* @min 1
* @default 10
*
* @param OffsetX
* @text Offset X
* @desc The horizontal offset from the right edge of the screen
* @type number
* @min 0
* @default 10
*
* @param OffsetY
* @text Offset Y
* @desc The vertical offset from the top edge of the screen
* @type number
* @min 0
* @default 10
*
* @command ShowPopupMessage
* @text Show Popup Message
* @desc Displays a message with background image and sound effect
*
* @arg message
* @text Message
* @desc The message to display
* @type string
* @default You got a potion!
*
* @arg duration
* @text Duration
* @desc The duration in seconds for the message to be displayed
* @type number
* @min 1
* @default 15
*
* @command ShowPopupMessageBig
* @text Show Popup Message Big
* @desc Displays a message with background image and sound effect
*
* @arg message
* @text Message
* @desc The message to display
* @type string
* @default You got a potion!
*
* @arg duration
* @text Duration
* @desc The duration in seconds for the message to be displayed
* @type number
* @min 1
* @default 15
*
* @command Init
* @text Init
* @desc Init
*
*/
(() => {
const pluginName = 'PM_Notification';
const parameters = PluginManager.parameters(pluginName);
const backgroundImage = parameters['BackgroundImage'];
const backgroundImageBig = parameters['BackgroundImageBig'];
const soundEffect = parameters['SoundEffect'];
const offsetX = Number(parameters['OffsetX'] || 10);
const offsetY = Number(parameters['OffsetY'] || 10);
const animationSpeed = Number(parameters['AnimationSpeed'] || 10);
let popupMessages = [];
let zIndexCounter = 1000;
PluginManager.registerCommand(pluginName, 'ShowPopupMessage', args => {
const message = String(args.message);
const duration = Number(args.duration);
const scene = SceneManager._scene;
const lastMessage = popupMessages.length > 0 ? popupMessages[popupMessages.length - 1] : null;
let yPos = offsetY;
if (lastMessage) {
yPos = lastMessage.y + lastMessage.height + offsetY;
}
scene.showPopupMessage(backgroundImage, message, soundEffect, duration, offsetX, yPos);
});
PluginManager.registerCommand(pluginName, 'ShowPopupMessageBig', args => {
const message = String(args.message);
const duration = Number(args.duration);
const scene = SceneManager._scene;
const lastMessage = popupMessages.length > 0 ? popupMessages[popupMessages.length - 1] : null;
let yPos = offsetY;
if (lastMessage) {
yPos = lastMessage.y + lastMessage.height + offsetY;
}
scene.showPopupMessage(backgroundImageBig, message, soundEffect, duration, offsetX, yPos);
});
PluginManager.registerCommand(pluginName, 'Init', args => {
const scene = SceneManager._scene;
// Show dummy popup message
scene.showPopupMessage(backgroundImage, "Init", null, 5, 0, 0);
scene.showPopupMessage(backgroundImageBig, "Init", null, 5, 0, 0);
// Immediately remove the sprite if it exists
if (scene.popupMessages) {
if (scene.popupMessages.length > 0) {
const sprite = scene.popupMessages[scene.popupMessages.length - 1];
if (sprite) {
sprite.parent.removeChild(sprite);
scene.popupMessages.pop(); // Remove from array as well
}
}
}
});
Scene_Base.prototype.showPopupMessage = function(imageName, message, soundName, duration, offsetX, offsetY) {
let init = 0;
if (message == "Init") {
init = 1;
}
const bitmap = ImageManager.loadPicture(imageName);
const sprite = new Sprite(bitmap);
sprite.anchor.x = 0.5;
sprite.anchor.y = 0.5;
// Calculate initial position
sprite.x = Graphics.width - (sprite.width/2) - offsetX;
sprite.y = offsetY - (sprite.height/2);
sprite.opacity = 0;
sprite.z = zIndexCounter++;
const textSprite = new Sprite(new Bitmap(bitmap.width, bitmap.height));
textSprite.bitmap.drawText(message, 0, 0, bitmap.width, bitmap.height, 'center');
textSprite.anchor.x = 0.5;
textSprite.anchor.y = 0.5;
sprite.addChild(textSprite);
this.addChild(sprite);
if (soundName != null){
AudioManager.playSe({ name: soundName, volume: 90, pitch: 100, pan: 0 });
}
// Fade-in animation
const fadeInDuration = 12; // 0.2 seconds at 60 fps
const fadeOutDuration = 12; // 0.2 seconds at 60 fps
const displayDuration = duration * 60; // Convert duration from seconds to frames
let frame = 0;
if (init == 1){
frame = displayDuration-1;
}
const update = () => {
sprite.x = Graphics.width - (sprite.width/2) - offsetX;
textSprite.x = 0;
textSprite.y = 0;
const weight = Math.max((fadeInDuration * 20) - frame * 2, 0) * 0.00025;
sprite.scale.x = 1 + Math.sin((frame / (fadeInDuration)) * 2) * weight;
sprite.scale.y = 1 + Math.cos((frame / (fadeInDuration)) * 2) * weight;
if (sprite) {
if (frame < fadeInDuration) {
sprite.opacity = (frame / fadeInDuration) * 255;
} else if (frame >= displayDuration - fadeOutDuration) {
sprite.opacity = ((displayDuration - frame) / fadeOutDuration) * 255;
} else {
sprite.opacity = 255;
}
}
if (frame >= displayDuration) {
if (sprite) {
sprite.parent.removeChild(sprite);
popupMessages = popupMessages.filter(item => item !== sprite);
}
} else {
frame++;
requestAnimationFrame(update);
}
this.adjustPopupMessagesPosition();
};
update();
popupMessages.push(sprite);
this.adjustPopupMessagesPosition();
};
Scene_Base.prototype.adjustPopupMessagesPosition = function() {
let currentY = offsetY;
for (let i = 0; i < popupMessages.length; i++) {
const sprite = popupMessages[i];
if (sprite) {
const targetY = sprite.height + i * (sprite.height + (sprite.height/4));
sprite.y = sprite.y + Math.max(Math.abs(targetY - sprite.y) / 1000, 0) * (targetY - sprite.y);
}
}
};
})();
However I got a major issue. There are no problems with creating message bubbles and playing animations, but the error occurs when the menu screen is opened while the animation is playing :"Uncaught TypeError: Cannot read property 'position' of null"
I believe this is likely because the Sprite created in the current Scene loses its target when the Scene transitions to the Menu.
To be honest, I'm a complete beginner in coding(I think you can easily see that in the codes), and I have no idea about the exact reason or even a possible solution for this error. I would greatly appreciate your help on this matter.
Also, if possible, explaining to me why this happen would really glad too.
Thank you!
