Abe Smith
-
01:22:36 pm on July 11, 2008 | # |
I’ve hacked a pong flash game to be controlled using two potentiometers going into the analogue inputs on the Arduino board. The set up was just a test in combining different technologies but one use for it is that it could allow for multiple responsive controllers on internet flash applications and games.
Here’s a Video.
The main challenge of getting this to work was translating the jittery signal from the Arduino into a smooth and fluid movement in flash.
I separated the values from the different controllers by adding 257 to the second input in Arduino. Then once id identified the different feeds in flash I gave the second value a different name and subtracted the 257 back off again.
I achieved the smooth and fluid movement by using action script to store the incoming signals into 2 arrays and creating an average of each array which id use as the value to control the behavior of objects in flash.
Here’s a section of the AS2 code I wrote that did this (for just one of the values)
// fills array for value1
function loop1() {
// need to store data values e.g. store[arrayPointer] = data
dataArray[arrayPointer] = _root.mc_square._xscale;
arrayPointer = (arrayPointer+1);// adds 1 to arrayPointer each loop
//trace(arrayPointer);
_root.createAvg();
_root.divideAvg();
// if arrayPointer is a number the same as or greater than the length of the array
if (arrayPointer>=dataArray.length) {
arrayPointer = 0;// resets arrayPointer back to 0
}
}
// gets average value from array and makes the var avg equal to it.
function createAvg() {
for (var i:Number = 0; i<dataArray.length; i++) {
//trace(dataArray[i]);
avg = avg+(dataArray[i]);
}
}
// divides the average by the length of the array and outputs it
function divideAvg() {
avg = Math.round(avg/dataArray.length);
_root.mc_square2._xscale = (avg);
// make paddles = new averaged values
_root.p._y = (avg);
}
Another main problem I found was flash didn’t seem to treat the incoming xml socket data as an integer so to get it as a value I could use in my equation I linked the scale of an invisible movie clip on stage to the value of data and then used this movie clips properties in my calculations.