Multi Touching Trouble

Hey,
I have two images that need to be pressed at the same time for a function to activate. I set a counter, and the multi-touch is working, but the function does not activate when the counter = 2. Code:

[code]
function eventTouch(event)

if(event.phase == “began”) then
text1.text = “Hold”
counter = counter + 1
print(counter)
end

if(event.phase == “ended”) then
text1.text = “Press both images at the same time”
counter = counter - 1
print(counter)
end
end
image1:addEventListener(“touch”, eventTouch)
image2:addEventListener(“touch”, eventTouch)[/code]
And that’s the function: [code]
local function onTimer(event)
if flag then
print(“The button was pressed for 4 seconds”)
local event = {}
event.phase = “ended”
image1:dispatchEvent( event )
director:changeScene(“scene2”)
else
end
end

local function onTouch(event)
if (counter == 2) then
flag = true
timerHandle = timer.performWithDelay(4000,onTimer)
else
flag = false
timer.cancel(timerHandle)
timerHandle = nil
end
end[/code]
So, explaining: only once the user starts pressing both images, a timer starts. And only if he keeps holding it for 4 seconds, the scene will change. Otherwise, it will cancel the timer.
Any thoughts?
Appreciate it!

[import]uid: 95495 topic_id: 20787 reply_id: 320787[/import]

Hey Gustav,

This is plug and play code - run it and try touching and holding down on the part where the two images overlap. (Just to save building to device for multitouch.)

I think it might help :slight_smile:

[lua]display.setStatusBar (display.HiddenStatusBar)

local image1 = display.newCircle( 100, 100, 40 )
local image2 = display.newCircle( 150, 100, 40 )

touch1 = false
touch2 = false

local function timerUp()
print “Time up”
end

local function startTimer ()
if myTimer == nil then
print “started timer”
myTimer = timer.performWithDelay(4000, timerUp, 1)
end
end

local function onTouch1 (event)
if event.phase == “began” then
touch1 = true
if touch1 == true and touch2 == true then
startTimer()
end
else
touch1 = false
if myTimer then timer.cancel(myTimer) myTimer = nil end
end
end
image1:addEventListener(“touch”, onTouch1)

local function onTouch2 (event)
if event.phase == “began” then
touch2 = true
if touch1 == true and touch2 == true then
startTimer()
end
else
touch2 = false
if myTimer then timer.cancel(myTimer) myTimer = nil end
end
end
image2:addEventListener(“touch”, onTouch2)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 20787 reply_id: 81825[/import]

Great stuff! It worked!

Thanks a lot Peach :wink:

Is there any tutorial on setting a keyboard? I need the user to write his name, and then use his name in the next scene in a text… [import]uid: 95495 topic_id: 20787 reply_id: 82219[/import]

You’d use this API for the text field; http://developer.anscamobile.com/reference/index/nativenewtextfield

Say you called the field, nameField you could then grab the text the user entered like this;

[lua]username = nameField.text[/lua]

If you wanted it available in the next scene and are using director you’d either save it to a file (Ego on Techority would be easy for this) to load in the next scene or make it a global.

Globals should be avoided whether possible and are not good practice although at the end of the day if you have to use one for a name it wont be the end of the world.

Peach :slight_smile: [import]uid: 52491 topic_id: 20787 reply_id: 82412[/import]

Ok, so I managed to set the text fields, but I’m having trouble setting them globally.

Here’s what I got in the first scene: [code]
local defaultField
local function fieldHandler( getObj )
return function( event )
if ( “began” == event.phase ) then
elseif ( “ended” == event.phase ) then
elseif ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
end
end
end
defaultField = native.newTextField( _W/4, 100, _W/2, 30,
fieldHandler( function() return defaultField end ) )

local defaultField2
local function fieldHandler2( getObj )
return function( event )
if ( “began” == event.phase ) then
elseif ( “ended” == event.phase ) then
elseif ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
end
end
end
defaultField2 = native.newTextField( _W/4, _H/2 + 110, _W/2, 30,
fieldHandler2( function() return defaultField2 end ) ) [/code]

Meaning two text fields, and then, in the next scene, two texts that need to be filled with the names of these text fields: [code]
local text1 = display.newText(username, 0, 0, native.systemFont, 20*2)
text1.xScale = 0.5; text1.yScale = 0.5
text1:setReferencePoint(display.CenterReferencePoint)
text1.rotation = 180
text1.x = 60; text1.y = 120

local text2 = display.newText(username, 0, 0, native.systemFont, 20*2)
text2.xScale = 0.5; text2.yScale = 0.5
text2:setReferencePoint(display.CenterReferencePoint)
text2.x = _W - 60; text2.y = _H - 120[/code]

What should I change from here??
Thanks a lot!
[import]uid: 95495 topic_id: 20787 reply_id: 83249[/import]

Hey there,

I can’t actually see you setting the username in the first scene.

You need to do;

[lua]username = defaultField.text[/lua]

Which you’d do in the fieldHandler function. (Likely in the “submitted” phase.)

From there you could either save the username to a file and load it in the next scene or you could instead make it global, by doing;

[lua]_G.username = defaultField.text[/lua]

Then recall in the next scene using _G.username.

You want to avoid globals wherever possible - different people have different views on how “bad” it is to use them. Personally I see it as a necessary evil from time to time :wink:

Let me know how you go.

Peach :slight_smile: [import]uid: 52491 topic_id: 20787 reply_id: 83275[/import]

Ok, I have this now:[code]
local defaultField
local function fieldHandler( getObj )
return function( event )
if ( “began” == event.phase ) then
elseif ( “ended” == event.phase ) then
elseif ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
username = defaultField.text
end
end
end
defaultField = native.newTextField( _W/4, 100, _W/2, 30,
fieldHandler( function() return defaultField end ) )

local defaultField2
local function fieldHandler2( getObj )
return function( event )
if ( “began” == event.phase ) then
elseif ( “ended” == event.phase ) then
elseif ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
username2 = defaultField2.text
end
end
end
defaultField2 = native.newTextField( _W/4, _H/2 + 110, _W/2, 30,
fieldHandler2( function() return defaultField2 end ) )
_G.username = defaultField.text
_G.username2 = defaultField2.text [/code]
And then[code]
local text1 = display.newText(username, 0, 0, native.systemFont, 20*2)
text1.xScale = 0.5; text1.yScale = 0.5
text1:setReferencePoint(display.CenterReferencePoint)
text1.rotation = 180
text1.x = 60; text1.y = 120

local text2 = display.newText(username, 0, 0, native.systemFont, 20*2)
text2.xScale = 0.5; text2.yScale = 0.5
text2:setReferencePoint(display.CenterReferencePoint)
text2.x = _W - 60; text2.y = _H - 120
_G.username = defaultField.text
_G.username2 = defaultField2.text[/code]
But I get the following error from terminal:

lua:11: attempt to index global ‘defaultField’ (a nil value).

I thought it could be a simulator problem, but even for iOS simulator it occurs.
Thoughts?

[import]uid: 95495 topic_id: 20787 reply_id: 83323[/import]

Yeah, this is wrong - try;

[lua]local defaultField
local function fieldHandler( getObj )
return function( event )
if ( “began” == event.phase ) then
elseif ( “ended” == event.phase ) then
elseif ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
_G.username = defaultField.text
end
end
end
defaultField = native.newTextField( _W/4, 100, _W/2, 30,
fieldHandler( function() return defaultField end ) )

local defaultField2
local function fieldHandler2( getObj )
return function( event )
if ( “began” == event.phase ) then
elseif ( “ended” == event.phase ) then
elseif ( “submitted” == event.phase ) then
native.setKeyboardFocus( nil )
_G.username2 = defaultField2.text
end
end
end
defaultField2 = native.newTextField( _W/4, _H/2 + 110, _W/2, 30,
fieldHandler2( function() return defaultField2 end ) ) [/lua]

and

[lua]local text1 = display.newText(_G.username, 0, 0, native.systemFont, 20*2)
text1.xScale = 0.5; text1.yScale = 0.5
text1:setReferencePoint(display.CenterReferencePoint)
text1.rotation = 180
text1.x = 60; text1.y = 120

local text2 = display.newText(_G.username, 0, 0, native.systemFont, 20*2)
text2.xScale = 0.5; text2.yScale = 0.5
text2:setReferencePoint(display.CenterReferencePoint)
text2.x = _W - 60; text2.y = _H - 120[/lua]

Let me know how that goes.

Peach :slight_smile: [import]uid: 52491 topic_id: 20787 reply_id: 83350[/import]

It went perfect!! Thanks a lot Peach!!

One last question on this theme: how do I squeeze the screen when the keyboard goes up? [import]uid: 95495 topic_id: 20787 reply_id: 83357[/import]

Sorry, I have one more (lol): I was testing for device and the multi touch working the way I expected. It is only activating the function when I touch both images at the same time. I wished the first user touched, and then, when the second one did, it activates it. And after both of them pressing for 4 seconds, it finishes the timer and the next scene is up.

This is the code: [code]
display.setStatusBar (display.HiddenStatusBar)

local image1 = display.newCircle( 100, 100, 40 )
local image2 = display.newCircle( 150, 100, 40 )

touch1 = false
touch2 = false

local function timerUp()
print “Time up”
end

local function startTimer ()
if myTimer == nil then
print “started timer”
myTimer = timer.performWithDelay(4000, timerUp, 1)
end
end

local function onTouch1 (event)
if event.phase == “began” then
touch1 = true
if touch1 == true and touch2 == true then
startTimer()
end
else
touch1 = false
if myTimer then timer.cancel(myTimer) myTimer = nil end
end
end
image1:addEventListener(“touch”, onTouch1)

local function onTouch2 (event)
if event.phase == “began” then
touch2 = true
if touch1 == true and touch2 == true then
startTimer()
end
else
touch2 = false
if myTimer then timer.cancel(myTimer) myTimer = nil end
end
end
image2:addEventListener(“touch”, onTouch2)[/code]

Thanks a lot for your help… [import]uid: 95495 topic_id: 20787 reply_id: 83368[/import]

To squeeze the screen up you’d put what is on the screen in a group and then change the group.yScale.

For the timer to activate only on the second you’d just not reference the timer in touch1 function, only the second touch function.

Peach :slight_smile: [import]uid: 52491 topic_id: 20787 reply_id: 83485[/import]