生活随笔
收集整理的這篇文章主要介紹了
createjs打飞机
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 此文章代碼來源于createjs打飛機
- 我只是將他教程代碼,一步步寫出來了而已
- 下面給出完整代碼,其中有一張雪碧圖和音效資源,包含在源文章中,這里沒有
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><script src="js/easeljs.js"></script><script src="js/soundjs.js"></script><script src="js/preloadjs.js"></script><script src="js/tweenjs.js"></script><script src="js/jquery-2.1.4.min.js"></script><title>Document
</title>
</head><body><canvas id="game" width="500" height="700" style="background-color: black"></canvas><div id="btn"></div><script>var stage;var starArr = [];const ARROW_KEY_LEFT = 37; const ARROW_KEY_RIGHT = 39; const SPACE_KEY = 32; var leftKeyDown = false,rightKeyDown = false; var fireAble = true; var lives = 24;var score = 0;var enemyClip = [];var enemy = [];var player;var speed = 5;var fires = [];breakAble = true;function init() {stage = new createjs.Stage('game');sWidth = stage.canvas.width;sHeight = stage.canvas.height;queue = new createjs.LoadQueue(true);createjs.Sound.registerPlugins([createjs.HTMLAudioPlugin]);queue.installPlugin(createjs.Sound);queue.on("complete", handleComplete);queue.loadFile({id: "shot",src: "music/aa.mp3"});queue.loadManifest([{id: "sprite",src: "./img/sprites.png"},]);}init();var instance = null;function handleComplete() {console.log(queue.getResult('shot'));queue.getResult('shot').setAttribute('webkit-playsinline', 'true');queue.getResult('shot').setAttribute('playsinline', 'true');$('#btn').click(function () {var instance = createjs.Sound.play("shot");});$('#btn').click();}function audioComplete(e) {console.log('d');console.log(e);}function buildGame() {buildMsg(); buildPlayer(); buildEnemy(); buildSpace(); }function updateGame() {updateStar();updatePlayer();updateFire();updateEnemy();updateMsg();}function buildSpace() {var i, star, w, h, alpha;starSky = new createjs.Container();for (i = 0; i < 200; i++) {star = new createjs.Shape();w = Math.floor(Math.random() * stage.canvas.width);h = Math.floor(Math.random() * stage.canvas.height);alpha = Math.random();star.graphics.beginFill("#FFF").drawCircle(0, 0, 1);star.x = w;star.y = h;star.alpha = alpha;starSky.addChild(star);starArr.push(star);stage.addChild(starSky);}}function buildPlayer() {var data = {images: [queue.getResult("sprite")],frames: [[0, 0, 37, 42],[37, 0, 42, 42],[79, 0, 37, 42],[116, 0, 42, 42],[158, 0, 37, 42],[0, 70, 64, 64],[64, 70, 64, 64],[128, 70, 64, 64],[192, 70, 64, 64],[256, 70, 64, 64],[320, 70, 64, 64],[384, 70, 64, 64],[448, 70, 64, 64],[512, 70, 64, 64],[576, 70, 64, 64],[640, 70, 64, 64],[704, 70, 64, 64],[768, 70, 64, 64]],animations: {ship: 0,enemy1: 1,enemy2: 2,enemy3: 3,enemy4: 4,exp: {frames: [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],speed: .5}}};spriteSheet = new createjs.SpriteSheet(data);player = new createjs.Sprite(spriteSheet, "ship");player.x = sWidth / 2;player.y = sHeight - player.getBounds().height;stage.addChildAt(player, 0);}function setControl() {window.onkeydown = handleKeyDown;window.onkeyup = handleKeyUp;}function handleKeyDown(e) {e = !e ? window.event : e;switch (e.keyCode) {case ARROW_KEY_LEFT:leftKeyDown = true;break;case ARROW_KEY_RIGHT:rightKeyDown = true;break;}}function handleKeyUp(e) {e = !e ? window.event : e;switch (e.keyCode) {case ARROW_KEY_LEFT:leftKeyDown = false;break;case ARROW_KEY_RIGHT:rightKeyDown = false;break;case SPACE_KEY:playFire();}}function playFire() {if (fireAble) {var fire = new createjs.Shape();fire.graphics.beginFill("#FF0").drawRect(0, 0, 2, 5).endFill();fire.x = player.x + 18;fire.y = 658;createjs.Sound.play("shot");fires.push(fire);stage.addChild(fire);}}function buildMsg() {livesTxt = new createjs.Text("lives:" + lives, "20px Times", "#FFF");livesTxt.y = 5;livesTxt.x = 10;stage.addChild(livesTxt);scoreTxt = new createjs.Text("score:" + score, "20px Times", "#FFF");scoreTxt.y = 5;scoreTxt.x = sWidth - 100;stage.addChild(scoreTxt);}function buildEnemy() {var i, e1, e2, e3, e4;e1 = new createjs.Sprite(spriteSheet, "enemy1");e2 = new createjs.Sprite(spriteSheet, "enemy2");e3 = new createjs.Sprite(spriteSheet, "enemy3");e4 = new createjs.Sprite(spriteSheet, "enemy4");enemyClip.push(e1, e2, e3, e4);console.log(enemyClip);buildEnemis();}function buildEnemis() {var i, j = 0,en, en1;var k = 0; for (i = 0; i < 4; i++) {en = enemyClip[i].clone();for (j = 0; j < 6; j++) {k++;en1 = en.clone();enemy.push(en1);createjs.Tween.get(en1).wait(5000 * k).to({x: 300,y: 800}, 5000, createjs.Ease.sineInOut(-2))stage.addChild(en1);}}console.log(enemy);}function updateStar() {var i, star, yPos;for (i = 0; i < 200; i++) {star = starArr[i];yPos = star.y + 5 * star.alpha;if (yPos >= stage.canvas.height) {yPos = 0;}star.y = yPos;}}function updatePlayer() {var nextX = player.x;if (leftKeyDown) {nextX = player.x - speed;if (nextX < 0) {nextX = 0;}} else if (rightKeyDown) {nextX = player.x + speed;if (nextX > (sWidth - 37)) {nextX = sWidth - 37;}}player.x = nextX;}function updateFire() {var i, nextY, fire;for (i = 0; i < fires.length; i++) {fire = fires[i];nextY = fire.y - 10;if (nextY == 0) { fires.splice(i, 1)stage.removeChild(fire);continue;}fire.y = nextY;}}function updateEnemy() {var i, j, fire, enemy1;for (i = 0; i < fires.length; i++) {for (j = 0; j < enemy.length; j++) {fire = fires[i];enemy1 = enemy[j];var fx = fire && fire.x;var fy = fire && fire.y;var ex = enemy1.x;var ey = enemy1.y;var ew = enemy1.getBounds().width;var eh = enemy1.getBounds().height;if (ey >= 800 || ex >= 600) {enemy.splice(j, 1);stage.removeChild(enemy1);}if (fy < ey + eh && fy > ey && fx > ex && fx < ex + ew && ey > 0) {score += 10;fires.splice(i, 1);enemy.splice(j, 1);stage.removeChild(fire);stage.removeChild(enemy1);createjs.Sound.play("explosion");var exp1 = new createjs.Sprite(spriteSheet, "exp");exp1.x = ex;exp1.y = ey;exp1.addEventListener('animationend', function(e) {stage.removeChild(e.target);});stage.addChild(exp1);}}}}function updateMsg() {scoreTxt.text = "score:" + score;livesTxt.text = "lives:" + lives;}function checkGame() {var i, en, pl;if (enemy.length == 0) {buildEnemis();}pl = player;plx = player.x;ply = player.y;plw = player.getBounds().width;plh = player.getBounds().height;for (i = 0; i < enemy.length; i++) {en = enemy[i];enx = en.x;eny = en.y;enw = en.getBounds().width;enh = en.getBounds().height;if (eny + enh < 800 && eny + enh > ply && enx > plx && enx < plx + plw && breakAble) {stage.removeChild(player);pl = null;player = null;fireAble = false;breakAble = false;setTime = setTimeout(newPlayer, 10);break;}}}function newPlayer() {clearTimeout(setTime);player = new createjs.Sprite(spriteSheet, "ship");player.x = sWidth / 2;player.y = sHeight - player.getBounds().height;player.alpha = 0;createjs.Tween.get(player).to({alpha: 1}, 1000, createjs.Ease.getPowIn(1)).call(function() {lives--;fireAble = true;breakAble = true;});stage.addChildAt(player, 0);}function startGame(event) {createjs.Ticker.setFPS(60);createjs.Ticker.addEventListener('tick', function() {updateGame(); checkGame();stage.update();});}</script>
</body></html>
總結
以上是生活随笔為你收集整理的createjs打飞机的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。