1) Create the MovieClips for a Shooter, a Enemy and a Bullet in the library
2) Set the Linkage for the MovieClips in the library, make sure the names are precise
3) Create a text box, set it to Dynamic Text and name it score_txt
4) Copy and paste the code in the root frame
5) Test Movie
6) For version two, create the MovieClips for EnemyBullet and GameOver in the library
7) Copy and paste to overwrite the code in the root frame
Version 1:
Version 2://Copy the code and put it on the framevar interval_dur = 50;var shooter_mc = createMC("Shooter", 400, 500);_root.bullet_speed = 90;_root.move_speed = 20;_root.enemy_move_speed = 10;_root.stage_width = 800;_root.spawn_counter = 0;_root.spawn_max = 15;_root.score_counter_max = 10;function createMC(id:String, x_loc:Number, y_loc:Number):MovieClip {var i = _root.getNextHighestDepth();var mc:MovieClip = _root.attachMovie(id, "_mc"+i+"_", i, {_x:x_loc, _y:y_loc});return mc;}shooter_mc.onEnterFrame = function() {if (Key.isDown(Key.LEFT)) {this._x -= _root.move_speed;}if (this._x<0) {this._x = 0;}if (Key.isDown(Key.RIGHT)) {this._x += _root.move_speed;}if (this._x>_root.stage_width) {this._x = _root.stage_width;}if (Key.isDown(Key.SPACE) && _root.bullet_mc == undefined) {_root.bullet_mc = createMC("Bullet", this._x, this._y);_root.bullet_mc.onUnload = function() {if (_root.bullet_mc == this) {_root.bullet_mc = undefined;}};_root.bullet_mc.onEnterFrame = function() {if (this._y<=0) {removeMovieClip(this);return;}for (i=0; i<_root.bullet_speed; i++) {this._y--;for (var j in _root.enemy_list) {var target_mc:MovieClip = _root.enemy_list[j];if (target_mc.hitTest(this)) {_root.score++;_root.score_txt.text = _root.score;removeMovieClip(target_mc);removeMovieClip(this);}}}};}};function updateTimer():Void {_root.spawn_counter++;if (_root.spawn_counter>=_root.spawn_max) {_root.spawn_counter -= _root.spawn_max;var r = random(2)*_root.stage_width;var enemy_mc = createMC("Enemy", r, random(200));enemy_mc.speed = _root.enemy_move_speed+random(10);if (r != 0) {enemy_mc.speed = -enemy_mc.speed;}_root.enemy_list.push(enemy_mc);enemy_mc.onEnterFrame = function() {this._x += this.speed;if (this._x<-50 || this._x>_root.stage_width+50) {removeMovieClip(this);}};}}_root.score_txt.text = "0";_root.score = 0;_root.enemy_list = new Array();var intervalID:Number = setInterval(updateTimer, interval_dur);stop();
var interval_dur = 50;_root.shooter_mc = createMC("Shooter", 400, 500);_root.bullet_speed = 90;_root.move_speed = 20;_root.enemy_move_speed = 10;_root.stage_width = 800;_root.spawn_counter = 0;_root.spawn_max = 15;_root.score_counter_max = 10;function createMC(id:String, x_loc:Number, y_loc:Number):MovieClip {var i = _root.getNextHighestDepth();var mc:MovieClip = _root.attachMovie(id, "_mc"+i+"_", i, {_x:x_loc, _y:y_loc});return mc;}_root.shooter_mc.onEnterFrame = function() {if (Key.isDown(Key.LEFT)) {this._x -= _root.move_speed;}if (this._x<0) {this._x = 0;}if (Key.isDown(Key.RIGHT)) {this._x += _root.move_speed;}if (this._x>_root.stage_width) {this._x = _root.stage_width;}if (Key.isDown(Key.SPACE) && _root.bullet_mc == undefined) {_root.bullet_mc = createMC("Bullet", this._x, this._y);_root.bullet_mc.onUnload = function() {if (_root.bullet_mc == this) {_root.bullet_mc = undefined;}};_root.bullet_mc.onEnterFrame = function() {if (this._y<=0) {removeMovieClip(this);return;}for (i=0; i<_root.bullet_speed; i++) {this._y--;for (var j in _root.enemy_list) {var target_mc:MovieClip = _root.enemy_list[j];if (target_mc.hitTest(this)) {_root.score++;_root.score_txt.text = _root.score;removeMovieClip(target_mc);removeMovieClip(this);}}}};}};function updateTimer():Void {_root.spawn_counter++;if (_root.spawn_counter>=_root.spawn_max) {_root.spawn_counter -= _root.spawn_max;var r = random(2)*_root.stage_width;var enemy_mc = createMC("Enemy", r, random(200));enemy_mc.speed = _root.enemy_move_speed+random(10);if (r != 0) {enemy_mc.speed = -enemy_mc.speed;}_root.enemy_list.push(enemy_mc);enemy_mc.onEnterFrame = function() {this._x += this.speed;if (this._x<-50 || this._x>_root.stage_width+50) {removeMovieClip(this);}if (random(33) == 0) {var bullet_mc = createMC("EnemyBullet", this._x, this._y);bullet_mc.onEnterFrame = function() {this._y += 10;if (this._y>600) {removeMovieClip(this);return;}if (this.hitTest(_root.shooter_mc)) {createMC("GameOver", _root.shooter_mc._x, _root.shooter_mc._y);removeMovieClip(_root.shooter_mc);removeMovieClip(this);return;}};}};}}_root.score_txt.text = "0";_root.score = 0;_root.enemy_list = new Array();var intervalID:Number = setInterval(updateTimer, interval_dur);stop();
No comments:
Post a Comment