Referencing movie clips from within an actionscript class
- by Ant
Hi all,
I have been given the task of adding a scoring system to various flash games. This simply involves taking input, adding functionality such as pausing and replaying and then outputting the score, time left etc. at the end.
I've so far successfully edited two games. Both these games used the "actions" code on frames. The latest game I'm trying to do uses an actionscript class which makes it both easier and harder.
I'm not very adept at flash at all, but I've worked it out so far. I've added various movie clips that are to be used for displaying the pause screen background, buttons for replaying etc.
I've been showing and hiding these using:
back._visible = true;
//movie clip, instance of back (back.png)
I doubt it's best practice, but it's quick and has been working. However, now with the change of coding style to classes, this doesn't seem to work. I kinda understand why, but I'm now unsure how to hide/show these elements.
Any help would be greatly appreciated :) I've attached the modified AS.
class RivalOrbs extends MovieClip
{
var infinite_levels, orbs_start, orbs_inc, orbs_per_level, show_timer, _parent, one_time_per_level, speed_start, speed_inc_percent, max_speed, percent_starting_on_wrong_side, colorize, colors, secs_per_level;
function RivalOrbs()
{
super();
mc = this;
this.init();
} // End of the function
function get_num_orbs()
{
if (infinite_levels)
{
return (orbs_start + (level - 1) * orbs_inc);
}
else if (level > orbs_per_level.length)
{
return (0);
}
else
{
return (orbs_per_level[level - 1]);
} // end else if
} // End of the function
function get_timer_str(secs)
{
var _loc2 = Math.floor(secs / 60);
var _loc1 = secs % 60;
return ((_loc2 > 0 ? (_loc2) : ("0")) + ":" + (_loc1 >= 10 ? (_loc1) : ("0" + _loc1)));
} // End of the function
function frame()
{
//PLACE PAUSE CODE HERE
if (!Key.isDown(80) and !Key.isDown(Key.ESCAPE)) {
_root.offKey = true;
} else if (Key.isDown(80) or Key.isDown(Key.ESCAPE)) {
if (_root.offKey and _root.game_mode == "play") {
_root.game_mode = "pause";
/*
back._visible = true;
btn_resume._visible = true;
btn_exit._visible = true;
txt_pause._visible = true;
*/
}
else if (_root.offKey and _root.game_mode == "pause") {
_root.game_mode = "play";
}
_root.offKey = false;
}
if (_root.game_mode == "pause" or paused)
{
return;
} else {
/*
back._visible = false;
btn_resume._visible = false;
btn_exit._visible = false;
txt_pause._visible = false;
*/
}
if (show_timer && total_secs != -1 || show_timer && _parent.timesup)
{
_loc7 = total_secs - Math.ceil((getTimer() - timer) / 1000);
var diff = oldSeconds - (_loc7 + additional);
if (diff > 1)
additional = additional + diff;
_loc7 = _loc7 + additional;
oldSeconds = _loc7;
trace(oldSeconds);
mc.timer_field.text = this.get_timer_str(Math.max(0, _loc7));
if (_loc7 <= -1 || _parent.timesup)
{
if (one_time_per_level)
{
_root.gotoAndPlay("Lose");
}
else
{
this.show_dialog(false);
return;
} // end if
} // end if
} // end else if
var _loc9 = _root._xmouse;
var _loc8 = _root._ymouse;
var _loc6 = {x: _loc9, y: _loc8};
mc.globalToLocal(_loc6);
_loc6.y = Math.max(-mc.bg._height / 2 + gap / 2, _loc6.y);
_loc6.y = Math.min(mc.bg._height / 2 - gap / 2, _loc6.y);
mc.wall1._y = _loc6.y - gap / 2 - mc.wall1._height / 2;
mc.wall2._y = _loc6.y + gap / 2 + mc.wall1._height / 2;
var _loc5 = true;
for (var _loc4 = 0; _loc4 < this.get_num_orbs(); ++_loc4)
{
var _loc3 = mc.stage["orb" + _loc4];
_loc3.x_last = _loc3._x;
_loc3.y_last = _loc3._y;
_loc3._x = _loc3._x + _loc3.x_speed;
_loc3._y = _loc3._y + _loc3.y_speed;
if (_loc3._x < l_thresh)
{
_loc3.x_speed = _loc3.x_speed * -1;
_loc3._x = l_thresh + (l_thresh - _loc3._x);
_loc3.gotoAndPlay("hit");
} // end if
if (_loc3._x > r_thresh)
{
_loc3.x_speed = _loc3.x_speed * -1;
_loc3._x = r_thresh - (_loc3._x - r_thresh);
_loc3.gotoAndPlay("hit");
} // end if
if (_loc3._y < t_thresh)
{
_loc3.y_speed = _loc3.y_speed * -1;
_loc3._y = t_thresh + (t_thresh - _loc3._y);
_loc3.gotoAndPlay("hit");
} // end if
if (_loc3._y > b_thresh)
{
_loc3.y_speed = _loc3.y_speed * -1;
_loc3._y = b_thresh - (_loc3._y - b_thresh);
_loc3.gotoAndPlay("hit");
} // end if
if (_loc3.x_speed > 0)
{
if (_loc3._x >= m1_thresh && _loc3.x_last < m1_thresh || _loc3._x >= m1_thresh && _loc3._x <= m2_thresh)
{
if (_loc3._y <= mc.wall1._y + mc.wall1._height / 2 || _loc3._y >= mc.wall2._y - mc.wall2._height / 2)
{
_loc3.x_speed = _loc3.x_speed * -1;
_loc3._x = m1_thresh - (_loc3._x - m1_thresh);
_loc3.gotoAndPlay("hit");
} // end if
} // end if
}
else if (_loc3._x <= m2_thresh && _loc3.x_last > m2_thresh || _loc3._x >= m1_thresh && _loc3._x <= m2_thresh)
{
if (_loc3._y <= mc.wall1._y + mc.wall1._height / 2 || _loc3._y >= mc.wall2._y - mc.wall2._height / 2)
{
_loc3.x_speed = _loc3.x_speed * -1;
_loc3._x = m2_thresh + (m2_thresh - _loc3._x);
_loc3.gotoAndPlay("hit");
} // end if
} // end else if
if (_loc3.side == 1 && _loc3._x > 0)
{
_loc5 = false;
} // end if
if (_loc3.side == 2 && _loc3._x < 0)
{
_loc5 = false;
} // end if
} // end of for
if (_loc5)
{
this.end_level();
} // end if
} // End of the function
function colorize_hex(mc, hex)
{
var _loc4 = hex >> 16;
var _loc5 = (hex ^ hex >> 16 << 16) >> 8;
var _loc3 = hex >> 8 << 8 ^ hex;
var _loc2 = new flash.geom.ColorTransform(0, 0, 0, 1, _loc4, _loc5, _loc3, 0);
mc.transform.colorTransform = _loc2;
} // End of the function
function tint_hex(mc, hex, amount)
{
var _loc4 = hex >> 16;
var _loc5 = hex >> 8 & 255;
var _loc3 = hex & 255;
this.tint(mc, _loc4, _loc5, _loc3, amount);
} // End of the function
function tint(mc, r, g, b, amount)
{
var _loc4 = 100 - amount;
var _loc1 = new Object();
_loc1.ra = _loc1.ga = _loc1.ba = _loc4;
var _loc2 = amount / 100;
_loc1.rb = r * _loc2;
_loc1.gb = g * _loc2;
_loc1.bb = b * _loc2;
var _loc3 = new Color(mc);
_loc3.setTransform(_loc1);
} // End of the function
function get_num_levels()
{
if (infinite_levels)
{
return (Number.MAX_VALUE);
}
else
{
return (orbs_per_level.length);
} // end else if
} // End of the function
function end_level()
{
_global.inputTimeAvailable = _global.inputTimeAvailable - (60 - oldSeconds);
++level;
_parent.levelOver = true;
if (level <= this.get_num_levels())
{
this.show_dialog(true);
}
else
{
_root.gotoAndPlay("Win");
} // end else if
} // End of the function
function get_speed()
{
var _loc3 = speed_start;
for (var _loc2 = 0; _loc2 < level - 1; ++_loc2)
{
_loc3 = _loc3 + _loc3 * (speed_inc_percent / 100);
} // end of for
return (Math.min(_loc3, Math.max(max_speed, speed_start)));
} // End of the function
function init_orbs()
{
var _loc6 = this.get_speed();
var _loc7 = Math.max(1, Math.ceil(this.get_num_orbs() * (percent_starting_on_wrong_side / 100)));
for (var _loc3 = 0; _loc3 < this.get_num_orbs(); ++_loc3)
{
var _loc2 = null;
if (_loc3 % 2 == 0)
{
_loc2 = mc.stage.attachMovie("Orb1", "orb" + _loc3, _loc3);
_loc2.side = 1;
if (colorize && color1 != -1)
{
this.colorize_hex(_loc2.orb.bg, color1);
} // end if
_loc2._x = Math.random() * (mc.bg._width * 4.000000E-001) - mc.bg._width * 2.000000E-001 - mc.bg._width / 4;
}
else
{
_loc2 = mc.stage.attachMovie("Orb2", "orb" + _loc3, _loc3);
_loc2.side = 2;
if (colorize && color2 != -1)
{
this.colorize_hex(_loc2.orb.bg, color2);
} // end if
_loc2._x = Math.random() * (mc.bg._width * 4.000000E-001) - mc.bg._width * 2.000000E-001 + mc.bg._width / 4;
} // end else if
_loc2._width = _loc2._height = orb_w;
_loc2._y = Math.random() * (mc.bg._height * 8.000000E-001) - mc.bg._height * 4.000000E-001;
if (_loc3 < _loc7)
{
_loc2._x = _loc2._x * -1;
} // end if
var _loc5 = Math.random() * 60;
var _loc4 = _loc5 / 180 * 3.141593E+000;
_loc2.x_speed = Math.cos(_loc4) * _loc6;
_loc2.y_speed = Math.sin(_loc4) * _loc6;
if (Math.random() >= 5.000000E-001)
{
_loc2.x_speed = _loc2.x_speed * -1;
} // end if
if (Math.random() >= 5.000000E-001)
{
_loc2.y_speed = _loc2.y_speed * -1;
} // end if
} // end of for
} // End of the function
function init_colors()
{
if (colorize && colors.length >= 2)
{
color1 = colors[Math.floor(Math.random() * colors.length)];
for (color2 = colors[Math.floor(Math.random() * colors.length)]; color2 == color1; color2 = colors[Math.floor(Math.random() * colors.length)])
{
} // end of for
this.tint_hex(mc.side1, color1, 40);
this.tint_hex(mc.side2, color2, 40);
}
else
{
color1 = -1;
color2 = -1;
} // end else if
} // End of the function
function get_total_secs()
{
if (show_timer)
{
if (secs_per_level.length > 0)
{
if (level > secs_per_level.length)
{
return (secs_per_level[secs_per_level.length - 1]);
}
else
{
return (secs_per_level[level - 1]);
} // end if
} // end if
} // end else if
return (-1);
} // End of the function
function start_level()
{
trace ("start_level");
_parent.timesup = false;
_parent.levelOver = false;
_parent.times_up_comp.start_timer();
this.init_orbs();
mc.level_field.text = "LEVEL " + level;
total_secs = _global.inputTimeAvailable;
if (total_secs > 60)
total_secs = 60;
timer = getTimer();
paused = false;
mc.dialog.gotoAndPlay("off");
} // End of the function
function clear_orbs()
{
for (var _loc2 = 0; mc.stage["orb" + _loc2]; ++_loc2)
{
mc.stage["orb" + _loc2].removeMovieClip();
} // end of for
} // End of the function
function show_dialog(new_level)
{
mc.back._visible = false;
trace("yes");
paused = true;
if (new_level)
{
this.init_colors();
} // end if
this.clear_orbs();
mc.dialog.gotoAndPlay("level");
if (!new_level || _parent.timesup)
{
mc.dialog.level_top.text = "Time\'s Up!";
/*
dyn_line1.text = "Goodbye " + _global.inputName + "!";
dyn_line2.text = "You scored " + score;
//buttons
if (_global.inputTimeAvailable > 60)
btn_replay._visible = true;
btn_resume._visible = false;
btn_exit._visible = false;
txt_pause._visible = false;
sendInfo = new LoadVars();
sendLoader = new LoadVars();
sendInfo.game_name = 'rival_orbs';
sendInfo.timeavailable = _global.inputTimeAvailable;
if (sendInfo.timeavailable < 0)
sendInfo.timeavailable = 0;
sendInfo.id = _global.inputId;
sendInfo.score = level*_global.inputFactor;
sendInfo.directive = 'record';
//sendInfo.sendAndLoad('ncc1701e.aspx', sendLoader, "GET");
sendInfo.sendAndLoad('http://keyload.co.uk/output.php', sendLoader, "POST");
*/
}
else if (level > 1)
{
mc.dialog.level_top.text = "Next Level:";
}
else
{
mc.dialog.level_top.text = "";
} // end else if
mc.dialog.level_num.text = "LEVEL " + level;
mc.dialog.level_mid.text = "Number of Orbs: " + this.get_num_orbs();
_root.max_level = level;
var _this = this;
mc.dialog.btn.onRelease = function ()
{
_this.start_level();
};
} // End of the function
function init()
{
var getInfo = new LoadVars();
var getLoader = new LoadVars();
getInfo.directive = "read";
getInfo.sendAndLoad('http://keyload.co.uk/input.php', getLoader, "GET");
getLoader.onLoad = function (success)
{
if (success) {
_global.inputId = this.id;
_global.inputTimeAvailable = this.timeavailable;
_global.inputFactor = this.factor;
_global.inputName = this.name;
} else {
trace("Failed");
}
}
_root.game_mode = "play";
/*
back._visible = false;
btn_exit._visible = false;
btn_replay._visible = false;
btn_resume._visible = false;
txt_pause._visible = false;
*/
l_thresh = -mc.bg._width / 2 + orb_w / 2;
t_thresh = -mc.bg._height / 2 + orb_w / 2;
r_thresh = mc.bg._width / 2 - orb_w / 2;
b_thresh = mc.bg._height / 2 - orb_w / 2;
m1_thresh = -wall_w / 2 - orb_w / 2;
m2_thresh = wall_w / 2 + orb_w / 2;
this.show_dialog(true);
mc.onEnterFrame = frame;
} // End of the function
var mc = null;
var orb_w = 15;
var wall_w = 2;
var l_thresh = 0;
var r_thresh = 0;
var t_thresh = 0;
var b_thresh = 0;
var m1_thresh = 0;
var m2_thresh = 0;
var color1 = -1;
var color2 = -1;
var level = 1;
var total_secs = 30;
var gap = 60;
var timer = 0;
var additional = 0;
var oldSeconds = 0;
var paused = true;
var _loc7 = 0;
} // End of Class