HOME ➔ SUPPORT ➔ Community ➔ General CourseLab issues ... Getting/Setting variable in imported Flash/SWF-File
Getting/Setting variable in imported Flash/SWF-File
  View type:
Is there a way to get or set a variable in an imported Flash-File?
I'm making a memory game. I want to import it into CL. If user solves the puzzle it should be scored.

A Happy New Year 2010,
In Flash, use 'flash.external.ExternalInterface' to call javascript functions and to set callback functions.
See thread 'Accessing a score through javascript' for info how to access a CourseLab variable from javascript.

At loading time you can set Flashvars parameters. Example:
- open properties of the imported swf
- Parameter: FlashVars
- Value: mp3Name=mymusic.mp3
Thank you barend.

But I can't get that to work. What I want to do, is the following:

I'm importing an swf-File with an Ok-Button into a CL-Slide.
After import, I'm running the Slide/Module. Now, if I click the button in the swf:
- The content of variable 'xyz' in the swf is set from 0 to 1.
- Content of xyz (equals 1 now) should be put in a CL-variable #xyz.

So I could set the completion status, add it to the score, move to the next slide and so on.

If this would succeed vice versa, so that I could set a CL-Variable in Flash, this would make up many interesting possibilities for me and others who use Flash.

The example in the thread 'Accessing a score through javascript' respectively 'random question test' shows me, that there is an internal CL-Function called OpenSlideByID. For I'm no good javascript-programmer I see no possibility to store a Flash/Javascript-Variable in a CL-Variable.

Perhaps I've overseen CL-Issues or I'm totally wrong, so you can put me in the right direction, please.

Oops ... I copy/pasted pbhanneys name and after publishing I saw I copied his emailaddress.
Sorry pb ... I'll ask Slav to be so kind to change it.

Anyway, it won't lead to more spam because this emailaddress is already visible for spiders.[;)]
Hi Egon,

A javascript function you define within a CL action cannot be found by any other item. I think it has to do with the javascript being not in the html but in xml and then written by javascript into the file (or something like that). See below.

However, you might be able to use an Flash.externalInterface.call to change the CL variable. It is stored in an array 'g_arVar' inside CL. If I remember well, it is an associative array, i.e. the index is the name of the variable.

Please have a look at thread 'random question test' where pbhanney changes the content of a CL variable in javascript.

Chances are you'll have to add a few lines to courselab.js and add a simple function like
function changeVar(myvar)
g_arVars["xyz"] = myvar;

If you know how to use Flash' Actionscript 3 (2?) you'll be able to define these kind of simple functions in Javascript.

An older way of accessing javascript (I didn't us it for ages) is something like
getURL("javascript:g_arVars['xyz'] = myvar;");

Succes! [:)]
It works! Thank you, barend! [:p]
I put your suggestion
function changeVar(myvar)
g_arVars["xyz"] = myvar;
into courselab.js.

Then in Flash (AS3) I tested it. The swf contains a textfield and a button:
import flash.external.ExternalInterface;
var okButton:ok = new ok(); /*button-Object in flash-library*/
var fp:int; /*name of variable in CL*/
okButton.x = 120; /*buttons position*/
okButton.y = 100;
addChild(okButton); /*draws button on Flash canvas*/
/*listen for mousebutton up, if so do function clickOk*/
okButton.addEventListener(MouseEvent.MOUSE_UP, clickOK);

function clickOK(event:MouseEvent) {
} /*call the javascript function in courselab.js and set fp's value to 1*/
I published the swf and imported it in a CL-Slide.
For testing-purpose I defined a SLIDE-variable: Action VARIABLE(name='fp',value='0',Scope='Entire Module'

Then I put a button on the slide and added an Action to this CL-button:

Action onclick : MSGBOX(Text='#fp').

After that I run the slide (SHIFT F5). Pressing the CL-Button shows me 0 in the messagebox. I set this in the slide variable. That's the initial value of fp.
Then I press the swf's ok-Button and fp gets value 1 from the swf. Now I press the CL-button again and the messagebox shows me 1. [:)]
You're welcome [:)]

Nick, is this something for

Message options
No additional options