こんにちは。きんくまです。
この間のF-siteの用にflixelでつくった弾幕ゲームのデモを少し修正できたのでアップします。
デモのときは、iPadで使えるようにキーボード操作でなく、タッチ操作にしてました。
■あそびかた
キーボードの上下左右で操作。3回やられたらゲームオーバー。
クリアしてもループするだけです。すみません、、。
あれ? ブラウザでみると、けっこうカクつきますね。
swfそのまま見ると、もうちっとなめらかなんだけど。
ソースコードも書いておきます。
flixelが面倒なことをやってくれてるので、わりと短め?なのかな。
自機爆発のビットマップデータはflixelについてたflixelのロゴデータですw
shooting_demo.as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package { import flash.events.Event; import org.flixel.FlxGame; [SWF(width= "480" , height= "640" , backgroundColor= "#000000" , frameRate= "60" )] public class shooting_demo extends FlxGame { public function shooting_demo() { super ( 480 * 0.5 , 640 * 0.5 , MenuState, 2 , 60 , 60 ); } override protected function create(FlashEvent:Event): void { super .create(FlashEvent); } } } |
MenuState.as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package { import org.flixel.FlxButton; import org.flixel.FlxG; import org.flixel.FlxState; import org.flixel.FlxText; public class MenuState extends FlxState { private var _firstChar:FlxText; private var _title:FlxText; private var _startButton:FlxButton; public function MenuState() { super (); } public override function create(): void { super .create(); FlxG.mouse.show(); _startButton = new FlxButton(FlxG.width * 0.5 - 40 , 160 , "START" , onStartClick); add(_startButton); _title = new FlxText( 54 , 90 , 200 , "hooting!!" ); _title.size = 28 ; add(_title); _firstChar = new FlxText( 27 , 80 , 40 , "S" ); _firstChar.color = 0xFFFFCC33 ; _firstChar.size = 46 ; add(_firstChar); } public override function destroy(): void { super .destroy(); _startButton = null ; _title = null ; } private function onStartClick(): void { FlxG.mouse.hide(); FlxG.switchState( new PlayState()); } } } |
PlayState.as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | package { import flashx.textLayout.elements.BreakElement; import org.flixel.FlxEmitter; import org.flixel.FlxG; import org.flixel.FlxGroup; import org.flixel.FlxPoint; import org.flixel.FlxSprite; import org.flixel.FlxState; import org.flixel.FlxText; import org.flixel.FlxTilemap; public class PlayState extends FlxState { private var _player:FlxSprite; private var _enemy:FlxSprite; private var _bullets:FlxGroup; private var _enemyBullets:FlxGroup; private var _shotCnt: int = 0 ; private var _enemyShotCnt: int = 0 ; private var _enemyHealthBar:FlxSprite; private var _enemyHelathBarBase:FlxSprite; private var _enemyShootOffsetRad: Number = 0 ; private var _scoreTxt:FlxText; private var _score: int ; private var _isGameOver: Boolean = false ; private var _gameOverText:FlxText; private var _pressZkeyText:FlxText; private const MAX_ENEMY_HEALTH: int = 100 ; private const PLAYER_MAX_HEALTH: int = 3 ; private var _playerExplosion:FlxEmitter; [Embed(source= "org/flixel/data/logo.png" )] private var LogoBmd:Class; override public function create(): void { _player = new FlxSprite(FlxG.width * 0.5 , FlxG.height - 50 ); _player.makeGraphic( 8 , 8 , 0xffff0000 ); _player.maxVelocity.x = 120 ; _player.maxVelocity.y = 120 ; _player.drag.x = _player.maxVelocity.x * 4 ; _player.drag.y = _player.maxVelocity.y * 4 ; _player.health = PLAYER_MAX_HEALTH; _player.immovable = true ; add(_player); _enemy = new FlxSprite(FlxG.width * 0.5 , 40 ); _enemy.makeGraphic( 30 , 8 , 0xff00ff00 ); _enemy.health = MAX_ENEMY_HEALTH; _enemy.velocity.y = 0 ; _enemy.acceleration.y = 0 ; _enemy.immovable = true ; _enemy.acceleration.x = 10 ; _enemy.maxVelocity.x = 100 ; add(_enemy); var i: int ; _bullets = new FlxGroup(); for (i = 0 ; i < 60 ; i++){ var bullet:FlxSprite = new FlxSprite( 100 , 100 ); bullet.makeGraphic( 2 , 5 , 0xffffffff ); bullet.exists = false ; _bullets.add(bullet); } add(_bullets); _enemyBullets = new FlxGroup(); for (i = 0 ; i < 300 ; i ++){ var enemyBullet:FlxSprite = new FlxSprite( 100 , 100 ); enemyBullet.makeGraphic( 4 , 4 , 0xffffff00 ); enemyBullet.exists = false ; enemyBullet.immovable = true ; _enemyBullets.add(enemyBullet); } add(_enemyBullets); _enemyHelathBarBase = new FlxSprite( 10 , 20 ); _enemyHelathBarBase.makeGraphic(FlxG.width - 20 , 3 , 0xffffffff ); add(_enemyHelathBarBase); _enemyHealthBar = new FlxSprite( 10 , 20 ); _enemyHealthBar.makeGraphic((FlxG.width - 20 ), 3 , 0xffff0000 ); add(_enemyHealthBar); _scoreTxt = new FlxText( 10 , 2 , 100 ); add(_scoreTxt); _score = 0 ; _gameOverText = new FlxText( 80 , 120 , 100 , "GAME OVER" ); _gameOverText.size = 24 ; _gameOverText.visible = false ; add(_gameOverText); _pressZkeyText = new FlxText( 88 , 200 , 120 , "PRESS Z KEY" ); _pressZkeyText.size = 8 ; _pressZkeyText.visible = false ; add(_pressZkeyText); _playerExplosion = new FlxEmitter(FlxG.width * 0.5 , FlxG.height * 0.5 ); _playerExplosion.setSize( 30 , 30 ); _playerExplosion.setXSpeed(- 300 , 300 ); _playerExplosion.setYSpeed(- 300 , 300 ); _playerExplosion.setRotation(- 720 , 720 ); _playerExplosion.makeParticles(LogoBmd, 650 , 32 , true , 0 ); add(_playerExplosion); } override public function update(): void { var bullet:FlxSprite; _player.acceleration.x = 0 ; _player.acceleration.y = 0 ; if (!_player.exists && FlxG.keys.Z){ _gameOverText.visible = false ; _pressZkeyText.visible = false ; _player.health = PLAYER_MAX_HEALTH; _player.exists = true ; } if (FlxG.keys.RIGHT){ _player.acceleration.x = _player.maxVelocity.x * 3 ; } if (FlxG.keys.LEFT){ _player.acceleration.x = -_player.maxVelocity.x * 3 ; } if (FlxG.keys.UP){ _player.acceleration.y = -_player.maxVelocity.y * 3 ; } if (FlxG.keys.DOWN){ _player.acceleration.y = _player.maxVelocity.y * 3 ; } if (_player.exists && _shotCnt > 2 ){ for each (bullet in _bullets.members){ if (!bullet.exists){ bullet.x = _player.x + 3 ; bullet.y = _player.y; bullet.velocity.x = 0 ; bullet.velocity.y = - 200 ; bullet.exists = true ; _shotCnt = 0 ; break ; } else { } } } else { _shotCnt++; } var enemyBullet:FlxSprite; var enemyBulletShoootCnt: int = 0 ; var enemyBulletRad: Number ; var direction: Number = 90 ; var enmbulletMax: int = 8 ; var dRad: Number = Math.PI * (enmbulletMax - 1 ) * 2 / 180.0 ; var currentRad: Number = Math.PI * direction / 180.0 - dRad * (enmbulletMax- 1 ) * 0.5 + Math.sin(_enemyShootOffsetRad) * 0.5 ; var enmBulletVelocity: Number = 50 ; if (_enemyShotCnt > 15 ){ for each (enemyBullet in _enemyBullets.members){ if (!enemyBullet.exists){ enemyBullet.x = _enemy.width * 0.5 + _enemy.x; enemyBullet.y = _enemy.height + _enemy.y; enemyBullet.velocity.x = Math.cos(currentRad) * enmBulletVelocity; enemyBullet.velocity.y = Math.sin(currentRad) * enmBulletVelocity; currentRad += dRad; enemyBulletShoootCnt++; enemyBullet.exists = true ; if (enemyBulletShoootCnt >= enmbulletMax){ break ; } } } _enemyShotCnt = 0 ; _enemyShootOffsetRad += 0.2 ; if (_enemyShootOffsetRad > Math.PI * 2 ){ _enemyShootOffsetRad -= Math.PI * 2 ; } } else { _enemyShotCnt++; } super .update(); for each (bullet in _bullets.members){ if (bullet.exists && bullet.y < 0 ){ bullet.exists = false ; bullet.velocity.y = 0 ; } } for each (enemyBullet in _enemyBullets.members){ if (enemyBullet.exists){ if (enemyBullet.y > FlxG.height || enemyBullet.y < 0 || enemyBullet.x < 0 || enemyBullet.x > FlxG.width){ enemyBullet.exists = false ; enemyBullet.velocity.x = 0 ; enemyBullet.velocity.y = 0 ; } } } var enemyMoveMaxX: int = 20 ; var enemyHalfW: int = _enemy.width * 0.5 ; if (_enemy.x + enemyHalfW < enemyMoveMaxX){ _enemy.velocity.x = 0 ; _enemy.acceleration.x = 10 ; _enemy.x = enemyMoveMaxX - enemyHalfW; } if (_enemy.x + enemyHalfW > FlxG.width - enemyMoveMaxX){ _enemy.velocity.x = 0 ; _enemy.acceleration.x = - 10 ; _enemy.x = FlxG.width - enemyMoveMaxX - enemyHalfW; } FlxG.overlap(_enemy, _bullets, onEnemyWithBulletsOverlap); FlxG.overlap(_player, _enemyBullets, onPlayerWithEnemyBulletsOverlap); updateEnemyHealthBar(); if (_player.x < 0 ){ _player.x = 0 ; } if (_player.x > FlxG.width){ _player.x = FlxG.width; } if (_player.y < 0 ){ _player.y = 0 ; } if (_player.y > FlxG.height){ _player.y = FlxG.height; } _scoreTxt.text = 'SCORE: ' + _score.toString(); } private function updateEnemyHealthBar(): void { var barScale: Number = _enemy.health / MAX_ENEMY_HEALTH; if (barScale * (FlxG.width - 20 ) >= 1 ){ _enemyHealthBar.makeGraphic((FlxG.width - 20 ) * barScale, 3 , 0xffff0000 ); _enemyHealthBar.exists = true ; } else { _enemyHealthBar.exists = false ; } } override public function destroy(): void { super .destroy(); _player = null ; } private function onEnemyWithBulletsOverlap(enemy:FlxSprite, bullet:FlxSprite): void { enemy.health -= 1 ; enemy.velocity.y = 0 ; enemy.acceleration.y = 0 ; enemy.flicker( 0.2 ); bullet.exists = false ; if (enemy.health < 0 ){ enemy.health = MAX_ENEMY_HEALTH; enemy.x = 40 ; enemy.y = 40 ; _score += 100000 ; } _score += 100 ; } private function onPlayerWithEnemyBulletsOverlap(player:FlxSprite, enemyBullet:FlxSprite): void { if (_player.flickering){ return ; } _player.health--; _player.flicker( 1.0 ); enemyBullet.exists = false ; if (_player.health <= 0 ){ _player.kill(); _playerExplosion.x = _player.x; _playerExplosion.y = _player.y; _playerExplosion.start( true , 3 ); _gameOverText.visible = true ; _pressZkeyText.visible = true ; } } } } |
■ 自作iPhoneアプリ 好評発売中!
・フォルメモ - シンプルなフォルダつきメモ帳
・ジッピー電卓 - 消費税や割引もサクサク計算!
■ LINEスタンプ作りました!
毎日使える。とぼけたウサギ