PKJNB index.html シンプル シューティング - js do it PKJ/|index.js// forked from _shimizu's "シューティング (未完成)" http://jsdo.it/_shimizu/pP7D enchant(); var w = 420; var h = 420; var Background = enchant.Class.create(enchant.Sprite, { initialize: function(){ enchant.Sprite.call(this, 420, 840); this.x = 0; this.y =-420; this.image = game.assets['http://jsrun.it/assets/f/i/n/w/finw5.jpg']; this.addEventListener('enterframe', function(){ this.y += 2; if(this.y >= 420) this.y = -840; }); game.rootScene.addChild(this); } }); var Player = enchant.Class.create(enchant.Sprite, { initialize: function(x, y){ enchant.Sprite.call(this, 60, 60); this.image = game.assets['http://jsrun.it/assets/n/z/B/B/nzBBh.png']; this.x = x; this.y = y; this.frame = 21; var root = game.rootScene; root.addEventListener('touchstart', function(e){ player.x = e.x; game.touched = true; }); root.addEventListener('touchend', function(e){ player.x = e.x; game.touched = false; }); root.addEventListener('touchmove', function(e){ player.x = e.x; }); this.addEventListener('enterframe', function(){ if(game.touched && game.frame % 3 == 0){ var s = new PlayerShoot(this.x+24, this.y) ; } }); game.rootScene.addChild(this); } }); var Shoot = enchant.Class.create(enchant.Sprite, { initialize: function(x, y, direction){ enchant.Sprite.call(this, 16, 16); this.image = game.assets['http://jsrun.it/assets/2/u/s/u/2usuD.png']; this.x = x; this.y = y; this.frame = 56; this.direction = direction; this.moveSpeed = 10; this.addEventListener('enterframe', function(){ this.y -= this.moveSpeed * Math.cos(this.direction); this.x += this.moveSpeed * Math.sin(this.direction); if(this.y > h || this.x > w || this.x < -this.width || this.y < -this.height){ this.remove(); } }); game.rootScene.addChild(this); }, remove: function(){ game.rootScene.removeChild(this); delete this; } }); var PlayerShoot = enchant.Class.create(Shoot, { initialize: function(x, y){ Shoot.call(this, x, y, 0); this.addEventListener('enterframe', function(){ for(var i in enemies){ if(enemies[i].intersect(this)){ var blast = new Blast(enemies[i].x, enemies[i].y); this.remove(); enemies[i].remove(); game.score += 100; } } }); } }); var Enemy = enchant.Class.create(enchant.Sprite, { initialize: function(x, y, omega){ enchant.Sprite.call(this, 32, 32); this.image = game.assets['http://jsrun.it/assets/2/j/o/h/2johw.png']; this.x = x; this.y = y; this.frame = 0; this.time = 0; this.omega = omega * Math.PI / 180; this.direction = 0; this.moveSpeed = 3; this.addEventListener('enterframe', function(){ this.direction += this.omega; this.y += this.moveSpeed * Math.cos(this.direction); this.x += this.moveSpeed * Math.sin(this.direction); if(this.y > h || this.x > w || this.x < -this.width || this.y < -this.height){ this.remove(); }else if(this.time++ % 20 == 0){ var s = new EnemyShoot(this.x+6, this.y); } }); game.rootScene.addChild(this); }, remove: function(){ game.rootScene.removeChild(this); delete enemies[this.key]; delete this; } }); var EnemyShoot = enchant.Class.create(Shoot, { initialize: function(x, y){ Shoot.call(this, x, y, Math.PI); this.frame = 52; this.addEventListener('enterframe', function(){ if(player.within(this, 8)){ game.end(game.score, "SCORE: "+ game.score); } }); } }); var Blast = enchant.Class.create(enchant.Sprite, { initialize: function(x, y){ enchant.Sprite.call(this, 16, 16); this.x = x; this.y = y; this.image = game.assets['http://jsrun.it/assets/s/O/p/w/sOpw9.gif']; this.frame = 0; this.duration = 20; this.frame = 0; this.time = 0; this.addEventListener('enterframe', function(){ this.time++; this.frame = Math.floor(this.time / this.duration * 5); if(this.time == this.duration) this.remove(); }); game.rootScene.addChild(this); }, remove:function(){ game.rootScene.removeChild(this); } }); window.onload = function(){ game = new Game(w, h); game.fps = 24; game.score = 0; game.touched = false; game.preload( 'http://jsrun.it/assets/2/j/o/h/2johw.png', 'http://jsrun.it/assets/n/z/B/B/nzBBh.png', 'http://jsrun.it/assets/2/u/s/u/2usuD.png', 'http://jsrun.it/assets/2/j/o/h/2johw.png', 'http://jsrun.it/assets/s/O/p/w/sOpw9.gif', 'http://jsrun.it/assets/f/i/n/w/finw5.jpg' ); game.onload = function(){ background1 = new Background(); background2 = new Background(); background2.y = -1260; player = new Player(0, 350); enemies = []; // game.rootScene.backgroundColor = 'black'; game.rootScene.addEventListener('enterframe', function(){ if(rand(100) < 10){ var x = rand(w); var omega = x < 160 ? 1 :-1; var enemy = new Enemy(x, 0, omega); enemy.key = game.frame; enemies[game.frame] = enemy; } scoreLabel.score = game.score; }); scoreLabel = new ScoreLabel(8, 8); game.rootScene.addChild(scoreLabel); }; game.start(); }; function rand(val){ return Math.floor(Math.random() * val); } PKJ4? style.cssbody { margin:0px }PKJNB index.htmlPKJ/|'index.jsPKJ4? ,style.cssPKf