Here's some points I need to address before moving on:
The "parent" of a frame refers to another frame that it shares its visibility, scale, and other attributes with. For example, you have one frame representing a dialog box, and another representing a button. Normally, the button would have its parent set to the dialog box so when you show and hide the box, the button would respond accordingly and show or hide itself as well.
ACFrame is defined at the bottom, which isn't useful at all. Lua and other programming languages execute code in top-down order. When you reference
ACFrame, Lua doesn't know what it is yet and attempts to find a global with that name. At this time, it's simply
nil. When
Frame:SetPoint() gets this, it positions relative to the screen itself instead of the frame you're creating later.
To start, let's rearrange the code so we can get things to work:
This can stay where it is. The function only reacts to what's passed to it. Drag handlers registered to move parent.
Button:SetNormalTexture() was used to show each icon.
Lua Code:
local CreateButton; do-- Prototype for function
-- Drag Handlers
local function OnDragStart(self) self:GetParent():StartMoving(); end
local function OnDragStop(self) self:GetParent():StopMovingOrSizing(); end
-- Tooltip Handlers
local function OnEnter(self)
if self.Tooltip then
GameTooltip:SetOwner(self,"ANCHOR_TOP");
GameTooltip:AddLine(self.Tooltip,0,1,0.5,1,1,1);
GameTooltip:Show();
end
end
local function OnLeave(self) if GameTooltip:IsOwned(self) then GameTooltip:Hide(); end end
-- Button Generator (this will be assigned to the upvalue noted as a function prototype)
function CreateButton(parent,name,texture,text,tooltip)
tooltip=tooltip or text;-- If no tooltip, use button text
-- Create our button
local btn=CreateFrame("Button",name,parent,"SecureActionButtonTemplate");
btn:SetSize(30,30);
-- Setup button text
btn:SetNormalFontObject("GameFontNormalSmall");
btn:SetHighlightFontObject("GameFontHighlightSmall");
btn:SetDisabledFontObject("GameFontDisableSmall");
btn:SetText(text);
-- Setup button's backgorund
btn:SetNormalTexture(texture);
-- Register handlers
btn:RegisterForClicks("AnyUp");-- Register all buttons
btn:RegisterForDrag("LeftButton");-- Register for left drag
btn:SetScript("OnDragStart",OnDragStart);
btn:SetScript("OnDragStop",OnDragStop);
btn:SetScript("OnEnter",OnEnter);
btn:SetScript("OnLeave",OnLeave);
btn.Tooltip=tooltip;
-- Return our button
return btn;
end
end
The frame definition is moved here so the code below can use it. Proper registration for dragging has been implemented.
New frames are always shown and have no anchors, so
Frame:Show() and
Frame:ClearAllPoints() respectively are unnecessary.
Frame:SetSize() replaces both
Frame:SetWidth() and
Frame:SetHeight().
Frame:SetScale() was removed as you were sending
nil to it and
Frame:SetMovable() was properly set.
Lua Code:
local frame = CreateFrame("Frame","ACFrame",UIParent)
frame:SetPoint("CENTER",UIParent)
frame:SetSize(30+30+20,(7*30)+20)
frame:SetBackdrop(StaticPopup1:GetBackdrop())
frame:SetMovable(true)
frame:EnableMouse(true)-- Receive mouse events (Buttons automatically have this set)
frame:RegisterForDrag("LeftButton")-- Register left button for dragging
frame:SetScript("OnDragStart",frame.StartMoving)-- Set script for drag start
frame:SetScript("OnDragStop",frame.StopMovingOrSizing)-- Set script for drag stop
The following is slightly modified to use the frame pointer stored in
frame immediately above. Layout was corrected to fit nicely within frame.
Lua Code:
local button=CreateButton(frame,"AshranCommanderButton1","Interface\\Icons\\achievement_pvp_a_h",nil,"Warspear Keep");
button:SetPoint("TOPRIGHT",-10,-10);-- Anchors always default to an object's parent
button:SetScript("OnClick", function()
SendChatMessage("Warspear Keep ","SAY")
DoEmote("follow")
end )
local button=CreateButton(frame,"AshranCommanderButton2","Interface\\Icons\\achievement_garrison_tier02_horde",nil,"Emberfall Tower");
button:SetPoint("TOP",AshranCommanderButton1,"BOTTOM");-- Anchors always default to an object's parent
button:SetScript("OnClick", function()
SendChatMessage("Emberfall Tower ","SAY")
DoEmote("follow" , UnitName("target"))
end )
local button=CreateButton(frame,"AshranCommanderButton3","Interface\\Icons\\achievement_garrison_tier01_horde",nil,"Volrath's Advance");
button:SetPoint("TOP",AshranCommanderButton2,"BOTTOM");-- Anchors always default to an object's parent
button:SetScript("OnClick", function()
SendChatMessage("Volrath's Advance ","SAY")
DoEmote("follow" , UnitName("target"))
end )
local button=CreateButton(frame,"AshranCommanderButton4","Interface\\Icons\\achievement_doublejeopardy",nil,"The Crossroads");
button:SetPoint("TOP",AshranCommanderButton3,"BOTTOM");-- Anchors always default to an object's parent
button:SetScript("OnClick", function()
SendChatMessage("The Crossroads ","SAY")
DoEmote("follow" , UnitName("target"))
end )
local button=CreateButton(frame,"AshranCommanderButton5","Interface\\Icons\\achievement_garrison_tier01_alliance",nil,"Tremblade's Vanguard");
button:SetPoint("TOP",AshranCommanderButton4,"BOTTOM");-- Anchors always default to an object's parent
button:SetScript("OnClick", function()
SendChatMessage("Tremblade's Vanguard ","SAY")
DoEmote("follow" , UnitName("target"))
end )
local button=CreateButton(frame,"AshranCommanderButton6","Interface\\Icons\\achievement_garrison_tier02_alliance",nil,"Archmage Overwatch");
button:SetPoint("TOP",AshranCommanderButton5,"BOTTOM");-- Anchors always default to an object's parent
button:SetScript("OnClick", function()
SendChatMessage("Archmage Overwatch ","SAY")
DoEmote("follow" , UnitName("target"))
end )
local button=CreateButton(frame,"AshranCommanderButton7","Interface\\Icons\\achievement_pvp_h_a",nil,"Stormshield Stronghold");
button:SetPoint("TOP",AshranCommanderButton6,"BOTTOM");-- Anchors always default to an object's parent
button:SetScript("OnClick", function()
SendChatMessage("Stormshield Stronghold ","SAY")
DoEmote("follow" , UnitName("target"))
end )
local button=CreateButton(frame,"AshranCommanderButton8","Interface\\Icons\\Ability_rogue_sprint",nil,"Amphitheater of Annihilation(AoA)");
button:SetPoint("RIGHT",AshranCommanderButton1,"LEFT");-- Anchors always default to an object's parent
button:SetScript("OnClick", function()
SendChatMessage("AoA - EVENT:STADIUM RACING - Block the entrance!","SAY")
DoEmote("follow" , UnitName("target"))
end )
local button=CreateButton(frame,"AshranCommanderButton9","Interface\\Icons\\spell_fire_fire",nil,"Brute's Rise(BR)");
button:SetPoint("RIGHT",AshranCommanderButton2,"LEFT");-- Anchors always default to an object's parent
button:SetScript("OnClick", function()
SendChatMessage("BR - EVENT:OGRE FIRES - Block the stairs! ","SAY")
DoEmote("follow" , UnitName("target"))
end )
local button=CreateButton(frame,"AshranCommanderButton10","Interface\\Icons\\achievement_boss_furyfurnace",nil,"Ring of Conquest(RoC)");
button:SetPoint("RIGHT",AshranCommanderButton3,"LEFT");-- Anchors always default to an object's parent
button:SetScript("OnClick", function()
SendChatMessage("Ring of Conquest GO RoC ","SAY")
DoEmote("follow" , UnitName("target"))
end )
local button=CreateButton(frame,"AshranCommanderButton11","Interface\\Icons\\Trade_archaeology_apexisstatue",nil,"Ashran Excavation(Mines)");
button:SetPoint("RIGHT",AshranCommanderButton4,"LEFT");-- Anchors always default to an object's parent
button:SetScript("OnClick", function()
SendChatMessage("Ashran Excavation - EVENT:APEXIS MARKS - Secure the center! ","SAY")
DoEmote("follow" , UnitName("target"))
end )
local button=CreateButton(frame,"AshranCommanderButton12","Interface\\Icons\\achievement_reputation_ogre",nil,"Seat of Kor'lok");
button:SetPoint("RIGHT",AshranCommanderButton5,"LEFT");-- Anchors always default to an object's parent
button:SetScript("OnClick", function()
SendChatMessage("Seat of Kor'lok - Kor'lok - Kill the ogre! ","SAY")
DoEmote("follow" , UnitName("target"))
end )
local button=CreateButton(frame,"AshranCommanderButton13","Interface\\Icons\\Spell_lifegivingspeed",nil,"Molten Quarry(MQ)");
button:SetPoint("RIGHT",AshranCommanderButton6,"LEFT");-- Anchors always default to an object's parent
button:SetScript("OnClick", function()
SendChatMessage("Molten Quarry - EVENT:Empowered Ore - Block the entrance! ","SAY")
DoEmote("follow" , UnitName("target"))
end )
local button=CreateButton(frame,"AshranCommanderButton14","Interface\\Icons\\Achievement_halloween_ghost_01",nil,"Ashmaul Burial Grounds(ABG)");
button:SetPoint("RIGHT",AshranCommanderButton7,"LEFT");-- Anchors always default to an object's parent
button:SetScript("OnClick", function()
SendChatMessage("Ashmaul Burial Grounds - EVENT:RISEN SPIRITS - Clear the center and block the entrance! ","SAY")
DoEmote("follow" , UnitName("target"))
end )
There is more that can be done to optimize the code like creating a layout table and using that in a loop to create your buttons, but for now, this'll do.