# enchant.js 小テスト1-2 ## 課題: プレイヤーの動作 ## タスク:プレイヤーが矢印キーで左右に動作する。 矢印キーを押すとプレイヤーが左右移動するようにします。 得点を画面に表示だけします。 ### ヒント playerが"enterframe"内で矢印キーを押すと左右に移動します。 scoreLabelというlabel(文字)のクラスで作成します。 ### 開始コード: enchant(); window.onload = function() { const PLAYER = 'player.png'; const COIN = 'coin.png'; const ENEMY = 'enemy.png'; const DISP_SIZE = 640; 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]; core.rootScene.addChild(player); //ここにコードを記述 }; 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 = 640; 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); }; core.start(); }; function rand(n) { return Math.floor(Math.random() * (n + 1)); } </details>
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up