PDA

View Full Version : NameSpaces - getting functionB to use a var from functionA



Bobafart
Mar 25th, 2007, 08:59 PM
I am trying to devise a scheme where if a user clicks divA and then clicks divB, divB will be able to use a var that was defined in the divA clicked function.



<div id="divA" onClick="selectAttacker()">your unit</div>

<div id="divB" onClick="doCombat()">enemy unit</div>

the user first selects an attacker and then does selects the enemy for the attacker to fire on...

how do I make it so that in the doCombat() function I am able to reference that divA was the unit responsible for causing damage to divB???

Bobafart
Mar 25th, 2007, 09:01 PM
I thought about using namespaces and public functions.. but it isn't working:




YAHOO.namespace('game.container');
YAHOO.game.container = function() {
var fn = function(e, id, param, o) {
// private function -- process arguments here

};
return {
init : function() {
// blah blah
},
selectAttacker : function(attackingUnitID, typeOfDmg) {
var attackingUnitID = attackingUnitID;
alert(attackingUnitID);

},
doCombat: function(targetUnit, typeOfDmg) {
alert(YAHOO.game.container.selectAttacker.attackingUnitID);

}
};
}();



my doCombat() alert() ends up showing the alert dialog box but it is empty... no errors either.

how would you recommend I tackle this problem? I don't have to use namespaces... please let me know how one function can use a var defined in another.

Bobafart
Mar 26th, 2007, 12:55 PM
anyone?