如何在两个动画 Canvas 元素之间进行通信?

How to Communicate between Two Animated Canvas Elements?

我想在两个动画 canvas 元素之间进行交流。

我用 Adob​​e Animate CC 制作了两个 html5 canvas js 动画。我已将这两个元素放在一个 html 页面中。我可以从这些动画中成功调用函数 - 警报在下面的代码中成功触发。

我想从一个动画中调用函数来控制另一个动画。我需要帮助了解如何正确 call/name/address 动画。到目前为止,我没有收到 canvas_ship.gotoAndPlay(12);canvas_car.gotoAndPlay(7); 的回复,我已经花了几个小时尝试不同的参考资料。我不是一个大程序员,但我确信这是一件简单的事情。感谢您的帮助!

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Multiple Canvas Animations Talking to Each Other</title>


<script src="http://code.createjs.com/easeljs-0.8.1.min.js"></script>
<script src="http://code.createjs.com/tweenjs-0.6.1.min.js"></script>
<script src="http://code.createjs.com/movieclip-0.8.1.min.js"></script>
<script src="ship.js"></script>
<script src="car.js"></script>

<script>
function init () {

    var canvas, stage, exportRoot;

    canvas = document.getElementById("canvas_ship");
    exportRoot = new libs_ship.ship();

    stage = new createjs.Stage(canvas);
    stage.addChild(exportRoot);
    stage.update();

    createjs.Ticker.setFPS(libs_ship.properties.fps);
    createjs.Ticker.addEventListener("tick", stage);


    canvas = document.getElementById("canvas_car");
    exportRoot = new libs_car.car();

    stage = new createjs.Stage(canvas);
    stage.addChild(exportRoot);
    stage.update();

    createjs.Ticker.setFPS(libs_car.properties.fps);
    createjs.Ticker.addEventListener("tick", stage);
}

function tell_Ship_what_frame_to_go_to(){
    alert("tell_Ship_what_frame_to_go_to was triggered");
    canvas_ship.gotoAndPlay(12);  //This line does not work.
}
function tell_Car_what_frame_to_go_to(){
    alert("tell_Car_what_frame_to_go_to was triggered");
    canvas_car.gotoAndPlay(7);  //This line does not work.
}


</script>
</head>

<body onload="init();" style="background-color:#D4D4D4">
    <canvas id="canvas_ship" width="300" height="250" style="background-color:#FFFFFF"></canvas>
    <canvas id="canvas_car" width="300" height="250" style="background-color:#FFFFFF"></canvas>
</body>
</html>

我得到了帮助,现在分享答案。不客气。找个时间请我吃顿饭吧。

在 Adob​​e Animate 中,您需要将库命名空间(我认为是在“高级”选项卡的“发布”设置中)更改为 lib_jerry 或您想出的任何自定义名称...只要它不同于其他动画。然后只需按照此代码中的设置进行操作。您可以从 Animate 动画中调用函数。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Container</title>

<script src="https://code.createjs.com/createjs-2015.11.26.min.js"></script>
<script src="tommy.js"></script>
<script src="jerry.js"></script>
<script>

var canvas, stage, tomtom, jerjer;
function init() {

    var exportRoot;

    //Tommy
    canvas = document.getElementById("canvas_tommy");

    tomtom = new lib_tommy.tommy();
    stage = new createjs.Stage(canvas);
    stage.addChild(tomtom);
    stage.update();

    createjs.Ticker.setFPS(lib_tommy.properties.fps);
    createjs.Ticker.addEventListener("tick", stage);


    //Jerry
    canvas = document.getElementById("canvas_jerry");

    jerjer = new lib_jerry.jerry();

    stage = new createjs.Stage(canvas);
    stage.addChild(jerjer);
    stage.update();

    createjs.Ticker.setFPS(lib_jerry.properties.fps);
    createjs.Ticker.addEventListener("tick", stage);

}

function button_from_tommy_was_clicked(){   
    tomtom.gotoAndPlay(5);
}

function button_from_jerry_was_clicked(){   
    jerjer.gotoAndPlay(5);
}

</script>

</head>
<body onload="init();" style="background-color:#D4D4D4;margin:0px;">
    <canvas id="canvas_tommy" width="970" height="90" style="background-color:#727272"></canvas>
    <canvas id="canvas_jerry" width="970" height="90" style="background-color:#727272"></canvas>
</body>
</html>