知识点如下
1.新建一个Js类 继承 RPGMZ游戏引擎自带的 Window_Base 类
2.让窗口在游戏内显示出来
3.输出 HelloWorld 标准文本
正文
// 自定义窗口 名字为_New_Window
function _New_Window() {
this.initialize(...arguments);
};
_New_Window.prototype = Object.create(Window_Base.prototype);
_New_Window.prototype.constructor = _New_Window;
// 初始化窗口
_New_Window.prototype.initialize = function(x, y, width, height) {
Window_Base.prototype.initialize.call(this, x, y, width, height);
};
//画出内容
_New_Window.prototype.refresh = function(item_data){
this.contents.clear();
let _data_zero = "Hello World";
this.drawText(_data_zero , 0, 0, this.contents.measureTextWidth(_data_zero), "center");
};
_New_Window.prototype.update = function(){
this.refresh();
};
RPGMZ代码写起来比较繁琐
总之就是新建一个类 继承Window_Base 然后用位图 画出文本 Hello World
一、类结构与继承
function _New_Window() {
this.initialize(...arguments);
};
_New_Window.prototype = Object.create(Window_Base.prototype);
_New_Window.prototype.constructor = _New_Window;
继承关系:_New_Window
继承自 Window_Base
,因此拥有基类的所有功能(如窗口初始化、文本绘制、动画效果等)。
二、初始化方法
构造函数:通过 initialize
方法初始化实例,参数会传递给基类的初始化逻辑。
_New_Window.prototype.initialize = function(x, y, width, height) {
Window_Base.prototype.initialize.call(this, x, y, width, height);
};
初始化方法
功能:调用父类 Window_Base
的 initialize
方法,创建一个位于 (x, y)
坐标、宽高为 width
和 height
的窗口。
三、内容绘制方法
功能:清空窗口内容并绘制新内容。
_New_Window.prototype.refresh = function() {
this.contents.clear();
let _data_zero = "Hello World";
this.drawText(_data_zero, 0, 0, this.contents.measureTextWidth(_data_zero), "center");
};
四、自动刷新窗口内容
_New_Window.prototype.update = function(){
this.refresh();
};
update
方法会在游戏主循环的每一帧被自动调用(RPG Maker MZ 引擎的标准机制)。
五、重写Scene_Map 初始化事件
_Scene_Map_prototype_createAllWindows = Scene_Map.prototype.createAllWindows;
Scene_Map.prototype.createAllWindows = function() {
_Scene_Map_prototype_createAllWindows.call(this);
this.addChild(new _New_Window(new Rectangle(50,50,200,100)));
};
this.addChild(new _New_Window(new Rectangle(50,50,200,100))); 是核心代码 新建一个类 并且用 addChild 添加进去
注意:new Rectangle(50,50,200,100) 是窗口的坐标和宽度高度 新一个Rectangle矩形
结尾总结
请Project1论坛的小圈子 离开