PDA

View Full Version : Javascript communicating with flash?



thePIN
10-18-2007, 10:34 PM
hi guys

basically i need to know how to get the position of the y-coordinate and how far its travelled down the height of a flash file..
I'm aiming to produce something like this..

here (http://www.bannerblog.com.au/banners/coke_scroll.html)
it wont work in Firefox by the way.. I'm not sure why..

but if at all possible could some one just set me on the right path of what I should be coding and where/how I should be directing it..
maybe some code to help me too?

thanks :)

thePIN
10-19-2007, 04:37 PM
does no1 have any ideas guys?!

Trinithis
10-19-2007, 05:06 PM
I'm pretty sure you can write "flash code" in your flash program if that would solve your problem.

thePIN
10-19-2007, 07:09 PM
yea...there is that part to it as well, but after having broken down the example I linked to, it says it uses javascript to get the measurements of the page and then feeds theses into flash so it knows which frame to play based on the javascript.. the problem is I'm not a very good (at all) javascripter, and I don't really understand all this kind of thing, so what I'm really hoping for is some terms to search for to set me on my way to learning what i need for this project.. the possibility of code, i was hoping would have been as an example so i knew what was doing what...

here (http://www.weh8design.com/flashbanner/files) is some of the files I've taken from the original I'm trying to re create, the file named 'javascript.js' is the one with lots of javascript which I don't understand in much detail...

sorry, I know this is kinda long, but its for a uni project, and the tutors were a little vague.. so just some kinda path to set off on?

thePIN
10-22-2007, 03:22 PM
anyone?

patmanbofh
10-22-2007, 04:46 PM
Hi there, possibly this may be useful.....the example re coke can is a flash file consisting of 13 frames. Each frame shows the can in an increasingly crushed state. There is a javascript function within the html called tratascroll (this particular example is spanish!!) whenever the user scrolls down the html page the function is called:
There is an predefined array consisting of 13 positions
the variable t is calculated by subtracting the offsettop for the coke object from the body scrolltop.
The conditional statement is checking to see if t is between any of the array values. ie if it is >= 70 and <=80 (n=1) ie has the user scrolled enough to initiate another frame being played. The case statement is setting up a parameter which is passed back to the flash file "som1" to "som4" case 1 and "som4" to "som6" for case 2.
The frame variable is then incremented by 1 for each execution of the for loop.
The important statements where you are concerned are the
document.all['sky'].setVariable('idSom', idSom); This is passing a value to the flash variable idSom. (The actual sound played for each frame)
and
document.all['sky'].FrameNum = intFrame;
This is setting the actual frame to be played.
I haven't fully deconstructed the html/javascript code but hopefully this is a start. Havent had time to look at your sample code yet, but if I get the chance I'll get back to you!!
Regards!

This is a copy relevant code of the javascript from the sample page
var intFrame = 0;
var arrPos = new Array(0, 75, 80, 95, 119, 167, 231, 284, 320, 359, 406, 455, 489); // Posicoes da lata

function trataScroll(e) {
var t = parseInt((document.body.scrollTop - document.all['bannerCoke'].offsetTop));

for (n in arrPos) { // Verificamos cada posicao
if (t >= arrPos[n] && t < arrPos[parseInt(n) + 1] && n > intFrame - 1) {
intAtual = parseInt(n) + 1;
intOffset = intAtual - intFrame;

// Verificamos o som a ser tocado
switch(intOffset) {
case 1:
idSom = 'som' + parseInt((Math.random() * 3) + 1);
break;
case 2:
idSom = 'som' + parseInt((Math.random() * 2) + 4);
break;
default:
idSom = 'som6';
break;
}

// Define som
document.all['sky'].setVariable('idSom', idSom);

intFrame = intAtual;
document.all['sky'].FrameNum = intFrame;
}
}
}

// Controle de Scroll
window.onscroll = trataScroll;