Problem with Game_Interpreter

I have some problem with my script, umh i want to make a plugin command to switch off and on the image on the map. Here is my plugin:

Spoiler
var param = PluginManager.parameters("KAX_RealisticRain.js");
var RainGraphic = String(param['RainGraphic'] || "rainc6");
var RainSpeed = Number(param['RainSpeed'] || 3);
//=======================================================================
var alias_GI_plugincommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
alias_GI_plugincommand.call(this, command, args);
if (command === 'startRain') {
switch (args[0]) {
case '1':
RainSpeed = 1;
this.image.origin.y -= RainSpeed;
break;
case '2':
RainSpeed = 2;
this.image.origin.y -= RainSpeed;
break;
case '3':
RainSpeed = 3;
this.createRain();
break;
case '4':
RainSpeed = 4;
this.image.origin.y -= RainSpeed;
break;
case '5':
RainSpeed = 5;
this.image.origin.y -= RainSpeed;
break;
case '0':
SceneManager.push(Scene_Map);
break;
};
};
};
//===========================================================
function Window_Rain() {
this.initialize.apply(this, arguments);
}

Window_Rain.prototype = Object.create(Window_Base.prototype);
Window_Rain.prototype.constructor = Window_Rain;

Window_Rain.prototype.initialize = function(x, y) {
var width = this.windowWidth();
var height = this.windowHeight();
Window_Base.prototype.initialize.call(this, x, y, width, height);
this.refresh();
};

Window_Rain.prototype.windowWidth = function() {
return 816;
};

Window_Rain.prototype.windowHeight = function() {
return 624
};

Window_Rain.prototype.refresh = function() {
var x = this.textPadding();
var width = this.contents.width - this.textPadding() * 2;
this.contents.clear();
};

Window_Rain.prototype.createRain = function() {
this.image = new TilingSprite();
this.image.bitmap = ImageManager.loadPicture(RainGraphic);
this.image._onBitmapLoad();
this.image.move(0, 0, Graphics.width, Graphics.height);
this.image.opacity = 70;
this.addChild(this.image);
this.updateRain();
};

Window_Rain.prototype.updateRain = function() {
this.image.origin.y -= 3;
};

Window_Rain.prototype.update = function() {
Window_Base.prototype.update.call(this);

};

Window_Rain.prototype.open = function() {
this.refresh();
Window_Base.prototype.open.call(this);
};
//====================================================================
var ailias_SC_start = Scene_Map.prototype.start;
Scene_Map.prototype.start = function() {
ailias_SC_start.call(this);
this.windowrain = new Window_Rain(0, 0);
this.windowrain.opacity = 0;
this.addChild(this.windowrain);
};
//=====================================================================


I run my code with Plugin Command is startRain 3 and it gets errors below: 


[IMG]http://i.imgur.com/n648JOg.png[/IMG]


I have just begin with creating MV plugin, i really don't know where i was wrong or maybe, i missed something. Sorry for bad english (i am an asian), i will be very appreciate if anyone can help me with my plugin, thanks in advance.
line 21 is "this.createRain()", however at that point 'this' refers to a Game_Interpreter object, not a Window_Rain object. There are several ways you could get around that, but the easiest, or at least first that I can think of, would be something like:


Game_Interpreter.prototype.createRain = function() {
SceneManager._scene.windowrain.createRain();
};




However, looking at your code, I can see several problems.

  1. You redefine Window_Rain.update but all it does is call the update function of Window_Base. If you don't add anything else to the function, you don't need to do this. Since Window_Rain inherits from Window_Base, it already has that function and it already does that.
  2. Your plugin command check includes a line to push another map scene on the stack. Why? What is the purpose of this?
  3. You create the window in the start function of Scene_Map, but it would be better placed in create. You should also load in the rain image during this stage because images do not instantly load, they can take time. In many cases it'll be less than a second to load, but it's a noticeable lag.
  4. Your plugin is not encapsulated (wrapped in, surrounded by) an anonymous function. This means that your variables param, RainGraphic, RainSpeed, and all your aliases are all global variables that anyone can access and change. Please look into how anonymous functions work, and what they're used for.



This may seem discouraging, but it's all to help you learn. The best way to learn how to do something, especially programming, to try and fail at it. You learn why what you did didn't work so that you're able to get around such issues in the future. It can take a long time to understand a lot of things, but putting in the effort is the best way to learn how to do it. Don't lose hope, just keep on trying. Use Google to try to find answers to some of your questions if you can't find or get answers on the forums, the internet is an enormous resource for helping developers out.
I can't agree more that my code is such a mess. Thank you very much for helping me with my plugin and giving me some advices, i will try it.  

Original thread: https://forums.rpgmakerweb.com/threads/problem-with-game_interpreter.67459/