troubleshooting Question

CreateJS - animation being used is last one assigned

Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America asked on
Game ProgrammingComputer GamesJavaScript
4 Comments1 Solution348 ViewsLast Modified:
If you look at my "generateGameBoard(  )" function, it should be assigning either one PNG or the other, depending on the modulus logic I've put in place.

What ends-up happening is the last image assigned becomes the one that ALL of the canvas elements are using.

I'm new to animation, CreatgeJS and also new to  JavaScript arrays and could use some help getting this fixed so it works as intended.  

Thank you!


JavaScript:
// for future use:  var initGamePieces = {};

// for future use:  initGamePieces["00"] = "powersourceRed.png";


$(document).ready(function() {

    function generateGameBoard() {
        var app = "";
        for (var y = 0; y < 10; y++) {
            for (var x = 0; x < 10; x++) {
                //app += "<canvas id='c' class='canvas' spritesrc='images/wall.png' height='64' width='64'></canvas>";

                //app += "<div id='" + x + y + "' class='gamepiece'><canvas id='canvas1' class='canvas' spritesrc='../images/destroyerRed.png' height='64' width='64'></canvas></div>";

                //app += "<canvas id='" + y + x + "' class='canvas' spriteSrc='images/destroyerRed.png' height='64' width='64'></canvas>";

                if (x % 2 === 0) {
                    app += "<canvas id='" + y + x + "' class='canvas' spriteSrc='images/destroyerRed.png' height='64' width='64'></canvas>";
                } else {
                    app += "<canvas id='" + y + x + "' class='canvas' spriteSrc='images/engineerBlue.png' height='64' width='64'></canvas>";
                }

                //app += "<div id='" + x + y + "' class='gamepiece'><canvas id='canvas' class='canvas' spritesrc='http://static.guineashots.com/tutorials/easeljs/assets/bubbles.png' height='64' width='64'></canvas></div>";
            }
        }
    

        $("#gameboard").empty();
        $("#gameboard").append(app);

        console.log("done generating empty game board");
        console.log(app);
    }

    generateGameBoard();

    function init(canvas) {
        var stage = new createjs.Stage(canvas);

        canvas.width = window.innerWidth;

        img = new Image();

        img.src = canvas.getAttributeNode("spriteSrc").value;

        console.log(img.src);

        img.onload = function (event) {

            var data = {
                framerate: 10,
                images: [img],
                frames: { width: 64, height: 64, regX: 32, regY: 32 },
                animations: {
                    'explode': [0, 10]
                }
            }

            var spritesheet = new createjs.SpriteSheet(data);
            var animation = new createjs.Sprite(spritesheet, "explode");
            animation.x = canvas.width / 2;
            animation.y = canvas.height / 2;

            stage.addChild(animation);
          //  var i = 0;

            createjs.Ticker.addEventListener("tick", update);
            function update(event) {

                animation.x = 50;
                animation.y = 40;
                stage.update();

                //i++;

                //if (i > 300) {
                //    i = 0;
                //}
            }
        }
    }


    canvasArr = document.getElementsByClassName("canvas");

    for (var a = 0; a < canvasArr.length; a++) {
        init(canvasArr[a]);
        console.log(canvasArr[a].id);
    }

});

Open in new window


HTML:
<!DOCTYPE html>
<meta charset="utf-8">

<html>
<head>
    <title></title>
    <link href="Styles/robotzcss.css" rel="stylesheet"/>
    <script src="Scripts/jquery-1.10.2.js"></script>
    <!--<script src="https://code.createjs.com/createjs-2015.11.26.min.js"></script>-->
    <script src="Scripts/robotz/robotzjs.js"></script>
</head>
<body>

    <script src="https://code.createjs.com/createjs-2015.11.26.min.js"></script>
    <div id="gameboard" style="position: relative; float: left; height: 680px; border: 1px solid red; width: 680px;"></div>

</body>
</html>

Open in new window


CSS:
body{
    margin: 0px;
    padding:0px;
}


.gamepiece {
    position:relative;
    float:left;
    width: 64px;
    height: 64px;
    border:1px solid transparent;
}

/* unvisited link */
.gamepiece:link {
    
}

/* visited link */
.gamepiece:visited {
    
}

/* mouse over link */
.gamepiece:hover {
    background-color: azure;
}

/* selected link */
.gamepiece:active {
    
}

Open in new window

ASKER CERTIFIED SOLUTION
Robert Schutt
Software Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 4 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 4 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004