package com.DanceAssassin { /////////////////////////////////////////////////////////////////////////// // Doug Macdonald // http://www.dougwillsave.us // // Music engine: Contains a short song, split into 6 clips of 4 seconds // each. Keeps track of how much time has passed and, when necessary, // beings playing the next clip of the song. Additionally, this class // keeps track of what 'beat' of the song is currently playing; other parts // of the game logic reads this value in order to determine when actions // are performed in according with the beat. // // Because the song is split into short clips, the music will never be // out of sync with the gameplay for any longer than 4 seconds at a time, // eliminating a potential major problem when creating a rhythm game // in Flash. // // Created as part of a flash prototype developed for a class at DigiPen // Institute of Technology. // // © 2010 DigiPen (USA) Corporation /////////////////////////////////////////////////////////////////////////// import org.flixel.*; public class Music extends FlxSprite { [Embed(source = '../../data/beat_1.mp3')] private var MusBeat1:Class; [Embed(source = '../../data/beat_2.mp3')] private var MusBeat2:Class; [Embed(source = '../../data/beat_3.mp3')] private var MusBeat3:Class; [Embed(source = '../../data/beat_4.mp3')] private var MusBeat4:Class; [Embed(source = '../../data/beat_5.mp3')] private var MusBeat5:Class; [Embed(source = '../../data/beat_6.mp3')] private var MusBeat6:Class; private var Song:Array = [1, 1, 2, 3, 2, 3, 4, 5, 4, 5, 6, 6, 4, 5, 4, 5]; private var _time:Number; public var _ttb:Number; private var _index:int; public var _beat:int; public function Music() : void { _time = 0; _ttb = 0; _index = 0; _beat = 0; } override public function update() : void { _time += FlxG.elapsed; if (_time >= 4.0) { _time = 0; _ttb = 0; _beat = 0; _index++; if (_index == Song.length) _index = 0; StartTrack(Song[_index]); } else if (_ttb > 0.5) { _ttb -= 0.5; } else if (_time > 3.4) _beat = 7; else if (_time > 2.9) _beat = 6; else if (_time > 2.4) _beat = 5; else if (_time > 1.9) _beat = 4; else if (_time > 1.4) _beat = 3; else if (_time > 0.9) _beat = 2; else if (_time > 0.4) _beat = 1; } public function StartTrack(index:int): void { switch (index) { case 1: FlxG.play(MusBeat1); break; case 2: FlxG.play(MusBeat2); break; case 3: FlxG.play(MusBeat3); break; case 4: FlxG.play(MusBeat4); break; case 5: FlxG.play(MusBeat5); break; case 6: FlxG.play(MusBeat6); break; } } } }