#newbie: how to check the selection

  1. Scene has some game objects.
  2. You need to choose some of them via tap or touch.

Pseudo-code is:

 function checkSelectedObjects ()
    if (some defined objects are selected) then
         print ("Good choice")
    elseif (you need some more objects) then
         print ("Try again") --or ("Continue to select") --or("Just one more left")
    end
end

So, how to check a multiple selection of the game objects?
Thanks a lot.

You can either do this:

local function handleTouch(event)
	if (event.phase == "ended") then
		if (event.target.id == "choice1") then
			print "1"
		elseif (event.target.id == "choice2") then
			print "2"
		elseif (event.target.id == "choice3") then
			print "3"
		elseif (event.target.id == "choice4") then
			print "4"
		end
	end
	return true
end

for i = 1, 4 do
	local choice = display.newRect( display.contentCenterX, 200 * i, display.contentWidth / 2, 100 )
	choice.id = "choice" .. i
	choice:addEventListener( "touch", handleTouch )
end

or this:

local tableChoices = {}

local function checkSelected()
	for i = 1, #tableChoices do
		if (tableChoices[i].isSelected) then
			print ("choice " .. i .. " selected")
		end
	end
end

local function handleTouch(event)
	if (event.phase == "ended") then
		if (event.target.id == "choice") then
			event.target.isSelected = true
		elseif (event.target.id == "continue") then
			checkSelected()
		end
	end
	return true
end

for i = 1, 4 do
	local choice = display.newRect( display.contentCenterX, 200 * i, display.contentWidth / 2, 100 )
	choice.id = "choice"
	choice.isSelected = false
	choice:addEventListener( "touch", handleTouch )
	table.insert(tableChoices, choice)
end

local continue = display.newRect(display.contentCenterX, 1200, display.contentWidth / 1.2, 200)
continue.id = "continue"
continue:addEventListener( "touch", handleTouch )
1 Like

Didn’t we discuss exactly this in another topic earlier today? Where did that disappear? :roll_eyes:

EDIT:

https://forums.solar2d.com/t/how-to-select-some-game-objects-in-a-correct-way/351134/9

“# Oops! That page doesn’t exist or is private.”

What happened here? I don’t really feel like answering questions if they might disappear all of a sudden…

1 Like

Yes, please don’t delete threads after you’ve received your answer.

The idea behind using forums is that the knowledge is public and that it can benefit other people as well. Most people, myself included, help others out in the forums because it helps the community at large. Also, if you later came up with a better solution to your question by yourself, then that’s great, but you should consider posting it to the same thread so that others can learn too, i.e. help others out too if you can.

Like Markus already said, I am not particularly motivated to help out if I suspect that my answer will be deleted once it has helped a single person. For that kind of help, i.e. 1on1 tutoring, code review, programming/problem solving, etc. you can just consider hiring me and you will have my undivided attention.

2 Likes

By the way, I didn’t even know that it was possible to delete your own threads. Shouldn’t that be restricted to forum moderators/admins only? I’m afraid we’ll see more threads disappearing as it is now. Everybody might not understand the point of a forum, or have one of a million other reasons to delete their topics when they don’t need them anymore…

2 Likes

@Markus_Ranner
@XeduR
Sorry, gentleman, it’s my fault. I posted two similar topics at the same time, but later flagged one to delete via moderator. Wrong one, seems to me. And, this morning, it was too embarrassing to get your advices lost.

I took your clear examples into account, thank you for that.

Last night I composed a scene, that can clarify and show what I want to invent here.

local composer = require("composer")
local scene = composer.newScene()

local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX

function scene:create(event)
local sceneGroup = self.view

local background = display.newRect(display.screenOriginX, display.screenOriginY, screenW, screenH)
background.anchorX = 0
background.anchorY = 0
background:setFillColor(0)

local key1 = display.newRect (100, 150, 50, 50)
local key2 = display.newRect (200, 250, 50, 50)
local key3 = display.newRect (300, 150, 50, 50)

local sum = 0

function key1:touch (e)
		if (e.phase == "began") then
			self.xScale = 1.3; self.yScale = 1.3
			display.getCurrentStage():setFocus(self, e.id)

		elseif (e.phase == "ended") then
			self.xScale = 1; self.yScale = 1
			display.getCurrentStage():setFocus( nil )
			key1:removeEventListener("touch", key1)

			if (sum == 0) then
				print ("Key 1 was found")
				sum = sum + 1

			elseif (sum == 1) then
				sum = sum + 1
				print ("Two keys!")

			elseif (sum == 2) then
				print ("All keys!")
			end
	end
end

key1:addEventListener("touch", key1)

function key2:touch (e)
		if (e.phase == "began") then
			self.xScale = 1.3; self.yScale = 1.3
			display.getCurrentStage():setFocus(self, e.id)

		elseif (e.phase == "ended") then
			self.xScale = 1; self.yScale = 1
			display.getCurrentStage():setFocus( nil )
			key2:removeEventListener("touch", key2)

			if (sum == 0) then
				print ("Key 2 was found")
				sum = sum + 1

			elseif (sum == 1) then
				sum = sum + 1
				print ("Two keys!")

			elseif (sum == 2) then
				print ("All keys!")
			end
	end
end

key2:addEventListener("touch", key2)

function key3:touch (e)
		if (e.phase == "began") then
			self.xScale = 1.3; self.yScale = 1.3
			display.getCurrentStage():setFocus(self, e.id)

		elseif (e.phase == "ended") then
			self.xScale = 1; self.yScale = 1
			display.getCurrentStage():setFocus( nil )
			key3:removeEventListener("touch", key3)

			if (sum == 0) then
				print ("Key 3 was found")
				sum = sum + 1

			elseif (sum == 1) then
				sum = sum + 1
				print ("Two keys!")

			elseif (sum == 2) then
				sum = sum + 1
				print ("All keys!")
			end
	end
end

key3:addEventListener("touch", key3)


local key4 = display.newRect (100, 250, 50, 50)
local key5 = display.newRect (200, 150, 50, 50)
local key6 = display.newRect (300, 250, 50, 50)

function key4:touch (e)

	if (e.phase == "began") then
		self.xScale = 1.3; self.yScale = 1.3
		display.getCurrentStage():setFocus(self, e.id)

	elseif (e.phase == "ended") then
				display.getCurrentStage():setFocus( nil )
				key4:removeEventListener("touch", key4)
				key4:removeSelf()
				key4 = nil
 end
end

key4:addEventListener("touch", key4)

function key5:touch (e)

	if (e.phase == "began") then
			self.xScale = 1.3; self.yScale = 1.3
			display.getCurrentStage():setFocus(self, e.id)

	elseif (e.phase == "ended") then
				display.getCurrentStage():setFocus( nil )
				key5:removeEventListener("touch", key5)
				key5:removeSelf()
				key5 = nil
 end
end

key5:addEventListener("touch", key5)

function key6:touch (e)

	if (e.phase == "began") then
				self.xScale = 1.3; self.yScale = 1.3
				display.getCurrentStage():setFocus(self, e.id)

	elseif (e.phase == "ended") then
				display.getCurrentStage():setFocus( nil )
				key6:removeEventListener("touch", key6)
				key6:removeSelf()
				key6 = nil
	end
end

key6:addEventListener("touch", key6)

--[[local function func ()
					if (sum == 3) then
	  				composer.gotoScene( "level2" )
	  			end
				end

Runtime:addEventListener ("enterFrame", func)]]

sceneGroup:insert( background )
sceneGroup:insert (key1)
sceneGroup:insert (key2)
sceneGroup:insert (key3)
sceneGroup:insert (key4)
sceneGroup:insert (key5)
sceneGroup:insert (key6)
end

function scene:show( event )
	local sceneGroup = self.view
	local phase = event.phase
	if phase == "will" then
	elseif phase == "did" then
	end
end

function scene:hide( event )
	local sceneGroup = self.view
	local phase = event.phase
	if event.phase == "will" then
	elseif phase == "did" then
	end
end

function scene:destroy( event )
	local sceneGroup = self.view
end
---------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-----------------------------------------------------------------------------------------
return scene

Ok, no hard feelings. Keep questions coming and we’ll do our best to help you.

I’m still not sure exactly what you want to achieve, but looking at your example I can see that you are repeating a lot of your code for no good reason. It looks like the touch listeners for key 1-3 are exactly the same, as are the listeners for key 4-6. Try to extract that to reusable functions instead. Repeated code becomes very hard to read and also hard to maintain once your code base grows bigger, because you need to make the same change in several places. Take a look at @bgmadclown’s example above, or the examples you got from us in the now deleted thread, and you’ll understand what I mean. The same event listener being reused for all objects.