12.07.2015 Views

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

514 Part III ■ Advanced Addon Techniquesif event == “UNIT_MAXHEALTH“ thenself.unit.healthBar:SetMinMaxValues(0, UnitHealthMax(unit))self.unit.healthBar:SetValue(UnitHealth(unit))elseif event == “UNIT_HEALTH“ thenself.unit.healthBar:SetValue(UnitHealth(unit))endendendThis function first checks to ensure that the frame’s unit attribute is set;otherwise the frame is not displaying a unit. Then the h<strong>and</strong>ler responds to thePLAYER_ENTERING_WORLD event, because it doesn’t matter which unit is beingshown by the frame. Then you check that the unit being altered (passed inarg1) is the same as the unit the frame is displaying. Inside this condition, theunit health <strong>and</strong> maximum health events are h<strong>and</strong>led, just changing the statusbars as appropriate.Power Update EventsProperly displaying <strong>and</strong> updating the power bar <strong>for</strong> a unit frame is inherentlymuch more difficult, because a unit can have multiple types <strong>of</strong> power. Mana,Energy, Rage, Runic Power, <strong>and</strong> Focus can all be queried by the UnitPower()API function, but they all fire different types <strong>of</strong> events. Those sneaky Druidswho are able to switch between Rage, Energy, <strong>and</strong> Mana when they shapeshiftfurther complicate the issue.Rather than registering <strong>for</strong> all <strong>of</strong> the events <strong>and</strong> then having to check thatyou’re responding to the correct one, you’ll only register each frame <strong>for</strong> theevents that are actually relevant. Replace your existing SquareUnitFrames_ResetPowerBar function with the following two functions (or add the firstfunction <strong>and</strong> just make the appropriate changes to the second):local function unregisterManyEvents(frame, ...)<strong>for</strong> i=1, select(“#“, ...) dolocal event = select(i, ...)frame:UnregisterEvent(event)endendfunction SquareUnitFrames_ResetPowerBar(button, unit)local powerType, powerToken = UnitPowerType(unit)local powerColor = PowerBarColor[powerToken]local alive = not UnitIsDeadOrGhost(unit)local parent = button:GetParent()unregisterManyEvents(parent, “UNIT_MANA“, “UNIT_RAGE“, i“UNIT_FOCUS“,“UNIT_ENERGY“, “UNIT_RUNIC_POWER“)unregisterManyEvents(parent, “UNIT_MAXMANA“, “UNIT_MAXRAGE“, i“UNIT_MAXFOCUS“, “UNIT_MAXENERGY“, “UNIT_MAXRUNIC_POWER“)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!