设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 4207|回复: 6
打印 上一主题 下一主题

[原创发布] 历时一个半月,终于写出了一个勉强能用的窗口插件

[复制链接]

Lv1.梦旅人

梦石
0
星屑
50
在线时间
48 小时
注册时间
2010-10-26
帖子
17
跳转到指定楼层
1
发表于 2016-4-22 21:28:15 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
虽然只有600多行代码,但写起来跟要了我命一样
效果展示


评分

参与人数 1星屑 +66 收起 理由
永恒の未知数 + 66

查看全部评分

Lv1.梦旅人

梦石
0
星屑
50
在线时间
48 小时
注册时间
2010-10-26
帖子
17
2
 楼主| 发表于 2016-4-22 21:29:33 | 只看该作者
附上我这坨翔一样的代码,过几天准备再优化一下可读性

JAVASCRIPT 代码复制
  1. /*:
  2.  * @author Created by 邪恶道具 on 2016/3/5.
  3.  *
  4.  * @param 图片数量
  5.  * @default 15
  6.  *
  7.  * @param 选项名
  8.  * @default 日记
  9.  *
  10.  * @param 初始化标签
  11.  * @default 0
  12.  *
  13.  * @param 功能选择
  14.  * @default 0
  15.  *
  16.  * @plugindesc
  17.  * 初始化标签为1时,初始化时显示所有图片,为0时隐藏所有图片
  18.  * 功能选择:1是默认,2和3非别是全部设置隐藏或显示
  19.  *
  20.  * @help
  21.  *
  22.  *
  23.  * 将需要使用的图片放入img文件夹中的diary文件夹内
  24.  * 文件名按照这种格式编辑 "diary (x).png",其中x为一个数字,此数字对应相应标签
  25.  * 因此该数字最好是连续的,如:diary (1).png  ,diary (2).png...
  26.  * Book.png,这种名称的图片为背景图片
  27.  * 日记选项中显示的标签,需要自己新建一个diary_name.rpgsave文件
  28.  * diary_name.rpgsave文件中的内容使用UTF-8字符,用英文逗号分割
  29.  * 该文件需要放入save文件夹中
  30.  *
  31.  *
  32.  */
  33. var params = PluginManager.parameters("diary_new");
  34. (function(){
  35.  
  36.  
  37.     var tabName = String(params['选项名'] || "请输入" );
  38.     //console.log("tagNum:"+tabName);
  39.  
  40.     //通过needsCommand指令添加Menu菜单中的Diary选项
  41.     var _Window_MenuCommand_addMainCommands = Window_MenuCommand.prototype.addMainCommands;
  42.     Window_MenuCommand.prototype.addMainCommands = function(){
  43.         _Window_MenuCommand_addMainCommands.call(this);
  44.         if (this.needsCommand('diary')) {
  45.             //第一个参数是选项的名称,第二个则是对应createCommandWindow里的字符
  46.             this.addCommand(tabName, 'diary', 1);//TextManager.diary的值为'日记'
  47.         }
  48.     };
  49.  
  50.     //将选项添加进去
  51.     var _Scene_Menu_createCommandWindow = Scene_Menu.prototype.createCommandWindow;
  52.     Scene_Menu.prototype.createCommandWindow = function(){
  53.         _Scene_Menu_createCommandWindow.call(this);
  54.         this._commandWindow.setHandler('diary',     diary_command.bind(this));
  55.     };
  56.  
  57.  
  58. })();
  59.  
  60. var diary_command = function(){
  61.     this._commandWindow.deselect();
  62.     run();
  63. };
  64.  
  65. var diary_command_activate = function(){
  66.     SceneManager.goto(Scene_Menu);
  67. };
  68.  
  69. /**
  70.  * Created by 邪恶道具 on 2016/3/5.
  71.  * 日记系统绘制窗口及其功能代码
  72.  * 图片读取是在状态机执行的间隔中进行的
  73.  */
  74.  
  75.  
  76. var functionSelection = Number(params['功能选择'] || "2" );
  77.  
  78.  
  79. var photoNum = Number(params['图片数量'] || 15);//需要载入的图片数量,从1开始,而不是0(也就是表示1-11(11个),而不是往常的0-11(12个))
  80.  
  81. var run = function(){
  82.     if(!document.getElementById("theCanvas")){
  83.         create_canvas();
  84.     }else{
  85.        var theCanvas = document.getElementById("theCanvas");
  86.         theCanvas.style.zIndex = 20;
  87.     }
  88.     fileWriteAndRead( functionSelection,photoNum,null, null);
  89.     diary(photoNum);
  90. };
  91.  
  92. var diary = function(photoNum) {
  93.     var theCanvas = document.getElementById("theCanvas");
  94.     var context = theCanvas.getContext("2d");
  95.  
  96.  
  97.     //状态机参数
  98.     var state_photoLoad = 1;
  99.     var state_initList = 2;
  100.     var state_windowDraw = 3;
  101.     var state_drawFont = 4;
  102.     var state_nextPage = 5;
  103.     var state_backPage = 6;
  104.     var state_end = 7;
  105.     var currentState = state_photoLoad;
  106.  
  107.  
  108.     //参数
  109.     var displayText = new Object();
  110.     displayText.flag = new Array();//为0显示"???",为1显示text属性的内容
  111.     displayText.text = new Array();//日记名称
  112.     displayText.maxLine = 12;//最大行数
  113.     displayText.textCount = 0;
  114.  
  115.  
  116.  
  117.     var photoLoadedCount = 0;//负责已载入图片的计数
  118.     var offset = 2;//边框偏移量
  119.     var line_x = 200;//分割线x坐标
  120.     var currentPage = 1;//记录当前的页数,从1开始
  121.     var rememberNameCount = 0;
  122.  
  123.  
  124.  
  125.     var coordinate = new Object();//该对象负责存储定位选项与显示的必要数据
  126.     coordinate.backGround = null;
  127.     coordinate.photoCache = new Array();//存储日记图片的数组
  128.     coordinate.font_x = new Array();
  129.     coordinate.font_y = new Array();
  130.     coordinate.font_height = new Array();
  131.     coordinate.font_width = new Array();
  132.  
  133.  
  134.     //临时参数
  135.     var global_Count = 0;
  136.     var global_Flag = 0;
  137.     var tempFlag = 0;
  138.  
  139.  
  140.     //coordinate.photoCache,从第0个元素开始是日记图片
  141.     var photoLoad = function () {
  142.         var backGround = new Image();
  143.         backGround.src = "img/diary/book.png";
  144.         backGround.addEventListener("load", load_done, false);
  145.         coordinate.backGround = backGround;
  146.         window.console.log("loading");
  147.  
  148.  
  149.         for (var i = 0; i < photoNum; i++) {
  150.             var number = i + 1;
  151.             var temp = new Image();
  152.             temp.src = "img/diary/diary (" + number + ").png";
  153.             temp.addEventListener("load", load_done, false);
  154.             coordinate.photoCache.push(temp);
  155.         }
  156.         //window.console.log("coordinate.photoCache.length:"+coordinate.photoCache.length);
  157.         currentState = state_end;
  158.     };
  159.  
  160.  
  161.     var initList = function () {
  162.         var temp = loadFile();
  163.         var tempName = string_Change_strArray();
  164.         for (var i = 0; i < coordinate.photoCache.length; i++) {
  165.             displayText.text.push(tempName[i]);
  166.         }
  167.  
  168.         for(var i = 0;i < temp.length;i++){
  169.             if(temp[i]==0||temp[i]==1){
  170.                 displayText.flag.push(temp[i]);
  171.             }
  172.         }
  173.         currentState = state_windowDraw;
  174.     };
  175.  
  176.     //将中文字符串转换成数组
  177.     var string_Change_strArray = function () {
  178.         var fs = require('fs');
  179.         var fileName = "diary_name.rpgsave";
  180.         var savePath = StorageManager.localFileDirectoryPath() + fileName;
  181.         var data = fs.readFileSync(savePath,"utf-8");
  182.         var temp = data.split(",");
  183.         var tempArray = new Array();
  184.         for(var i=0;i<temp.length;i++) {
  185.             if (temp[i] != ",") {
  186.                 tempArray.push(temp[i]);
  187.             }
  188.         }
  189.         return tempArray;
  190.     };
  191.  
  192.     var loadFile = function () {
  193.         var fs = require('fs');
  194.         var fileName = "diary.rpgsave";
  195.         var savePath = StorageManager.localFileDirectoryPath() + fileName;
  196.         var readData = null;
  197.         readData = fs.readFileSync(savePath);
  198.         readData = ""+readData;
  199.         return readData;
  200.     };
  201.  
  202.  
  203.  
  204.     var load_done = function () {
  205.         photoLoadedCount++;
  206.         //coordinate.photoCache.length + 1,背景图加上日记图片的数量
  207.         if (photoLoadedCount == (coordinate.photoCache.length + 1)) {
  208.             coordinate.backGround.removeEventListener("load", load_done, false);
  209.             for (var i = 0; i < coordinate.photoCache.length; i++) {
  210.                 coordinate.photoCache[i].removeEventListener("load", load_done, false);
  211.             }
  212.             currentState = state_initList;
  213.             coordinate.time = setInterval(stateMachine, 33);
  214.             console.log("loaded");
  215.             photoLoadedCount--;//这下才是日记图片数(-1是为了去掉背景的计数)
  216.         }
  217.     };
  218.  
  219.  
  220.     var windowDraw = function () {
  221.         var width = theCanvas.width - offset;
  222.         var height = theCanvas.height - offset;
  223.  
  224.         context.drawImage(coordinate.backGround, 0, 0);
  225.  
  226.  
  227.         //给背景增加透明度
  228.         context.beginPath();
  229.         context.globalAlpha = 0.8;
  230.         context.fillStyle = "#000000";
  231.         context.fillRect(0, 0, theCanvas.width, theCanvas.height);
  232.  
  233.         //绘制边框
  234.         context.beginPath();
  235.         context.lineWidth = 4;
  236.         context.lineJoin = "round";
  237.         context.strokeStyle = "#FFFFFF";
  238.         context.moveTo(offset, offset);
  239.         context.lineTo(width, offset);
  240.         context.lineTo(width, height);
  241.         context.lineTo(offset, height);
  242.         context.closePath();
  243.         context.stroke();
  244.  
  245.  
  246.         //获取显示日记内容区域的信息
  247.         coordinate.diaryDisplayArea_minX = line_x;
  248.         coordinate.diaryDisplayArea_maxX = width;
  249.         coordinate.diaryDisplayArea_minY = offset;
  250.         coordinate.diaryDisplayArea_maxY = height;
  251.  
  252.  
  253.         context.beginPath();
  254.         context.moveTo(line_x, offset);
  255.         context.lineTo(line_x, height);
  256.         context.stroke();
  257.  
  258.  
  259.         currentState = state_drawFont;
  260.     };
  261.  
  262.  
  263.     var drawFont = function () {
  264.         var offset = 20;
  265.         var tempStr = null;
  266.         //var temp = displayText.maxLine + rememberNameCount;
  267.  
  268.         if(photoNum==(displayText.maxLine + rememberNameCount - 2)){
  269.             rememberNameCount--;
  270.             currentPage--;
  271.         }
  272.  
  273.         var length = displayText.maxLine + rememberNameCount;
  274.  
  275.         for (var i = rememberNameCount; i < length; i++) {
  276.             if (displayText.flag[i] == 1) {
  277.                 tempStr = 0;
  278.             }
  279.  
  280.             if(displayText.flag[i] == 0){
  281.                 tempStr = 1;
  282.             }
  283.  
  284.             if ((i + 1) == length) {
  285.                 if(global_Count == coordinate.photoCache.length-1 || (global_Count == coordinate.photoCache.length && tempFlag == 0)){
  286.                     tempStr = 4;
  287.                 }else{
  288.                     tempStr = 2;
  289.                 }
  290.             }
  291.  
  292.             if(currentPage>1&&i==rememberNameCount){
  293.                 tempStr = 3;
  294.             }
  295.  
  296.             switch(tempStr){
  297.                 case 0:
  298.                     tempStr = displayText.text[i];
  299.                     if(global_Flag==0){
  300.                         global_Count++;
  301.                     }
  302.                     break;
  303.                 case 1:
  304.                     tempStr = "???";
  305.                     if(global_Flag==0){
  306.                         global_Count++;
  307.                     }
  308.                     break;
  309.                 case 2:
  310.                     tempStr = "下一个";
  311.                     break;
  312.                 case 3:
  313.                     tempStr = "上一个";
  314.                     break;
  315.                 case 4:
  316.                     if(displayText.flag[i] == 1){
  317.                         tempStr = displayText.text[i];
  318.                         global_Count++;
  319.                         break;
  320.                     }else{
  321.                         tempStr = "???";
  322.                         global_Count++;
  323.                         break;
  324.                     }
  325.                 case null:
  326.                     tempStr = "";
  327.                     window.console.log("无剩余图片");
  328.                     break;
  329.             }
  330.             if(global_Count > coordinate.photoCache.length && tempFlag == 0){
  331.                 global_Count = coordinate.photoCache.length;
  332.             }
  333.  
  334.             var fontMaxWidth = line_x - offset;
  335.             var fontSize = 30;
  336.             var y = offset + fontSize;
  337.             context.beginPath();
  338.             context.font = "normal " + fontSize + "px Arial";
  339.             context.fillStyle = "#FFFFFF";
  340.             var str_width = Math.round(context.measureText(tempStr).width);
  341.             var x = ((line_x - offset) / 2) - Math.floor(str_width / 2) + 10;//使字体居中
  342.             context.fillText(tempStr, x, y * (i - rememberNameCount + 1), fontMaxWidth);
  343.  
  344.  
  345.             coordinate.font_x.push(x);
  346.             coordinate.font_y.push((y * (i- rememberNameCount + 1)) - fontSize);//不减去fontSize,那么y会位于字体下方
  347.             coordinate.font_width.push(str_width);//压入字符串宽度
  348.  
  349.  
  350.             //绘制字体下划线
  351.             context.beginPath();
  352.             context.strokeStyle = "#FFFFFF";
  353.             var fontOffset = 10;
  354.             context.moveTo(x, y * (i- rememberNameCount + 1) + fontOffset);
  355.             context.lineTo(x + str_width, y * (i- rememberNameCount + 1) + fontOffset);
  356.             coordinate.font_height.push(fontSize + fontOffset);//压入字符串高度
  357.             context.stroke();
  358.  
  359.             coordinate.fontOffset = fontOffset;
  360.         }
  361.  
  362.  
  363.  
  364.         currentState = state_end;
  365.     };
  366.  
  367.  
  368.     var game_end = function () {
  369.         clearInterval(coordinate.time);
  370.     };
  371.  
  372.     var checkClick = function (event) {
  373.         if(event.button == 2 || exitKey(event)){
  374.             exitDiary(theCanvas);
  375.         }else{
  376.             var x = Math.round(event.pageX);
  377.             var y = Math.round(event.pageY);
  378.             //console.log("x:" + x);
  379.             //console.log("y:" + y);
  380.                 for (var i = 0; i < coordinate.photoCache.length; i++) {
  381.                     if (displayText.flag[i] == 1 || displayText.flag[i] == 0) {
  382.                         //minBoundary和maxBoundary分别为选项的左右边界
  383.                         var minX = coordinate.font_x[i];
  384.                         var minY = coordinate.font_y[i];
  385.                         var maxX = minX + Math.round(coordinate.font_width[i]);
  386.                         var maxY = minY + Math.round(coordinate.font_height[i]);
  387.                         var minBoundary = (x > minX && y > minY);
  388.                         var maxBoundary = (x < maxX && y < maxY);
  389.                         if (minBoundary && maxBoundary) {
  390.                             theCanvas.width = theCanvas.width;
  391.                             theCanvas.height = theCanvas.height;
  392.  
  393.                             global_Flag = 1;
  394.                             windowDraw();
  395.                             drawFont();
  396.                             currentState = null;
  397.  
  398.                             //获取设置图片位置的必要信息
  399.                             if(displayText.flag[i+rememberNameCount] == 1){
  400.                                 var centext_width = coordinate.diaryDisplayArea_maxX - coordinate.diaryDisplayArea_minX;
  401.                                 var centext_height = coordinate.diaryDisplayArea_maxY - coordinate.diaryDisplayArea_minY;
  402.                                 var centerPoint_x = (centext_width / 2 + coordinate.diaryDisplayArea_minX);
  403.                                 var centerPoint_y = (centext_height / 2 + coordinate.diaryDisplayArea_minY);
  404.  
  405.  
  406.                                 var photo_width = coordinate.photoCache[i+rememberNameCount].width / 2;
  407.                                 var photo_height = coordinate.photoCache[i+rememberNameCount].height / 2;
  408.  
  409.                                 var place_x = centerPoint_x - photo_width;
  410.                                 var place_y = centerPoint_y - photo_height;
  411.                             }
  412.                             if ((i + 1) == displayText.maxLine && global_Count < coordinate.photoCache.length) {
  413.                                 nextPage();
  414.                                 break;
  415.                             }
  416.                             if(i==0&&currentPage>1){
  417.                                 backPage();
  418.                                 break;
  419.                             }
  420.                             //日记内容显示部分
  421.                             if(!(((i + 1) == displayText.maxLine)&&(i==0&&currentPage>1))) {
  422.                                 if(displayText.flag[i+rememberNameCount] == 1){
  423.                                     context.drawImage(coordinate.photoCache[i+rememberNameCount], place_x, place_y);
  424.                                     break;
  425.                                 }
  426.                             }
  427.                         }
  428.                     }
  429.                 }
  430.             }
  431.     };
  432.     //diary窗口的退出函数
  433.     var exitDiary = function(theCanvas){
  434.         theCanvas.removeEventListener("mouseup", checkClick,false);
  435.         document.removeEventListener("keyup", checkClick,false);
  436.         theCanvas.style.zIndex = -20;
  437.         diary_command_activate();
  438.         theCanvas.width = theCanvas.width;
  439.         theCanvas.height = theCanvas.height;
  440.         window.console.log("退出完成");
  441.     };
  442.  
  443.     var exitKey = function(event){
  444.         switch (event.keyCode){
  445.             case 27:
  446.                 return true;
  447.                 break;
  448. /*            case 93:
  449.                 return true;
  450.                 break;*/
  451.         }
  452.     };
  453.  
  454.     var nextPage = function () {
  455.         if(global_Count < photoNum){
  456.             tempFlag = 0;
  457.         }
  458.         global_Count++;
  459.         currentPage++;
  460.         rememberNameCount++;
  461.         theCanvas.width = theCanvas.width;
  462.         theCanvas.height = theCanvas.height;
  463.  
  464.         windowDraw();
  465.         drawFont();
  466.         currentState = null;
  467.     };
  468.  
  469.     var backPage = function () {
  470.         if(global_Count == photoNum && tempFlag == 0){
  471.             global_Count--;
  472.         }
  473.         tempFlag = 1;
  474.         global_Count--;
  475.         currentPage--;
  476.         rememberNameCount--;
  477.         theCanvas.width = theCanvas.width;
  478.         theCanvas.height = theCanvas.height;
  479.         windowDraw();
  480.         drawFont();
  481.         currentState = null;
  482.     };
  483.  
  484.  
  485.     var stateMachine = function () {
  486.         switch (currentState) {
  487.             case state_photoLoad:
  488.                 photoLoad();
  489.                 break;
  490.             case state_initList:
  491.                 initList();
  492.                 break;
  493.             case state_windowDraw:
  494.                 windowDraw();
  495.                 break;
  496.             case state_drawFont:
  497.                 drawFont();
  498.                 break;
  499.             case state_nextPage:
  500.                 nextPage();
  501.                 break;
  502.             case state_backPage:
  503.                 backPage();
  504.                 break;
  505.             case state_end:
  506.                 game_end();
  507.                 break;
  508.             case null:
  509.                 //window.console.log("错误的状态值");
  510.                 break;
  511.         }
  512.     };
  513.  
  514.  
  515.     document.addEventListener('keyup', checkClick,false);
  516.     theCanvas.addEventListener("mouseup", checkClick, false);
  517.     coordinate.time = setInterval(stateMachine, 33);
  518.  
  519.  
  520. };
  521.  
  522. var create_canvas = function(){
  523.     var theCanvas = document.createElement("canvas");
  524.     if(!theCanvas){
  525.         document.write("no support canvas");
  526.         return ;
  527.     }
  528.     //mv的是816*624
  529.     theCanvas.width = 816;
  530.     theCanvas.height = 624;
  531.     theCanvas.style.position = "absolute";
  532.     theCanvas.style.margin = "auto";
  533.     theCanvas.style.top = 0 + "px";
  534.     theCanvas.style.left = 0 + "px";
  535.     theCanvas.style.right = 0 + "px";
  536.     theCanvas.style.bottom = 0 + "px";
  537.     theCanvas.style.zIndex = 20;
  538.     theCanvas.id = "theCanvas";
  539.     document.body.appendChild(theCanvas);
  540.     return theCanvas;
  541.  
  542. };
  543.  
  544.  
  545. /**
  546.  * Created by 邪恶道具 on 16-4-19.
  547.  * 负责文件的读取与存储,使用node.js
  548.  */
  549.  
  550.  
  551.  
  552.  
  553. var fileDisplay = Number(params['初始化标签'] || "0" );
  554.  
  555.  
  556. var _Game_Interpreter_pluginCommand =
  557.     Game_Interpreter.prototype.pluginCommand;
  558. Game_Interpreter.prototype.pluginCommand = function(command, args) {
  559.     _Game_Interpreter_pluginCommand.call(this, command, args);
  560.     console.log("functionSelection:"+functionSelection);
  561.     fileWriteAndRead( functionSelection,photoNum,command, args);
  562. };
  563.  
  564.  
  565. //第1个和3个参数决定运行那个函数,第2个参数表示有多少图片,第4个参数是插件变量
  566. //第1个参数是手动指定
  567. fileWriteAndRead = function(select,photoNum,command, args) {
  568.     var fs = require('fs');
  569.     var fileName = "diary.rpgsave";
  570.     var savePath = StorageManager.localFileDirectoryPath() + fileName;
  571.     var length = photoNum;//为图片数量
  572.     //intiData只在初始化时使用
  573.     var initData = null;
  574.     var readData = null;
  575.  
  576.     console.log("select2:"+select);
  577.  
  578.  
  579.     var initFile = function () {
  580.         initData = new Array(length);
  581.         for (var i = 0; i < length; i++) {
  582.             initData[i] = fileDisplay;
  583.         }
  584.         saveFile(initData);
  585.     };
  586.  
  587.     var saveFile = function (data) {
  588.         fs.writeFileSync(savePath, data,"utf-8");
  589.     };
  590.  
  591.     var loadFile = function () {
  592.         readData = fs.readFileSync(savePath,"utf-8");
  593.         readData = string_Change_array(readData);
  594.         return readData;
  595.     };
  596.  
  597.     //index为要修改的位置,flag的值为0和1
  598.     var changeFlag = function () {
  599.         readData = loadFile();
  600.         var count = 0;
  601.         for(var i=0;i<readData.length;i++){
  602.             if (count == args[0]) {
  603.                 readData[count] = args[1];
  604.                 break;
  605.             }
  606.             if((i % 2) == 0){
  607.                 count++;
  608.             }
  609.         }
  610.         saveFile(readData);
  611.     };
  612.  
  613.     //将数字字符串转换成数组
  614.     var string_Change_array = function (data) {
  615.         var temp = data.split("");
  616.         var tempArray = new Array();
  617.         for(var i=0;i<temp.length;i++){
  618.             if(temp[i]==1||temp[i]==0){
  619.                 tempArray.push(temp[i]);
  620.             }
  621.         }
  622.         return tempArray;
  623.     };
  624.  
  625.     //将中文字符串转换成数组
  626.     var string_Change_strArray = function () {
  627.         var fs = require('fs');
  628.         var fileName = "diary_name.rpgsave";
  629.         var savePath = StorageManager.localFileDirectoryPath() + fileName;
  630.         var data = fs.readFileSync(savePath,"utf-8");
  631.         var temp = data.split(",");
  632.         var tempArray = new Array();
  633.         for(var i=0;i<temp.length;i++) {
  634.             if (temp[i] != ",") {
  635.                 tempArray.push(temp[i]);
  636.             }
  637.         }
  638.         return tempArray;
  639.     };
  640.  
  641.     var exist = function () {
  642.         //为真表示文件已存在
  643.         if (!fs.existsSync(savePath)) {
  644.             initFile();
  645.         } else {
  646.             loadFile();
  647.         }
  648.         switch (command) {
  649.             case "changeFlag":
  650.                 changeFlag();
  651.                 break;
  652.             case "string_Change_strArray":
  653.                 string_Change_strArray();
  654.                 break;
  655.             case null:
  656.                 //console.log("指令为空");
  657.                 break;
  658.         }
  659.     };
  660.  
  661.     var reInitFile = function(flag){
  662.         console.log("进入reInitFile");
  663.         var temp = null;
  664.         if(flag == 2){
  665.             temp = 0;
  666.         }else{
  667.             temp = 1;
  668.         }
  669.         readData = loadFile();
  670.         for(var i=0;i<readData.length;i++){
  671.             if (readData[i] != ",") {
  672.                 readData[i] = temp;
  673.             }
  674.         }
  675.         console.log("reInitFile_readData:"+readData);
  676.         saveFile(readData);
  677.     };
  678.  
  679.     var run = function(select) {
  680.         var temp = 2;
  681.         switch (select) {
  682.             case 1:
  683.                 exist();
  684.                 break;
  685.             case 2:
  686.                 reInitFile(temp);
  687.                 break;
  688.             case 3:
  689.                 reInitFile(temp+1);
  690.                 break;
  691.         }
  692.     };
  693.     run(select);
  694. };


评分

参与人数 2星屑 +38 收起 理由
king + 18 塞糖 这个插件好,需要
夜狠简单 + 20 塞糖

查看全部评分

回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
491 小时
注册时间
2015-1-7
帖子
124
3
发表于 2016-4-22 22:47:32 | 只看该作者
之前在贴吧看见一个类似的教程,但他没有显示图片什么的,现在居然有完善的了,感谢
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
48 小时
注册时间
2010-10-26
帖子
17
4
 楼主| 发表于 2016-4-25 16:37:48 | 只看该作者
UML图和优化了语义后的脚本
[attachimg]307450[/attach diary_new.7z (21.73 KB, 下载次数: 95) img]

QQ截图20160425163725.png (50.04 KB, 下载次数: 14)

QQ截图20160425163725.png
回复 支持 1 反对 0

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
48 小时
注册时间
2010-10-26
帖子
17
5
 楼主| 发表于 2016-5-10 16:02:05 | 只看该作者
ps:这教程很久前就写好了,有人建议应该写的更通熟易懂,但水平有限修改过后也只有这样了

该教程的特点是对mv源码的依赖小,适合有js编程能力但对mv源码研究不深的人

理解这些技术后,就可以不受到mv本身的约束来写窗口和它所要展示的内容


1.首先,将选项添加进主菜单
    var _Window_MenuCommand_addMainCommands = Window_MenuCommand.prototype.addMainCommands;
    Window_MenuCommand.prototype.addMainCommands = function(){
        _Window_MenuCommand_addMainCommands.call(this);
        if (this.needsCommand('diary')) {
            this.addCommand(tagName, 'diary', 1);
        }
    };

上面这几段代码,用于将你窗口的选项添加进主菜单中,其中tagName为该选项所显示的字符串
'diary'则是一个指令名,该指令名可以自行设置,但一定要是字符串
第三个选项用途不明,但填1并没有问题,所以写你自己的窗口时填1即可


2.然后给你的选项添加功能
    var _Scene_Menu_createCommandWindow = Scene_Menu.prototype.createCommandWindow;
    Scene_Menu.prototype.createCommandWindow = function(){
        _Scene_Menu_createCommandWindow.call(this);
        this._commandWindow.setHandler('diary',     diary_command.bind(this));
    };

this._commandWindow.setHandler('diary',     diary_command.bind(this));
这段代码中的第一个参数是变量名,第二个参数用于给你添加的选项附加上逻辑功能
bind关键字与call类似,都是改变函数或变量的this指向,但不同的是,call是改变后立即执行,bind则是改变this的指向,但不立即执行
setHandler源码如下:
Window_Selectable.prototype.setHandler = function(symbol, method) {
    this._handlers[symbol] = method;
};


关于上面代码使用call的意义可以参见我以前的帖子
http://tieba.baidu.com/p/4185225 ... p;cid=0#79770073662

3.其他函数介绍

3.1 窗口跳转函数
var diary_command_activate = function(){
    SceneManager.goto(Scene_Menu);
};

该函数在退出日记窗口时执行,用于切换回主菜单,根据给goto输入不同的参数,可以跳转到其他菜单

3,2 窗口逻辑函数
var diary_command = function(){
    this._commandWindow.deselect();
    diary_run();
};
this._commandWindow.deselect();用于冻结主菜单,使其不接受鼠标与键盘操作
diary_run();则包含了你编写的窗口的所有代码

现在关键的几个函数都介绍完毕了,实际上通过这几个函数已经可以开始编写窗口了

现在来介绍我写的插件中的几项关键技术:状态机,资源预载和本地文件读写

先来说说状态机

    //状态机参数
    var state_photoLoad = 1;
    var state_initList = 2;
    var state_windowDraw = 3;
    var state_drawFont = 4;
    var state_nextPage = 5;
    var state_backPage = 6;
    var state_end = 7;
    var currentState = state_photoLoad;
首先设置一组状态变量,然后设置一个记录状态变量的变量,该变量记录下一个应该执行的函数的代码
设置一个状态机,然后设置一个计时器,我设置的是每33毫秒查询一次状态
在下述每个函数执行完时,状态变量currentState都会发生相应变化
这就是状态机了,简单而且能更有效的控制代码的执行,相当简单的技术

    //计时器
    coordinate.time = setInterval(stateMachine, 33);

    //状态机
    var stateMachine = function () {
        switch (currentState) {
            case state_photoLoad:
                photoLoad();
                break;
            case state_initList:
                initList();
                break;
            case state_windowDraw:
                windowDraw();
                break;
            case state_drawFont:
                drawFont();
                break;
            case state_nextPage:
                nextPage();
                break;
            case state_backPage:
                backPage();
                break;
            case state_end:
                game_end();
                break;
            case null:
                //window.console.log("错误的状态值");
                break;
        }
    };


资源预载:
在我的源码是photoLoad和load_done这两个函数在负责这一功能
原理非常简单

        var backGround = new Image();
        backGround.src = "img/diary/book.png";
        backGround.addEventListener("load", load_done, false);
        coordinate.backGround = backGround;
        window.console.log("loading");
在这段代码中,设置好正确的资源路径,然后加上一个监听函数来监听资源是否被载入

如果确认载入,则执行load_done函数来移除监听并计数,资源载入是在代码执行的空隙时进行的,所以photoLoad的末尾有这么一句代码currentState = state_end
用于停止轮询,等待载入
当load_done中的计数等于需要被载入的资源数量时,就表示资源被全部载入,此时再次启动轮询并指定新的状态
载入的资源此时位于内存中,所以会增加内存负担
此技术也是相当简单的


本地文件读写可以去看node.js的file system那一部分,说的比我好,具体实现可以参考我的代码
node存储的文件是一个字符串文件,所以要转换为数组时,需要自己写个转换函数,也可以参考我的源码
回复 支持 反对

使用道具 举报

Lv3.寻梦者

梦石
0
星屑
1232
在线时间
1017 小时
注册时间
2011-4-30
帖子
1516
6
发表于 2016-5-10 21:34:13 | 只看该作者
支持下.........
好想推荐自己的图书馆系统啊....

点评

ㄏㄏ  发表于 2016-5-14 14:44
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
16 小时
注册时间
2012-8-13
帖子
17
7
发表于 2016-10-24 20:12:53 | 只看该作者
支持下 虽然我载入了却不懂用。。。按esc打开日记直接出错了。。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-5-16 23:58

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表