[CreateJS] マウスアップ (4) ~stagemouseup, proxy~

CreateJSを使って、マウスアップを学習するよ。 :bouzu:

マウスダウンで首振り、マウスアップでジャンプさせようと思う。 :piyo:
ただし、ジャンプは5回までに制限するね。 :boy:
いろんなケースがあるので、試しに createjs.proxy を使ってみる。

createjs_mouseup4

ちゃんとカウントアップしてる! が!ジャンプが無制限に!
どうやら、イベントリスナーが削除できていないみたい。

index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MouseEvent [stagemouseup + proxy] (limited) | 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/soundjs-0.6.1.min.js"></script>
<script src="js/Piyo.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;
  }
  #footer{
    text-align:center;
    margin-top:20px;
    color:#FF0066;
    font-family:sans-serif;
    font-size:16px;
  }
</style>
<script>

var canvas, stage, stats;
var piyo;
var counter;
var count = 0;

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();

  piyo = new lib.Piyo();
  stage.addChild(piyo);
  piyo.x = canvas.width/2;
  piyo.y = 210;
  piyo.mouseChildren = false;

  piyo.addEventListener("mouseover", createjs.proxy(mouseover, this));
  piyo.addEventListener("mousedown", createjs.proxy(mousedown, this));
  stage.addEventListener("stagemouseup", createjs.proxy(mouseup, this));

  var loader = new createjs.LoadQueue(false);
  loader.installPlugin(createjs.Sound);
  loader.addEventListener("complete", complete);
  loader.loadFile({src: "assets/bound.mp3", id: "bound"});
  loader.loadFile({src: "assets/piyo.mp3", id: "se"});

  createjs.Ticker.setFPS(30);
  createjs.Ticker.timingMode = createjs.Ticker.RAF_SYNCHED;
  if (createjs.Touch.isSupported()) {
    createjs.Touch.enable(stage);
  }
  stage.enableMouseOver(10);
  stage.update();
}
function complete(event) {
  event.target.removeEventListener("complete", complete);

  initialize();
}
function initialize() {
  createjs.Ticker.addEventListener("tick", update);
}
function mouseover(event) {
  piyo.cursor = "pointer";
}
function mousedown(event) {
  piyo.swingHead();

  console.log("mousedown");
  window.footer.innerHTML = "mousedown";
}
function mouseup(event) {
  piyo.jump();

  count ++;
  counter.text = count;
  if (count >= 5) {
    clear();
  }

  console.log("mouseup");
  window.footer.innerHTML = "mouseup";
}
function clear() {
  piyo.cursor = "default";
  piyo.removeEventListener("mouseover", createjs.proxy(mouseover, this));
  piyo.removeEventListener("mousedown", createjs.proxy(mousedown, this));
  stage.removeEventListener("stagemouseup", createjs.proxy(mouseup, this));
}
function update(event) {
  piyo.update();
  stage.update();

  stats.update();
}
function playSound(id, volume) {
  var sound = createjs.Sound.play(id);
  sound.volume = volume;
}
function background() {
  var sky = new createjs.Shape();
  stage.addChild(sky);
  sky.graphics.beginLinearGradientFill(["#0069A0", "#00AAE4"], [0, 1], 0, 0, 0, 180);
  sky.graphics.drawRect(0, 0, 600, 180);

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

  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("MouseEvent", "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 subtitle = new createjs.Text("stagemouseup + proxy (limited)", "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;

  counter = new createjs.Text("0", "24px Myriad Pro", "#FFFFFF");
  stage.addChild(counter);
  counter.textAlign = "center";
  counter.textBaseline = "middle";
  counter.x = canvas.width/2;
  counter.y = 130;
  counter.alpha = 0.8;
}
</script>
</head>
<body onload="init();" style="background-color:#EEEEEE">
  <canvas id="canvas" width="600" height="240" style="background-color:#FFFFFF"></canvas>
  <div id="footer"></div>
</body>
</html>


[修正] (15/09/21 Mon 00:38)
CreateJS 0.8.1 にバージョンアップ。