Monday, June 23, 2014

How to calculate jumping height

Assume that jumping mechanic is using Cartesian Projection on y-position

jump_init is the start jump speed
gravity_pull is the constant gravity pull (weight is ignored)

Assume that x is jump_init and y is gravity_pull

maximum jumping height = x * (x/y + 1) / 2

Sunday, May 18, 2014

Flappy Bird v2 source code

_root.flag_gameover = false;
function gameover() {
_root.flag_gameover = true;
_root.bg0_mc.stop();
_root.bg1_mc.stop();
_root.bg2_mc.stop();
}
function updateTimer():Void {
if (_root.flag_gameover) {
return;
}
var i:Number = _root.container_mc.getNextHighestDepth();
var mc:MovieClip;
mc = _root.container_mc.attachMovie("Pipe", "pipe_mc_" + i, i, {_x:550, _y:100 + random(150)});
_root.pipe_list.push(mc);
mc.onEnterFrame = function() {
if (_root.flag_gameover) {
return;
}
this._x -= 10;
if (this._x < -100) {
removeMovieClip(this);
}
if (this.hitbox_top_mc.hitTest(_root.bird_mc.hitbox_mc)) {
_root.gameover();
}
if (this.hitbox_btm_mc.hitTest(_root.bird_mc.hitbox_mc)) {
_root.gameover();
}
};
}
var intervalID:Number = setInterval(updateTimer, 1000);

Sunday, May 4, 2014

Vertical Shooter

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:
//Copy the code and put it on the frame
var 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();
Version 2:
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();

Simple Minigame - Electric Rod

Simple Minigame
1) Draw a maze using Brush Tool, then convert it into MovieClip
2) Create a new layer for buttons with 3 key frames
3) Create a start button at the first key frame
4) Create a end button at the second key frame
5) Pull in the custom cursor MovieClip into the second key frame
6) Create the third key frame for victory
7) Put a "stop();" at the first key frame
8) Copy the code into corresponding buttons and MovieClips

Source Code:
//Custom cursor
onClipEvent (load) {
this._x = _root._xmouse;
this._y = _root._ymouse;
Mouse.hide();
startDrag(this);
}
onClipEvent (unload) {
Mouse.show();
}

//Maze
onClipEvent (mouseMove) {
if (_root.bln && this.hitTest(_root._xmouse, _root._ymouse, true)) {
_root.prevFrame();
}
}
onClipEvent (enterFrame) {
if (_root.bln && this.hitTest(_root._xmouse, _root._ymouse, true)) {
_root.prevFrame();
}
}

//Start button
on (release) {
_root.bln = true;
_root.nextFrame();
}

//End button
on (rollOver) {
nextFrame();
}