Question

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. 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. Can anyone provide and assistance? I am working in the Quartz Composer environment but still within the javascript patch.

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;
    }   
}
Was it helpful?

Solution

I don't know the Quartz Composer, but I do see what's wrong with your code. Each time this event fires, you indeed set all the values in the array back to 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; at the beginning of the execution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top