简体   繁体   English

事件的Javascript增量索引

[英]Javascript increment index on event

I am trying to figure out a way to increment an index of an array based on an event (such as left mouse click over a hit area). 我试图找出一种基于事件(例如,在点击区域上单击鼠标左键)来增加数组索引的方法。 The code below sets everything to zero and briefly changes the appropriate index to 1 while the mouse is clicked but changes back to 0 once the mouse is released. 下面的代码将所有内容都设置为零,并在单击鼠标时将相应的索引短暂地更改为1,但是一旦释放鼠标,便将其更改回0。 What I want to happen is each time the index value is incremented then it stores its current value instead of switching to 0. By the end the array should be mixed numbers. 我想发生的是每次索引值递增时,它就会存储其当前值,而不是切换到0。最后,数组应该是混合数字。 Can anyone provide and assistance? 谁能提供和帮助? I am working in the Quartz Composer environment but still within the javascript patch. 我正在Quartz Composer环境中工作,但仍在javascript补丁中。

function (__structure out) main (__structure Pos, __boolean Left, __number X, 
          __number Y, __number W, __number H, __number ShiftX, __number ShiftY) {

    if (!_testMode) {
        len = Pos.length;
        Hits = new Array()
            for (i = 0; i < len; i++) {
                Hits[i] = 0 
            }
            for (j = 0; j < len; j++) {
                if (Pos[j][1] >= (X-(W/2)) && Pos[j][1] <= (X +(W/2)) && 
                    Pos[j][0] >= (Y-(H/2)) && Pos[j][0] <= (Y +(H/2)) && Left) { 
                Hits[j]++
                }
            }

    result = new Object();
    result.out = Hits;
    return result;
    }   
}

I don't know the Quartz Composer, but I do see what's wrong with your code. 我不知道Quartz Composer,但是我确实看到您的代码出了什么问题。 Each time this event fires, you indeed set all the values in the array back to 0. 每次触发此事件时,您实际上都将数组中的所有值都设置回0。

Therefore, you should take the following lines of code out of your method and into the more global scope (in the current class or in the real global scope). 因此,应将以下代码行从方法中移出,并移入更全局的范围(在当前类或实际的全局范围中)。

Hits = new Array()
for (i=0;i<len;i++){
    Hits[i] = 0
}

So Hits should be a non-local array and the initialization of each Hits[i] to 0 should be done only once; 因此, Hits应该是一个非本地数组,并且每个Hits[i]初始化为0只能执行一次; at the beginning of the execution. 在执行开始时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM