Monday, August 4, 2008

Quartz, my patch and some Javascript.

Well as i said before, Ive finished my patch and have some handy bits to share.

First off is the Object type.
While trawling around trying to find a way to store values across frames i came across the Object type.
I haven't done much JavaScript before (just Java) and am not sure if this is just a quartz thing or not, but if you define an attribute of the Object class the attribute will be held like a normal variable instead of being calculated every frame (very helpful for storing values).
done by saying

if (Object.someVariable == undefined)
Object.someVariable = 0; //initializes the value to zero when composition is first started

you can then use this variable in your script.

here is an example using this. This script is for when my shake pad is shaken, it adds 1 to an object and sends true to one of the outputs when it reaches a certain value.
the output is then used to activate an animation.
inputs[1] is used as a reset switch.

if(Object.out == undefined || inputs[1])
Object.out = 0;
if(Object.count == undefined || inputs[1])
Object.count = 0;
if(Object.lastVal == undefined || inputs[1])
Object.lastVal = inputs[0];


else if(Object.lastVal != inputs[0])
{
Object.out = inputs[0]/2;
Object.count++;
Object.lastVal = inputs[0];
}

outputs[0] = Object.out;

if(Object.count < 70)
outputs[1] = false;
else
outputs[1] = true;


this script holds a click, ie output will be false until you press and release a button. it will then constantly output true (even after you release) until you press again. Sort of a toggle i suppose.

if(Object.released == undefined)
Object.released = true;
if(Object.out == undefined)
Object.out = false;

if(inputs[0] && Object.released)
{
Object.out = !Object.out;
Object.released = false;
}
else if(!inputs[0] )
Object.released = true;

outputs[0] = Object.out;

yer, thats all i have that other people might find helpful, but if scripts need writing I'm happy to help.

No comments: