JavaScript如何获取值并将其放入函数中

时间:2018-08-24 08:20:04

标签: javascript function this

您可以在此Link

中查看我的所有代码

并且有一个带有github页面Link

的演示

现在,我在开始游戏时就做到了,我可以看到游戏环境。但是我想让它选择我想要的角色。

因此,我使用VEX Libaray来使用询问什么Character的对话框。您可以查看我的屏幕截图。 enter image description here

因此,当我单击每个按钮时,它可以像这样打印。

enter image description here

在那之后,我唯一需要做的就是给this.sprite赋一个值。但我不知道如何将其放入播放器。

enter image description here

和核心代码在这里。

// To choose Character with VEX Library
function click1() {
    vex.dialog.open({
        message: 'Which character do you want to play?',
        buttons: [
            $.extend({}, vex.dialog.buttons.NO, {
                className: 'vex-dialog-button-primary-horizontal',
                text: 'Char-Boy',
                click: function (e) {
                    this.value = 'char-boy';
                    this.close();
                }
            }),
            $.extend({}, vex.dialog.buttons.NO, {
                className: 'vex-dialog-button-primary-horizontal',
                text: 'Char-Cat-Girl',
                click: function (e) {
                    this.value = 'char-cat-girl';
                    this.close();
                }
            }),
            $.extend({}, vex.dialog.buttons.NO, {
                className: 'vex-dialog-button-primary-horizontal',
                text: 'Char-Horn-Girl',
                click: function (e) {
                    this.value = 'char-horn-girl';
                    this.close();
                }
            }),
            $.extend({}, vex.dialog.buttons.NO, {
                className: 'vex-dialog-button-primary-horizontal',
                text: 'Char-Pink-Girl',
                click: function (e) {
                    this.value = 'char-pink-girl';
                    this.close();
                }
            }),
            $.extend({}, vex.dialog.buttons.NO, {
                className: 'vex-dialog-button-primary-horizontal',
                text: 'Char-Princess-Girl',
                click: function (e) {
                    this.value = 'char-princess-girl';
                    this.close();
                }
            })
        ],
        callback: function (value) {
            // char-boy','char-cat-girl','char-horn-girl','char-pink-girl','char-princess-girl
            if (value === 'char-boy') {
                console.log('You choose Char-boy');
                return value;
            } else if (value === 'char-cat-girl') {
                console.log('You choose Char-Cat-Girl');
                return value;
            } else if (value === 'char-horn-girl') {
                console.log('you choose Char-Horn-Girl');
                return value;
            } else if (value === 'char-pink-girl') {
                console.log('You Choose Char-Pink-Girl');
                return value;
            } else if (value === 'char-princess-girl') {
                console.log('You Choose Char-Princess-Girl');
                return value;
            } else {
                console.log('Choose Nothing');
                return value;
            }
        }
    })
}

let Player = function (x, y, speed, value) {
    this.x = x;
    this.y = y;
    this.speed = speed;
    this.sprite = 'images/' + value + '.png';
};

1 个答案:

答案 0 :(得分:0)

我猜您稍后在初始化游戏时会创建一个新的Player实例,以便您可以使用Player的原型来设置值,例如:

let Player = function (x, y, speed) {
   this.x = x;
   this.y = y;
   this.speed = speed;
   this.sprite = 'images/' + this.value + '.png';
};
Player.prototype.value = 'char-boy'; // this should go inside vex callback
console.log(new Player(50,50,100).sprite); // this should print "images/char-boy.png"

附加有关原型的文档的参考:https://www.w3schools.com/js/js_object_prototypes.asp