Create my own Toggle Image Button Class

Hi there community,

Could someone please validate this snippet? I am trying to create a toggle image (sound on/off, fx on/off, …). It is not a big class, but I am fairly new to Lua and the classes are a litte tricky because I have only developed in actionscript and java so far.

toggleImageWidget = {}  
  
local function onGroupTap(event)  
 group = event.target;  
  
 if(group.on == true) then  
 group.img\_on.isVisible = false;  
 group.img\_off.isVisible = true;  
 group.on = false;  
 else  
 group.img\_on.isVisible = true;  
 group.img\_off.isVisible = false;  
 group.on = true;  
 end  
  
end  
  
function toggleImageWidget:new(imagesSheet, imagesAsset, img\_on\_index, img\_off\_index)  
 local group = display.newGroup();  
 group:addEventListener("tap", onGroupTap);  
 group.on = true;  
  
 local img\_on = display.newImage(imagesSheet, imagesAsset.frameIndex[img\_on\_index]);  
 img\_on:setReferencePoint(display.TopLeftReferencePoint);  
 img\_on.isVisible = true;  
 group.img\_on = img\_on;  
 group:insert(img\_on);  
  
 local img\_off = display.newImage(imagesSheet, imagesAsset.frameIndex[img\_off\_index]);  
 img\_off:setReferencePoint(display.TopLeftReferencePoint);  
 img\_off.isVisible = false;  
 group.img\_off = img\_off;  
 group:insert(img\_off);  
  
 return group;  
end  
  
return toggleImageWidget;  
  

The code is working (as long as the images have the exact same size). Is this a good approach?

thx
[import]uid: 217591 topic_id: 35867 reply_id: 335867[/import]