こんにちは。
CreateJS 開発スピードが一向に上がらない ひろゆき です。
今回は、ひよこちゃんの背景で雲を流すことに挑戦してみます。
それっぽくできるかな?
HTMLソースコード
[sourcecode language=”js”]<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Piyo + 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/Sun.js"></script>
<script src="js/Clouds.js"></script>
<script src="js/stats.min.js"></script>
<script>
var canvas, stage, piyo, 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 + 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;
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);
//createjs.Ticker.addListener(this);
createjs.Ticker.addEventListener("tick", tick);
}
function tick() {
for (var n = 0; n < 12; n++) {
var cloud = clouds[n];
cloud.update();
}
piyo.update();
stage.update();
stats.update();
}
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]
Clouds.js
[sourcecode language=”js”](function (lib, img, cjs) {
var p; // shortcut to reference prototypes
// stage content:
(lib.Clouds = function() {
this.initialize();
// clouds
this.instance = new lib.cloud("synched",0);
this.instance.setTransform(0,0);
this.instance.alpha = 0.6;
this.addChild(this.instance);
}).prototype = p = new cjs.Container();
p.nominalBounds = new cjs.Rectangle(0,0,60,40);
// init
p.speed;
p.distance;
p.offset;
p.init = function(speed, distance, offset) {
this.speed = speed;
this.distance = distance;
this.offset = offset;
};
// update
p.update = function() {
this.x += this.speed;
if (this.x > this.distance + this.offset) {
this.x -= (this.distance + this.offset*2);
}
};
(以下略)[/sourcecode]
Piyo + Clouds [class] | CreateJS
背景との前後関係から、ひよこちゃんの配置タイミングを少し後にずらしてみた。
[追記] (2013.05.29.)
SoundJS の 0.4.1 へのバージョンアップに伴い、スクリプトを一部修正しました。
[追記] (2013.06.30.)
Ticker.addListener() は非推奨になっているようなので、書き換えました。
[追記] (2013.10.30.)
CreateJS を 0.7.0 にバージョンアップ。
Ticker.useRAF は非推奨になっているようなので、書き換えました。
1 Comment
[…] JS初心者が始めるCreateJS 第12回 抵抗のカラーコード for […]