# enchant.js 小テスト1-3 ## 課題: coinの作成 ## タスク: coinは0.5秒に1回(core.frame % 15 == 0)X座標はランダム、y座標は0に出現し下に5の速さで落ちていきます。 ### ヒント りんごキャッチのりんごと同じように落ちていきます。 コイン作成はrootSceneの"enterframe"内で作ります。 コインの動作はコインのクラスの中に記述していきます。 ### 開始コード: enchant(); window.onload = function() { const PLAYER = 'player.png'; const COIN = 'coin.png'; const ENEMY = 'enemy.png'; const DISP_SIZE = 320; const TIME = 30; var score = 0; var core = new Core(DISP_SIZE, DISP_SIZE); core.preload(PLAYER, COIN, ENEMY); core.fps = 30; core.onload = function() { var player = new Sprite(100, 100); player.x = DISP_SIZE / 2; player.y = DISP_SIZE - player.height * 2; player.image = core.assets[PLAYER]; player.on('enterframe', function() { if (core.input.left) { this.x -= 5; } if (core.input.right) { this.x += 5; } }); core.rootScene.addChild(player); var scoreLabel = new Label('Score: ' + score); scoreLabel.x = 10; scoreLabel.y = 10; core.rootScene.addChild(scoreLabel); var Coin = Class.create(Sprite, { initialize: function(x, y) { Sprite.call(this, 16, 16); this.x = ; this.y = ; this.image = this.on('', function() { }); } }); core.rootScene.on('enterframe', function() { scoreLabel.text = 'Score: ' + score; if () { core.rootScene.addChild(coin); } if (core.frame === core.fps * TIME) { core.stop(); } }); //上記のコインのコード、rootScene"enterframe"を完成させてください。 }; core.start(); }; function rand(n) { return Math.floor(Math.random() * (n + 1)); } <details> <summary>解答</summary> enchant(); window.onload = function() { const PLAYER = 'player.png'; const COIN = 'coin.png'; const ENEMY = 'enemy.png'; const DISP_SIZE = 320; const TIME = 30; var score = 0; var core = new Core(DISP_SIZE, DISP_SIZE); core.preload(PLAYER, COIN, ENEMY); core.fps = 30; core.onload = function() { var player = new Sprite(100, 100); player.x = DISP_SIZE / 2; player.y = DISP_SIZE - player.height * 2; player.image = core.assets[PLAYER]; player.on('enterframe', function() { if (core.input.left) { this.x -= 5; } if (core.input.right) { this.x += 5; } }); core.rootScene.addChild(player); var scoreLabel = new Label('Score: ' + score); scoreLabel.x = 10; scoreLabel.y = 10; core.rootScene.addChild(scoreLabel); var Coin = Class.create(Sprite, { initialize: function(x, y) { Sprite.call(this, 16, 16); this.x = x; this.y = y; this.image = core.assets[COIN]; this.on('enterframe', function() { this.y += 5; if (this.intersect(player)) { core.rootScene.removeChild(this); score++; } }); } }); core.rootScene.on('enterframe', function() { scoreLabel.text = 'Score: ' + score; if (core.frame % 15 === 0) { var coin = new Coin(rand(DISP_SIZE - 16), 0); core.rootScene.addChild(coin); } if (core.frame === core.fps * TIME) { core.stop(); } }); }); core.start(); }; function rand(n) { return Math.floor(Math.random() * (n + 1)); } </details>