Reputation: 21
I am trying to call a function inside the Canvas that is created when exporting a Flash CC 2014.1 project.
//JS Code (inside Flash)
function sayHello()
{
alert('hello');
}
//JS Code in my HTML
var canvas, stage, exportRoot;
function init() {
canvas = document.getElementById("canvas");
exportRoot = new lib.CreateJS();
stage = new createjs.Stage(canvas);
stage.addChild(exportRoot);
stage.update();
stage.enableMouseOver();
createjs.Ticker.setFPS(lib.properties.fps);
createjs.Ticker.addEventListener("tick", stage);
sayHello()
}
//I have tried:
stage.sayHello();
canvas.stage.sayHello();
//Any ideas?
Upvotes: 2
Views: 1519
Reputation: 11294
When you add code into the timeline in Flash, it is contextual to the symbol it is defined in. In your case, this is not the stage, but rather the exportRoot
instance, which is essentially the root MovieClip exported from Flash. Take a look at the JavaScript that gets exported, and you can see how the code is put together.
Further, you have defined an anonymous function on frame 1, so the function is only accessible on the frame 1 script, can not be called externally. You can get around this by defining the function as a property of the frame using this
.
this.sayHello = function() {
console.log("Hello");
}
This defines the sayHello
method as a variable on the current symbol (which in your case is probably exportRoot
. It can then be called from anywhere using:
exportRoot.sayHello()
I hope that makes sense. Let me know if you have any other questions, or need further information about how this all works.
Cheers.
Upvotes: 3