Ostatnio przerabialem kalkulator dla mojej strony i oczywiście utknąłem w połowie roboty. Kalkulator działa jak należy, ale mam drobny problem z wyświetlaczem. Chodzi o to, że działa tylko wtedy, kiedy jest w pierwszej, głównej warstwie filmu.
Używam tego skryptu:
Kod: Zaznacz cały
float register = 0;
float memory = 0;
string display = "0";
string operator = "";
bool display_flag = false;
bool operator_flag = false;
/////////
function Calculate ()
{
if (operator_flag){
if (operator == "+") register += Number (display);
if (operator == "-") register -= Number (display);
if (operator == "*") register *= Number (display);
if (operator == "/"){
if (register == 0){
display = "Error";
display_flag = false;
return;}
register /= Number (display);}
display = String (register);}
else register = Number (display);
if (display.length > 10) display = display.slice (0, 10);
display_flag = false;
}
/////////
function AddNumber (number)
{
if (!display_flag) display = String (number);
else display += String (number);
display_flag = true;
}
/////////
function Decimal ()
{
if (display_flag){
var i = display.indexOf (".");
if (i == -1) display += ".";}
else {
display = "0.";
display_flag = true;}
}
/////////
function Operate (op)
{
Calculate ();
operator = op;
operator_flag = true;
}
/////////
function Equals ()
{
Calculate ();
operator = "";
operator_flag = false;
}
/////////
function MemoryAdd ()
{
Equals ();
memory += Number (display);
}
/////////
function MemoryReturn ()
{
display = String (memory);
display_flag = false;
}
/////////
function Cancel ()
{
display = "0";
display_flag = false;
}
/////////
function CancelEverything ()
{
register = 0;
display = "0";
display_flag = false;
operator = "";
operator_flag = false;
memory = 0;
}
//////////
//////////Button =
-> Call calculator
Equals()
I każdy przycisk działa bez względu na warstwe filmu, ale tylko wtedy, jeśli wyświetlacz(display) jest w pierwszej.
Co robię źle? Co mam poprawić aby wyświetlacz działał w innych warstwach?
