[CreateJS] 画像の切り替え (2) ~Sprite, SpriteSheet~

CreateJSを使って、画像の切り替えを学習するよ。 :bouzu:

createjs_frogs2

SpriteクラスSpriteSheetクラスを試してみたよ。 :boy:

index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sprite + SpriteSheet [frogs] | CreateJS</title>
<script src="js/easeljs-0.8.1.min.js"></script>
<script src="js/tweenjs-0.6.1.min.js"></script>
<script src="js/movieclip-0.8.1.min.js"></script>
<script src="js/preloadjs-0.6.1.min.js"></script>
<script src="js/button.js"></script>
<script src="js/stats.min.js"></script>
<style type="text/css">
  #canvas{
    display:block;
    width:600px;
    margin-top:40px;
    margin-left:auto;
    margin-right:auto;
    padding-left:0;
    padding-right:0;
  }
</style>
<script>

var canvas, stage, stats;
var images;
var frog;
var selectedId = 0;
var playBtn;

function init() {
  stats = new Stats();
  stats.setMode(0);
  stats.domElement.style.position = "fixed";
  stats.domElement.style.right = "0px";
  stats.domElement.style.top = "0px";
  document.body.appendChild(stats.domElement);

  canvas = document.getElementById("canvas");

  stage = new createjs.Stage(canvas);

  background();

  playBtn = new lib.PlayButton();
  stage.addChild(playBtn);
  playBtn.x = canvas.width/2;
  playBtn.y = 220;
  playBtn.addEventListener("click", play);
  playBtn.enabled();

  images = images||{};
  var loader = new createjs.LoadQueue(false);
  loader.addEventListener("fileload", fileload);
  loader.addEventListener("complete", complete);
  loader.loadFile({src: "assets/frogs.png", id: "frogs"});

  createjs.Ticker.setFPS(30);
  createjs.Ticker.timingMode = createjs.Ticker.RAF_SYNCHED;
  if (createjs.Touch.isSupported()) {
    createjs.Touch.enable(stage);
  }
  stage.enableMouseOver(10);
  stage.update();
  createjs.Ticker.addEventListener("tick", update);
}
function fileload(event) {
  if (event.item.type == "image") {
    images[event.item.id] = event.result;
  }
}
function complete(event) {
  event.target.removeEventListener("fileload", fileload);
  event.target.removeEventListener("complete", complete);

  initialize();
}
function initialize() {
  var data = {
    "images": ["assets/frogs.png"],
    "frames": [[164, 69, 52, 65, 0, 26, 60], [110, 69, 52, 65, 0, 26, 60], [56, 136, 52, 65, 0, 26, 60], [56, 69, 52, 65, 0, 26, 60], [164, 2, 52, 65, 0, 26, 60], [110, 2, 52, 65, 0, 26, 60], [56, 2, 52, 65, 0, 26, 60], [2, 136, 52, 65, 0, 26, 60], [2, 69, 52, 65, 0, 26, 60], [2, 2, 52, 65, 0, 26, 60]],
    "animations": {"frog0": 0, "frog1": 0, "frog2": 2, "frog3": 3, "frog4": 4, "frog5": 5, "frog6": 6, "frog7": 7, "frog8": 8, "frog9": 9}
  };
  var spritesheet = new createjs.SpriteSheet(data);

  frog = new createjs.Sprite(spritesheet);
  stage.addChild(frog);
  frog.x = canvas.width/2;
  frog.y = 175;
}
function play(event) {
  selectedId = (selectedId + 1)%10;
  frog.gotoAndStop("frog" + selectedId);
}
function update(event) {
  stage.update();

  stats.update();
}

function background() {
  var sky = new createjs.Shape();
  stage.addChild(sky);
  sky.graphics.beginLinearGradientFill(["#0069A0", "#00AAE4"], [0, 1], 0, 0, 0, 150);
  sky.graphics.drawRect(0, 0, 600, 150);

  var ground = new createjs.Shape();
  stage.addChild(ground);
  ground.graphics.beginLinearGradientFill(["#99CC33", "#7EB133"], [0, 1], 0, 150, 0, 200);
  ground.graphics.drawRect(0, 150, 600, 50);

  var basic = new createjs.Text("HTML5 / CreateJS [0.8.1]", "14px Myriad Pro", "#FFFFFF");
  stage.addChild(basic);
  basic.textAlign = "left";
  basic.textBaseline = "bottom";
  basic.x = 10;
  basic.y = 20;
  basic.alpha = 0.6;
  var title = new createjs.Text("EaselJS", "24px Myriad Pro", "#FFFFFF");
  stage.addChild(title);
  title.textAlign = "center";
  title.textBaseline = "bottom";
  title.x = canvas.width/2;
  title.y = 65;
  title.alpha = 0.6;
  var version = new createjs.Text("[0.8.1]", "14px Myriad Pro", "#FFFFFF");
  stage.addChild(version);
  version.textAlign = "center";
  version.textBaseline = "bottom";
  version.x = canvas.width/2;
  version.y = 80;
  version.alpha = 0.6;
  var subtitle = new createjs.Text("Sprite + SpriteSheet", "20px Myriad Pro", "#FFFFFF");
  stage.addChild(subtitle);
  subtitle.textAlign = "center";
  subtitle.textBaseline = "bottom";
  subtitle.x = canvas.width/2;
  subtitle.y = 110;
  subtitle.alpha = 0.6;
  var publish = new createjs.Text("authoring: Sublime Text 2 + Flash CC", "14px Myriad Pro", "#FFFFFF");
  stage.addChild(publish);
  publish.textAlign = "right";
  publish.textBaseline = "bottom";
  publish.x = canvas.width - 10;
  publish.y = 20;
  publish.alpha = 0.6;
}
</script>
</head>
<body onload="init();" style="background-color:#EEEEEE">
  <canvas id="canvas" width="600" height="240" style="background-color:#FFFFFF"></canvas>
</body>
</html>


[修正] (15/09/20 Sun 23:55)
CreateJS 0.8.1 にバージョンアップ。