Skip to main content
Inspiring
December 28, 2022
Answered

assigning names from a class adobe animate/html5/easelJS

  • December 28, 2022
  • 2 replies
  • 794 views

Hi team,

So I think I got my index.html and game.js file connected successfully

- here is some code from an easelJS tutorial to get a moving red circle- this code is in my game.js file and the circle works fine.

class Game{
    constructor(exportRoot, stage){
        this.root = exportRoot;
        this.stage = stage;
        this.init();
    }
    
    init(){
        const game = this;

        var circle = new createjs.Shape();
        circle.graphics.beginFill("Crimson").drawCircle(0, 0, 50);
        circle.x = 100;
        circle.y = 100;
        stage.addChild(circle);
        createjs.Tween.get(circle, {loop: true})
          .to({x: 400}, 1000, createjs.Ease.getPowInOut(4))
          .to({alpha: 0, y: 75}, 500, createjs.Ease.getPowInOut(2))
          .to({alpha: 0, y: 125}, 100)
          .to({alpha: 1, y: 100}, 500, createjs.Ease.getPowInOut(2))
          .to({x: 100}, 800, createjs.Ease.getPowInOut(2));
        createjs.Ticker.setFPS(60);
        createjs.Ticker.addEventListener("tick", stage);
      }
}

Here is the index.html

<!DOCTYPE html>
<!--
	NOTES:
	1. All tokens are represented by '$' sign in the template.
	2. You can write your code only wherever mentioned.
	3. All occurrences of existing tokens will be replaced by their appropriate values.
	4. Blank lines will be removed automatically.
	5. Remove unnecessary comments before creating your template.
-->
<html>
<head>
<meta charset="UTF-8">
<meta name="authoring-tool" content="Adobe_Animate_CC">
<title>javaTest</title>
<!-- write your code here -->
<style>
  #animation_container {
	position:absolute;
	margin:auto;
	left:0;right:0;
	top:0;bottom:0;
  }
</style>
<script src="https://code.createjs.com/1.0.0/createjs.min.js"></script>
<script src="x-javaTest.js"></script>
<script src="game.js"></script>
<script>
var canvas, stage, exportRoot, anim_container, dom_overlay_container, fnStartAnimation, game;
function init() {
	canvas = document.getElementById("canvas");
	anim_container = document.getElementById("animation_container");
	dom_overlay_container = document.getElementById("dom_overlay_container");
	var comp=AdobeAn.getComposition("A21967C6DB7943FE8681899B44492054");
	var lib=comp.getLibrary();
	handleComplete({},comp);
}
function handleComplete(evt,comp) {
	//This function is always called, irrespective of the content. You can use the variable "stage" after it is created in token create_stage.
	var lib=comp.getLibrary();
	var ss=comp.getSpriteSheet();
	exportRoot = new lib.javaTest();
	stage = new lib.Stage(canvas);	
	//Registers the "tick" event listener.
	fnStartAnimation = function() {
          game = new Game(stage, exportRoot, lib);
		stage.addChild(exportRoot);
		createjs.Ticker.framerate = lib.properties.fps;
		createjs.Ticker.addEventListener("tick", stage);
	}	    
	//Code to support hidpi screens and responsive scaling.
	AdobeAn.makeResponsive(true,'both',true,1,[canvas,anim_container,dom_overlay_container]);	
	AdobeAn.compositionLoaded(lib.properties.id);
	fnStartAnimation();
}
    function handleTick(event) {
            if (game === undefined) game = new Game(exportRoot, stage);
    }
</script>
<!-- write your code here -->
</head>
<body onload="init();" style="margin:0px;">
	<div id="animation_container" style="background-color:rgba(255, 255, 255, 1.00); width:960px; height:640px">
		<canvas id="canvas" width="960" height="640" style="position: absolute; display: block; background-color:rgba(255, 255, 255, 1.00);"></canvas>
		<div id="dom_overlay_container" style="pointer-events:none; overflow:hidden; width:960px; height:640px; position: absolute; left: 0px; top: 0px; display: block;">
		</div>
	</div>
</body>
</html>

 but when I try to put an object - a button or a movieclip on the timeline my naming doesnt work.

class Game{
    constructor(exportRoot, stage){
        this.root = exportRoot;
        this.stage = stage;
        this.init();
    }
    
    init(){
        const game = this;
game.root.rect1.gotoAndStop("red");
      }
}

I think my index is wrong somehow? Or still I am just not understanding something?

Thanks for any help

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    There you go:
    https://bit.ly/3Q3eOwC

     

    I hope it helps to clarify all doubts.

    2 replies

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    December 29, 2022

    There you go:
    https://bit.ly/3Q3eOwC

     

    I hope it helps to clarify all doubts.

    Inspiring
    December 29, 2022


    Yes thank you this works! How and why it works is another thing - I just need to make sure I can modify it without issue but this is great- thank you for taking the time

    Inspiring
    December 29, 2022

    OK! I feel like I can confidently say that I can make changes to the project and change instance names and I havent broken it!  Yes!!  This was supposed to be a very simple procedure if I just followed the project along but I wanted to be able to build a project from scratch- not sure if I have acheived a full understanding here but I can look into your solution and work out how and why it works!  As always excellent! 

    kglad
    Community Expert
    Community Expert
    December 28, 2022

    don't call init before onstage instances are ready

    Inspiring
    December 28, 2022

    tell me more kglad?  What do I need to look at?

    kglad
    Community Expert
    Community Expert
    December 28, 2022

    init should not be called from the constructor if you're going to reference onstage instances there.