我的推送阵列"功能不起作用

时间:2015-05-14 00:24:47

标签: javascript arrays

我正在研究一个最终的幻想ATB战斗系统,我正在尝试实施一个"急速功能"。匆忙将让你在3秒内行动,而不是等待6秒才能采取行动。我基本上设置了一个 user 对象,它具有一个空数组的 status 属性。当执行加速按钮时,我"推送"这个词"急速"进入阵列。然后我发表一个声明,说如果我有"急速状态",那么我将让我的计时器在3秒内超过6秒。我遇到的问题是,如果我还有其他任何其他问题而不是"仓促"进入阵列,这种效果打破了。例如,如果我按下快速按钮3次,这将推动"急速"单词进入数组3次,这不是问题,因为我可以在以后删除它。我遇到的问题是,如果除了一个人之外还有其他任何事情,那就是#34;在数组中,这种效果不起作用,这不是我想要的,因为将来我会用它来推动其他状态效果。我怎么能告诉我的代码,即使其他数组项在我的数组中,仍然可以执行效果?

function hasteButton() {
    atbReset()
    user.status.push("haste");
    console.log(user.status);
};

var user = {
    status: []
};

function timeBar(el, color) {
    var elem = document.getElementById(el);
    if (user.status == "haste") {
        elem.style.transition = "width 3.0s, linear 0s";
        window.setTimeout(function () {
            boxLight()
        }, 2800);
    } else {
        elem.style.transition = "width 6.0s, linear 0s";
        window.setTimeout(function () {
            boxLight()
        }, 5800);
    }
}

3 个答案:

答案 0 :(得分:3)

您的代码中存在错误:您使用数组但是将其作为字符串检查;变化

if (user.status == "haste") {

if (user.status.indexOf("haste") != -1) {

答案 1 :(得分:1)

听起来你只想设置一个haste属性,而不是在数组中推送一个项目。这样,无论数组中的其他内容如何,​​您都可以直接检查属性,并且加速设置将独立于数组中的其他内容。数组用于有序集合,但haste状态似乎只是布尔状态,无论数组中的内容如何,​​因此它似乎不属于数组。

你可以像这样使用setting作为状态数组的属性:

function hasteButton() {
    atbReset()
    user.status.haste = true;
    console.log(user.status);
};

var user = {
    status: []
};

function timeBar(el, color) {
    var elem = document.getElementById(el);
    if (user.status.haste) {
        elem.style.transition = "width 3.0s, linear 0s";
        window.setTimeout(function () {
            boxLight()
        }, 2800);
    } else {
        elem.style.transition = "width 6.0s, linear 0s";
        window.setTimeout(function () {
            boxLight()
        }, 5800);
    }
}

答案 2 :(得分:0)

在您的代码中,您有:calculate。此调用将返回一个数组,您需要以某种方式访问​​它的元素。

我建议做类似的事情:

$scope.calculate = function calculate (x) {
    $scope.mF = device1[x];
}

如果要求元素在数组中找到isn&t; t的索引,则数组上的user.status方法返回-1。

相关问题