# enchant.js 宿題1-3 ## 課題3: アイテムと敵キャラクターの追加 ### タスク: ・"apple.png"という名前の画像を使用してリンゴのアイテムを作成してください。 ・"bat.png"という名前の画像を使用してコウモリの敵キャラクターを作成してください。 開始コード: enchant(); window.onload = function() { const BACKGROUND = "res/mori.jpeg" const CAT = "res/cat.png"; const DISP_SIZE = 640; var core = new Core(DISP_SIZE, DISP_SIZE); core.preload(BACKGROUND, CAT); core.onload = function() { var background = new Sprite(600, 338); background.x = 0; background.y = 0; background.scaleX = 1.0; background.scaleY = 2.0; background.image = core.assets[BACKGROUND]; core.rootScene.addChild(background); var cat = new Sprite(100, 100); cat.x = 5; cat.y = 400; cat.scaleX = 0.5; cat.scaleY = 0.5; cat.image = core.assets[CAT]; cat.on('enterframe', function() { if (core.input.right) { this.x += 10; } if (core.input.left) { this.x -= 10; } }); core.rootScene.addChild(cat); // ここにコードを追加 } core.start(); } <details> <summary>解答</summary> enchant(); window.onload = function() { const BACKGROUND = "res/mori.jpeg" const CAT = "res/cat.png"; const APPLE = "res/apple.png"; const BAT = "res/bat.png"; const DISP_SIZE = 640; var core = new Core(DISP_SIZE, DISP_SIZE); core.preload(BACKGROUND, CAT, APPLE, BAT); core.onload = function() { var background = new Sprite(600, 338); background.x = 0; background.y = 0; background.scaleX = 1.0; background.scaleY = 2.0; background.image = core.assets[BACKGROUND]; core.rootScene.addChild(background); var cat = new Sprite(100, 100); cat.x = 5; cat.y = 400; cat.scaleX = 0.5; cat.scaleY = 0.5; cat.image = core.assets[CAT]; cat.on('enterframe', function() { if (core.input.right) { this.x += 10; } if (core.input.left) { this.x -= 10; } }); core.rootScene.addChild(cat); var apple = new Sprite(100, 100); apple.x = 400; apple.y = 0; apple.scaleX = 0.5; apple.scaleY = 0.5; apple.image = core.assets[APPLE]; apple.on("enterframe", function(){ this.y += 5; }); core.rootScene.addChild(apple); var bat = new Sprite(400, 281); bat.x = 100; bat.y = 200; bat.scaleX = 0.5; bat.scaleY = 0.5; bat.image = core.assets[BAT]; bat.on("enterframe", function(){ if (core.frame % 40 >= 20) { this.y += rand(25); } else { this.y -= rand(20); } }) core.rootScene.addChild(bat); } core.start(); } function rand(n) { return Math.floor(Math.random() * (n + 1)); } </details>