-
1. Re: Array value Equal to string (if statement)
Ned Murphy Dec 16, 2012 8:06 AM (in response to LukeElChul)1 person found this helpfulIn general, you can often use looping and bracket notation to reduce code that is repetitive. In your case that applies to some degree, though the first conditional (if) of each group varies from one to the next which makes it a bit more difficult to whittle down to a generic solution that can handle all with the same code. The second half of the conditionals (else if) appears to be nearly identical for all three sets so that could be managed fairly easily.
So you need to try to get the first half of your conditionals to be similar for all, or otherwise you need to build in some specific conditionals within them to deal with logic specific to certain wslot# values
-
2. Re: Array value Equal to string (if statement)
LukeElChul Dec 16, 2012 9:14 AM (in response to Ned Murphy)I simplified it down to this
function useWeapon(weaponIndex:Number, weaponSelected:String, Type:String, Frame:Number) {
if (_root.wslot1 == weaponSelected && weaponIndex == 1) {
_root.wslot1full = false;
_root.wslot1 = "Nothing";
_root.cam.character.ws1.gotoAndStop(1);
_root.HitOrShoot = Type;
_root.mc.weapon.gotoAndStop(Frame);
_root.weapon = weaponSelected;
}
if (_root.wslot2 == weaponSelected && weaponIndex == 2) {
_root.wslot2full = false;
_root.wslot2 = "Nothing";
_root.cam.character.ws2.gotoAndStop(1);
_root.HitOrShoot = Type;
_root.mc.weapon.gotoAndStop(Frame);
_root.weapon = weaponSelected;
}
if (_root.wslot3 == weaponSelected && weaponIndex == 3) {
_root.wslot3full = false;
_root.wslot3 = "Nothing";
_root.cam.character.ws3.gotoAndStop(1);
_root.HitOrShoot = Type;
_root.mc.weapon.gotoAndStop(Frame);
_root.weapon = weaponSelected;
}
}
but is there away to change _root.wslot3 to somthing like wslots[3] or wslots[index] so I can then put all the 3 statements into 1
-
3. Re: Array value Equal to string (if statement)
Ned Murphy Dec 16, 2012 1:27 PM (in response to LukeElChul)If the three conditional are as similar now as I think I see them to be, then you can reduce that function to be...
function useWeapon(weaponIndex:Number, weaponSelected:String, Type:String, Frame:Number) {
for(i=0; i<Wslots.length; i++){
if (Wslots[i] == weaponSelected && weaponIndex == i+1) {
_root["wslot"+ String(i+1) +"full"] = false;
Wslots[i] = "Nothing";
_root.cam.character["ws"+ String(i+1)].gotoAndStop(1);
_root.HitOrShoot = Type;
_root.mc.weapon.gotoAndStop(Frame);
_root.weapon = weaponSelected;}
}}
One thing you should be mindful of is avoiding the unnecessary use of "_root" references in your code. If the variables/objects you are targeting are in the _root timeline, then there is no need to use _root references, and doing so can turn into a problem later on if this file ever gets loaded into another file. The use of the _root reference shown in blue for the term using the bracket notation is needed because you need some object reference to precede the brackets... in this case though, using "this" could probably work just as well.
-
4. Re: Array value Equal to string (if statement)
LukeElChul Dec 17, 2012 11:23 AM (in response to Ned Murphy)Ok thanks for thatb think I have it working
-
5. Re: Array value Equal to string (if statement)
Ned Murphy Dec 17, 2012 12:22 PM (in response to LukeElChul)You're welcome