ページ 11

【解決済み】制御文字が入った場所のx,yの取得方法

Posted: 2017年9月10日(日) 17:07
by ビービー
現在、制御文字でfillRectを使い文字を四角で囲みたいなーと思い挑戦中なのですが
制御文字が入った場所の特定ができず困っています。

とりあえず最初の行だけでもできればいいかなーと0,0にしてみたものが以下です。

コード: 全て選択

var _Window_Base_prototype_processEscapeCharacter = Window_Base.prototype.processEscapeCharacter;
Window_Base.prototype.processEscapeCharacter = function(code, textState) {
    _Window_Base_prototype_processEscapeCharacter.call(this, code, textState);
    switch (code) {
    case 'B':
        this.processBlackFillRect(this.obtainEscapeParam(textState), textState);
        break;
    }
};

Window_Base.prototype.processBlackFillRect = function(textState) {
    var Width = this.contents.fontSize / 2 * textState + 4;
    this.contents.paintOpacity = 64;
    this.contents.fillRect(0, 0, Width, this.contents.fontSize + 4, this.textColor(15));
    this.contents.paintOpacity = 255;
};
\B[8]と入力すると半角で8文字分の四角形が描画されるというものになります。

アイコンと同じでtextState.x + 2, textState.y + 2が使えると思っていたんですが使えず…
どうすれば制御文字が入力された位置を取得できるでしょうか?
せめて現在の行数だけでもわかると助かります。よろしくお願いします。

Re: 制御文字が入った場所のx,yの取得方法

Posted: 2017年9月11日(月) 02:17
by トリアコンタン
こんばんは!
ご推察の通り、textStateから値を取得できます。
ご提示のコードでは「processBlackFillRect」の引数の数が呼び出し元と、呼び出し先とで一致していません。
呼び出し元ではちゃんとtextStateを引数として渡しているので、あとはメソッド「processBlackFillRect」に2番目の引数を追加すればOKです。

おそらく単純な取り違えかと思います。JavaScriptはこういう場合も特にエラーにはならないのが難しいところですね。

以下が実装例になります。

コード: 全て選択

    var _Window_Base_prototype_processEscapeCharacter = Window_Base.prototype.processEscapeCharacter;
    Window_Base.prototype.processEscapeCharacter = function(code, textState) {
        _Window_Base_prototype_processEscapeCharacter.call(this, code, textState);
        switch (code) {
            case 'B':
                this.processBlackFillRect(this.obtainEscapeParam(textState), textState);
                break;
        }
    };

    Window_Base.prototype.processBlackFillRect = function(charNumber, textState) {
        var Width = this.contents.fontSize / 2 * charNumber + 4;
        this.contents.paintOpacity = 64;
        this.contents.fillRect(textState.x + 2, textState.y + 2, Width, this.contents.fontSize + 4, this.textColor(15));
        this.contents.paintOpacity = 255;
    };

Re: 制御文字が入った場所のx,yの取得方法

Posted: 2017年9月11日(月) 13:07
by ビービー
こんにちは!

いつもありがとうございます!
教えていただいたコードに書き換えたところうまく動作してくれました!
本当にありがとうございました!
image_20170911_130049.jpg