Commits

Chris Klimas committed 178dc46

Add Group:members() iterator

A convenience method that returns index, sprite over member sprites.

Comments (0)

Files changed (4)

zoetrope/core/group.lua

 	solid = true,
 
 	-- Property: sprites
-	-- A table of member sprites, in drawing order.
+	-- A table of member sprites, in drawing order. You can iterate over this
+	-- table with <members()>.
 	sprites = {},
 
 	-- Property: timeScale
 	-- 		nothing
 
 	remove = function (self, sprite)
-		for i, spr in ipairs(self.sprites) do
+		for i, spr in self:members() do
 			if spr == sprite then
 				table.remove(self.sprites, i)
 				return
 	--		nothing
 
 	moveToFront = function (self, sprite)
-		for i, spr in ipairs(self.sprites) do
+		for i, spr in self:members() do
 			if spr == sprite then
 				table.remove(self.sprites, i)
 				table.insert(self.sprites, sprite)
 	--		nothing
 	
 	moveToBack = function (self, sprite)
-		for i, spr in ipairs(self.sprites) do
+		for i, spr in self:members() do
 			if spr == sprite then
 				table.remove(self.sprites, i)
 				table.insert(self.sprites, 1, sprite)
 		table.sort(self.sprites, func)
 	end,
 
+	-- Method: members
+	-- A convenience method that iterates over all member sprites.
+	--
+	-- Arguments:
+	--		none
+	--
+	-- Returns:
+	--		order, sprite
+
+	members = function (self)
+		return ipairs(self.sprites)
+	end,
+
 	-- Method: collide
 	-- Collides all solid sprites in the group with another sprite or group.
 	-- This calls the <Sprite.onCollide> event handlers on all sprites that
 		if subgroups then
 			local count = 0
 
-			for _, spr in pairs(self.sprites) do
+			for _, spr in self:members() do
 				if spr:instanceOf(Group) then
 					count = count + spr:count(true)
 				else
 	contains = function (self, sprite, recurse)
 		if recurse ~= false then recurse = true end
 
-		for _, spr in pairs(self.sprites) do
+		for _, spr in self:members() do
 			if spr == sprite then return true end
 
 			if recurse and spr:instanceOf(Group) and spr:contains(sprite) then
 		if not self.active then return end
 		elapsed = elapsed * self.timeScale
 		
-		for _, spr in pairs(self.sprites) do
+		for _, spr in self:members() do
 			if spr.active then spr:startFrame(elapsed) end
 		end
 
 		if not self.active then return end
 		elapsed = elapsed * self.timeScale
 
-		for _, spr in pairs(self.sprites) do
+		for _, spr in self:members() do
 			if spr.active then spr:update(elapsed) end
 		end
 
 		if not self.active then return end
 		elapsed = elapsed * self.timeScale
 
-		for _, spr in pairs(self.sprites) do
+		for _, spr in self:members() do
 			if spr.active then spr:endFrame(elapsed) end
 		end
 
 			end
 		end
 		
-		for _, spr in pairs(self.sprites) do	
+		for _, spr in self:members() do	
 			if spr.visible then
 				if spr.translate then
 					spr:draw(spr.translate.x + scrollX, spr.translate.y + scrollY)

zoetrope/core/sprite.lua

 		local xChange = 0
 		local yChange = 0
 
-		if other.sprites then
+		if other.members then
 			-- handle groups
 
-			for _, spr in pairs(other.sprites) do
+			for _, spr in other:members() do
 				self:displace(spr, xHint, yHint)
 			end
 		else

zoetrope/sprites/emitter.lua

 	--		nothing
 
 	extinguish = function (self)
-		for _, spr in pairs(self.sprites) do
+		for _, spr in self:members() do
 			spr:die()
 		end
 

zoetrope/sprites/map.lua

 	-- makes sure all sprites receive startFrame messages
 
 	startFrame = function (self, elapsed)
-		for _, spr in pairs(self.sprites) do
+		for _, spr in self:members() do
 			spr:startFrame(elapsed)
 		end
 
 	-- makes sure all sprites receive update messages
 
 	update = function (self, elapsed)
-		for _, spr in pairs(self.sprites) do
+		for _, spr in self:members() do
 			spr:update(elapsed)
 		end
 
 	-- makes sure all sprites receive endFrame messages
 
 	endFrame = function (self, elapsed)
-		for _, spr in pairs(self.sprites) do
+		for _, spr in self:members() do
 			spr:endFrame(elapsed)
 		end
 
 		Sprite.endFrame(self, elapsed)
 	end,
 
+	-- mimic Group's members() method
+
+	members = function (self)
+		return ipairs(self.sprites)
+	end,
+
 	__tostring = function (self)
 		local result = 'Map (x: ' .. tostring(self.x) .. ', y: ' .. tostring(self.y) ..
 					   ', w: ' .. tostring(self.width) .. ', h: ' .. tostring(self.height) .. ', '