- Joined
- Jul 22, 2014
- Messages
- 5,634
- Reaction score
- 5,115
- First Language
- English
- Primarily Uses
- RMVXA
I'm starting to get very involved with MZ plugin making now and while I have been able to create some nifty functionality, I think I'm lacking on the fundamentals, especially when it comes to sprites and how to handle them. In particular, I am worried that I am missing steps along the way and that, in the long run, I will be tanking the technical performance of people who use my plugins.
And I would really appreciate any guidance on whether my approach will lead to any memory leaks or other significant technical issues.
Here's a simple scene I made last month, mostly just as a learning experience. The scene simply shows a background, allows the player to create a new faerie sprite onscreen with each left-click, and allows the player to remove all faerie sprites with a right-click. It was partially based off of the Game Over scene code, and it uses Scene_Base as its "parent class".
In particular, a few of the things that I'm doing:
And I would really appreciate any guidance on whether my approach will lead to any memory leaks or other significant technical issues.
Here's a simple scene I made last month, mostly just as a learning experience. The scene simply shows a background, allows the player to create a new faerie sprite onscreen with each left-click, and allows the player to remove all faerie sprites with a right-click. It was partially based off of the Game Over scene code, and it uses Scene_Base as its "parent class".
Code:
function Scene_Zero() {
this.initialize(...arguments);
}
Scene_Zero.prototype = Object.create(Scene_Base.prototype);
Scene_Zero.prototype.constructor = Scene_Zero;
Scene_Zero.prototype.initialize = function() {
Scene_Base.prototype.initialize.call(this);
};
Scene_Zero.prototype.create = function() {
Scene_Base.prototype.create.call(this);
this.playZeroMusic();
this.createBackground();
this.sprites = [];
};
Scene_Zero.prototype.update = function() {
if (this.isActive() && !this.isBusy()) {
if (Input.isTriggered("ok") || TouchInput.isTriggered()) {
SoundManager.playUseSkill();
var faesprite = new Sprite();
faesprite.bitmap = ImageManager.loadEnemy("Sylph");
this.addChild(faesprite);
this.sprites.push(faesprite);
faesprite._hue = Number(Math.random() * 360);
faesprite.x = Number(100 + Math.random() * 600);
faesprite.y = Number(100 + Math.random() * 400);
faesprite._updateColorFilter();
} else if (Input.isTriggered("cancel") || TouchInput.isCancelled()) {
for (fs of this.sprites) {
// REMOVE CHILD
this.removeChild(fs);
// DESTROY SPRITE
fs.destroy();
}
this.sprites = [];
}
}
Scene_Base.prototype.update.call(this);
};
Scene_Zero.prototype.terminate = function() {
Scene_Base.prototype.terminate.call(this);
AudioManager.stopAll();
};
Scene_Zero.prototype.playZeroMusic = function() {
AudioManager.stopBgm();
AudioManager.stopBgs();
};
Scene_Zero.prototype.createBackground = function() {
this._backSprite = new Sprite();
this._backSprite.bitmap = ImageManager.loadTitle1("Mountain");
this.addChild(this._backSprite);
};
- To create both the background and the faeries, I create a new sprite, then assign them a bitmap property using the ImageManager.loadwhatever method, then add the new sprite to the scene as its Child - is this a wise way to do it?
- When it's time to delete all the faerie sprites (due to a right-click), I iterate through the array of sprites, remove each as a Child from the scene, and then run the sprite's destroy() method - will this process free up all of the memory that the sprite was consuming, or do I need to do something else?
- I haven't added an actual way to exit the Scene yet, but assume that the terminate() function will run when the player leaves the scene - will the terminate() function automatically dispose of the this._backSprite sprite and free up its memory, or do I need to explicitly destroy it or do something else? How about existing faerie sprites that weren't cleared via right-click - will those be automatically disposed when the scene is terminated?

