JS初心者が始めるCreateJS 第13回

CreateJSこんにちは。
気まぐれでいろいろ始めちゃう ひろゆき です。

今回は、黒ひよこのデビューです。

HTMLソースコード

[sourcecode language=”js”]<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Piyo + BlackPiyo + Clouds [class] | CreateJS</title>

<link rel="stylesheet" type="text/css" href="css/index.css" media="all">
<script src="js/easeljs-0.7.0.min.js"></script>
<script src="js/tweenjs-0.5.0.min.js"></script>
<script src="js/movieclip-0.7.0.min.js"></script>
<script src="js/preloadjs-0.4.0.min.js"></script>
<script src="js/soundjs-0.5.0.min.js"></script>
<script src="js/Piyo.js"></script>
<script src="js/BlackPiyo.js"></script>
<script src="js/Sun.js"></script>
<script src="js/Clouds.js"></script>
<script src="js/stats.min.js"></script>

<script>
var canvas, stage, piyo, blackpiyo, clouds, stats;

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

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 title = new createjs.Text("CreateJS", "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.6.0]", "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("Piyo + BlackPiyo + Clouds", "20px Myriad Pro", "#FFFFFF");
stage.addChild(subtitle);
subtitle.textAlign = "center";
subtitle.textBaseline = "bottom";
subtitle.x = canvas.width/2;
subtitle.y = 120;
subtitle.alpha = 0.6;
var publish = new createjs.Text("authoring: Sublime Text + Flash CS6 [CreateJS]", "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;

piyo = new lib.Piyo();
//stage.addChild(piyo);
piyo.x = canvas.width/2;
piyo.y = 170;
blackpiyo = new lib.BlackPiyo();
//stage.addChild(blackpiyo);
blackpiyo.x = canvas.width/2 + 100;
blackpiyo.y = 170;

stage.enableMouseOver(10);

images = images||{};

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

stage.update();

createjs.Ticker.setFPS(30);
//createjs.Ticker.useRAF = true;
createjs.Ticker.timingMode = createjs.Ticker.RAF_SYNCHED;
//createjs.Ticker.addListener(this);
}
function fileloaded(event) {
if (event.item.type == "image") {
images[event.item.id] = event.result;
}
}
function loaded(event) {
var sun = new lib.Sun();
stage.addChild(sun);

clouds = [];
var positions = [
{x: 30, y: 20, speed: 1, scale: 1},
{x: 250, y: 20, speed: 1, scale: 1},
{x: 470, y: 20, speed: 1, scale: 1},
{x: 15, y: 40, speed: 0.9, scale: 0.9},
{x: 180, y: 40, speed: 0.9, scale: 0.9},
{x: 345, y: 40, speed: 0.9, scale: 0.9},
{x: 510, y: 40, speed: 0.9, scale: 0.9},
{x: 0, y: 60, speed: 0.8, scale: 0.8},
{x: 132, y: 60, speed: 0.8, scale: 0.8},
{x: 264, y: 60, speed: 0.8, scale: 0.8},
{x: 396, y: 60, speed: 0.8, scale: 0.8},
{x: 528, y: 60, speed: 0.8, scale: 0.8}
];
for (var n = 0; n < 12; n++) {
var cloud = new lib.Clouds();
stage.addChild(cloud);
var position = positions[n];
cloud.x = position.x + (Math.random() – 0.5)*40;
cloud.y = position.y + (Math.random() – 0.5)*10;
cloud.init(position.speed, 600, 30);
cloud.scaleX = cloud.scaleY = position.scale;
clouds.push(cloud);
}

stage.addChild(piyo);
stage.addChild(blackpiyo);

//createjs.Ticker.addListener(this);
createjs.Ticker.addEventListener("tick", tick);

start();
}
function tick() {
for (var n = 0; n < 12; n++) {
var cloud = clouds[n];
cloud.update();
}
piyo.update();
blackpiyo.update();
stage.update();

stats.update();
}
function start() {
piyo.swingHead();
var easing = createjs.Ease.quadInOut;
createjs.Tween.get(piyo).to({x: -60}, 4000, easing).call(complete);
createjs.Tween.get(blackpiyo).wait(800).to({x: -60}, 4000, easing).call(complete);
}
function complete(event) {
var target = event.target;
target.x = 660;
var easing = createjs.Ease.quadInOut;
createjs.Tween.get(target).to({x: -60}, 4000, easing).call(complete);
}
function playSound(id, volume) {
var sound = createjs.Sound.play(id);
sound.volume = volume;
}
</script>
</head>

<body onload="init();" style="background-color:#D4D4D4">
<canvas id="canvas" width="600" height="200" style="background-color:#FFFFFF"></canvas>
</body>
</html>[/sourcecode]

Piyo + BlackPiyo + Clouds [class] | CreateJS

逃げろー!

[追記] (2013.06.30.)
Ticker.addListener() は非推奨になっているようなので、書き換えました。
[追記] (2013.10.30.)
CreateJS を 0.7.0 にバージョンアップ。
Ticker.useRAF は非推奨になっているようなので、書き換えました。