Javascript Increment Index on event

Swiftlikeninja's picture

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?

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;
   }   
}

franz's picture
Re: Javascript Increment Index on event

why can't you just use a counter patch instead of javascript ?

cybero's picture
Re: Javascript Increment Index on event

Counter Patch would probably be faster. [Counter patch versus Javascript]

gtoledo3's picture
Re: Javascript Increment Index on event

There is some JavaScript code for setting up a counter buried in the qtz that sets up the templates for QC. That said, I agree with the other comments.

Swiftlikeninja's picture
Re: Javascript Increment Index on event

I was able to perform this with just a standard counter patch but what I am intending to do is perform a Hit test on a scalable grid of buttons without the use of any iterators. I have been able to hit test with the javascript but have no way to count number of hits without pulling out of the javascript. Speed-wise I havent seen any indication of lag from the javascript that would cause any hinderance in the composition performance. I will check out the buried qtz, but my failing is at counting within an array instead of just a one off.

franz's picture
Re: Javascript Increment Index on event

an alternative would be to use _1024_Structure_Tools, which has a hit test patch just for this.

Swiftlikeninja's picture
Re: Javascript Increment Index on event

Thank you franz, that plug in works great but still appears to only provides a temporary boolean. What I'm trying to figure out is a way to increment individual indexes based upon hit test. This will keep a running tally of how many times each button has been hit. I feel as though the coding is almost there but is just falling short. I've attached a sample comp showing the current state. I know I can drop a counter within the rendering iterator to handle the individual button counts but i was to try to keep as much code out of the iterator as possible as well as to help my understanding of javascript.

PreviewAttachmentSize
JSHitTest.qtz35.99 KB

gtoledo3's picture
Re: Javascript Increment Index on event

My "hit map and score" demo basically does this:

[/quote]

http://kineme.net/composition/gtoledo3/RectHit#comment-15392

That said, I'd use that approach with the stock (private) Hit Test patch, which is excellent and bugs have been fixed with since I published that comp. It even hit tests on the Z-axis. Also, instead of spookying out of the macro, you could make sure there are no renderers, and publish out.

jersmi's picture
Re: Javascript Increment Index on event

Here's the javascript counter I think George is talking about. I use it a lot. Kinda like a counter + range patch. I modified it so it wraps.

var i = 0;
var result = new Object();
function (__number index) main (__boolean up, __boolean down, __index min, __index max)
{
    if(up)
        ++i;
    if(down)
        --i;
    if(i>max-1)
        i=min;
    if(i<min)
        i=max-1;
 
    result.index = i;
    return result;
}

gtoledo3's picture
Re: Javascript Increment Index on event

Yep, that's it...and the max/min is taken out it will just go on ad infinitum. The range is practical though. :)