[SOLVED] Multitouch? How to move an analog joystick and press a button at the same time?

Hello guys,

I need a rescue! Ok, here my problem. I have a analog joystick and a weapon change button. I can move the joystick or press the weapon button but not do both. So I say ok, all I need is add the line:

system.activate( " multitouch" )

Still no go. Actually it seems that it does not matter that i am using the analog joystick which I thought it may not work with multitouch. Actually simply keeping a finger on the screen seems to block the touch to the joystick move, the exit button and the weapon button. Of course i start reading about multitouch and searching on this forum but frankly I am a little confused now! All I need is being able to move the joystick and change the weapon mode at the same time.

Below is my weapon button touch function. As you can I am just switching the weapon mode and changing the label of the button from GUN to LASER. I am using a widget button.

Any pointers will be greatly appreciated it.

Thanks guys.

Mo


I am using the following code for the joystick (thanks Jay!!)

http://developer.anscamobile.com/code/simple-analog-stick-joystick-module

And here the code for the weapon button:

[lua]local function btWeapon (event)
if event.phase == “release” then

– Play Sound
if fxOn == true then audio.play( tapSound ) end

if weaponMode == 1 then

weaponMode = 2

btWeapon:setLabel( “GUN” )

elseif weaponMode == 2 then

weaponMode = 1

btWeapon:setLabel( “LASER” )

else

return true

end

end

end

btWeapon = widget.newButton{
id = “btWeapons”,
left = right - 110 ,
top = bottom - 180,
label = “LASER”,
width = 135, height = 41,
cornerRadius = 8,
onEvent = btWeapon,
default=imagesHudPath…“weaponLabel.png”,
over=imagesHudPath…“weaponLabelOver.png”,

font = “Gears of Peace”,
fontSize = 14,
offset = 0,
labelColor = { default={ 255,255,255}, over={ 177,243,255 } }
}[/lua]
[import]uid: 49236 topic_id: 24143 reply_id: 324143[/import]

Hi, have you looked at the DragMeMultitouch sample app? It’s under interface. Focus on the following information:

[blockcode]
local function onTouch( event )
local t = event.target

local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t, event.id )
t.isFocus = true
elseif t.isFocus then
if “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( t, nil )
t.isFocus = false
end
end
return true
end
[/blockcode]

Reading through the joystick module it seems they have already handled this on their end so you should only need to apply the above code to your weapon button.
[import]uid: 109535 topic_id: 24143 reply_id: 97442[/import]

@arlenraisbeck: WOW! Thanks so much. It gives me something to chew on. I appreciated it.

Mo [import]uid: 49236 topic_id: 24143 reply_id: 97445[/import]

Hello again,

Thank you so much for the pointer. It really helped but I am hitting a another wall! I think it has to do with the fact that i am using a widget button which do not have event like “began”, “end” but has event like “press”, “moved” and “released” So I replaced “began” with “press” and “ended” with “release” in the button event code. Unfortunately I am getting the following error:

“…bad argument #-2 to ‘setFocus’ (Proxy expected, got nil)”

Below is my current code.

Any suggestions?

Thanks again.

Mo

EDIT: If I replace “press” with “began” for instance then I do not get an error but of course nothing happens because the widget button do not have event named “began”


[lua]local function btWeapon (event)

local t = event.target

if event.phase == “press” then

display.getCurrentStage():setFocus( t, event.id )
t.isFocus = true

– switch weapon
if weaponMode == 1 then

weaponMode = 2
btWeapon:setLabel( “GUN” )

elseif weaponMode == 2 then

weaponMode = 1
btWeapon:setLabel( “LASER” )

end

elseif t.isFocus then

if “release” == phase then
display.getCurrentStage():setFocus( t, nil )
t.isFocus = false

end

end

return true

end[/lua] [import]uid: 49236 topic_id: 24143 reply_id: 97455[/import]

Sorry, I don’t have any experience with widget buttons. My only advice would be to use images as buttons if you aren’t particularly set on using widgets.

[blockcode]
local btweapon = display.newImage(“btweapon.png”)

local function onTouchBtWeapon( event )
local t = event.target

local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t, event.id )
t.isFocus = true
elseif t.isFocus then
if “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( t, nil )
t.isFocus = false
end
end
return true
end
btweapon:addEventListener(“touch”, onTouchBtWeapon)

[/blockcode]

Then you can set your own properties within the btweapon object e.g btweapon.weaponType = "gun" Are you changing the way the button looks as well? Does it actually say “gun” or “laser” on it? If so you could do this with images or text and group it all together. For instance, one image with “gun” written on it and another with “laser” then put them in a group and make only one visible at a time, depending on the state. The entire group would then become your btweapon.

Is there a reason you want to use widget buttons for your project?

It could also have something to do with this:
[blockcode]
– In btweapon touch handler
display.getCurrentStage():setFocus( t, event.id )
display.getCurrentStage():setFocus( t, nil )

– In joystick module
display.getCurrentStage():setFocus( T )
display.getCurrentStage():setFocus( nil )

–>> So try it with the single parameter, so they match
– In btweapon touch handler
display.getCurrentStage():setFocus( t )
display.getCurrentStage():setFocus( nil )
[/blockcode]

Let me know how this goes. [import]uid: 109535 topic_id: 24143 reply_id: 97466[/import]

@arlenraisbeck

Thanks so much for the info. The reason I am using the widgets (buttons and scrollview) is that they are very convenient. In case of buttons for instance, you do not need to worry about loading up the images nor add event listeners. It’s all taking care for you.

But…I think you right and I should convert my weapon button into an image display as you describe. Thanks a lot for the code by the way. i will try it and report here my results. I will also need to deal with the joystick part as you said to make sure it do not block the weapon button if I move the joystick since I want the player to be able to switch weapon at will.

My guess is that many people are using widget buttons and multitouch since I could not find any posts about it. Maybe Ansa people can share their views on this since like I said widgets are very convenient and clean way of making an UI for your app.

Thanks a lot again for taking the time. I appreciated it.

Mo

ps: I am planning on keeping the widgets buttons for most of my app screens. I really only need multitouch on the main game screen… [import]uid: 49236 topic_id: 24143 reply_id: 97574[/import]

Hello,

WOW what a mistake! Epic. In the line t add multitouch

system.activate( " multitouch" )

I had a space before the m in multitouch!!! I am pretty sure I cut/past from somewhere but still.

In any event the multitouch is “almost” working. Now i can move the analog joystick and press the weapon mode button at the same time. The problem now is that if i am moving the joystick and then either press anywhere in the screen or the weapon button, the joystick loses focus? Not sure what I am doing wrong but i am almost certain that my weapon button is reacting correctly but my joystick has a serious problem with focus. Of course this behavior is only seen on the device since the simulator do not work with multitouch.

The funny part is that I just purchase Corona Ultimote (a FANTASTIC product) so to stop wasting time building to device. And here the funny part: The joystick and weapon button work like a charm when using Ultimote and the simulator!! The joystick always get the focus when needed. I am assuming these are the situation where there is a difference between the simulator and a real device…

I tried both;

display.getCurrentStage():setFocus( t, event.id )
and
display.getCurrentStage():setFocus( t )

in weapon button and the joystick and still no go. It is obvious I am doing something terrible wrong and just hope it is not something obvious like having an extra space on the word multitouch!!

Still it is a mistery to me why using “ultimote” and the simulator it works great but not on the device. I will think ultimote + simulator is the closest thing to a device. I guess not close enough…

I will continue slaving over this issue but I will appreciate any help I can get.

THANKS

Mo [import]uid: 49236 topic_id: 24143 reply_id: 97615[/import]

Hey, I just tested the joystick module myself, and I’m getting similar problems. I’m afraid I don’t know what’s going on there. Someone else may have to chime in at this point. The multitouch should work for multiple buttons, but including this joystick seems to cause problems. Good news is, it doesn’t look like the widgets were the problem.

I will need to sort this out for one of my own apps so I’ll let you know when I find a solution. Sorry I can’t be of more help right now. [import]uid: 109535 topic_id: 24143 reply_id: 97668[/import]

I am very sorry you are getting the same issue and yes I agree it seems to come from the analog joystick code. At least it means we are not doing really wrong with our code (it seems)

Unfortunately the creator of that analog joystick (Jay creator of the amazing Particle Candy module) has said that code was not supported which of course I understand. Still I am going to leave a message under that code page to see if he can helps with this issue of multitouch and joystick. The bizarre thing looking at his code is that it seems to deal with focus already so I am not what the issue.

I will keep you posted if I found a solution.

Thanks for your insights.

Mo [import]uid: 49236 topic_id: 24143 reply_id: 97748[/import]

GOT IT!

The answer was in this post:

http://developer.anscamobile.com/forum/2011/10/08/using-multiple-virtual-joysticks-same-time-multitouch

I saw before but i did not make the connection since they were talking about having 2 joysticks at the same time. Duh! that’s also mean having multitouch enable!

So below is the joystick code changes (Group.onDrag = function ( event ) function)

In my own code (weapon button) I still use:

[lua]if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

elseif t.isFocus then

if “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus(nil )
t.isFocus = false[/lua]

And it seems to work like a charm on devices like ipad and iphone4.

Thanks to “producerism” for coming up with a solution and to Jay (X-expressive) for coming with the joystick code in the first place!

Enjoy!

Mo


[lua]Group.onDrag = function ( event )

local T = event.target – THUMB
local S = T.parent – STICK
local phase = event.phase
local ex,ey = S:contentToLocal(event.x, event.y)
ex = ex - T.x0
ey = ey - T.y0

if “began” == phase then
if S.Timer ~= nil then timer.cancel(S.Timer); S.Timer = nil end

–display.getCurrentStage():setFocus( T )
display.getCurrentStage():setFocus( T, event.id )

T.isFocus = true
– STORE INITIAL POSITION
T.x0 = ex - T.x
T.y0 = ey - T.y

elseif T.isFocus then
if “moved” == phase then


S.distance = Sqr (ex*ex + ey*ey)
if S.distance > S.maxDist then S.distance = S.maxDist end
S.angle = ( (Atan2( ex-0,ey-0 )*180 / Pi) - 180 ) * -1
S.percent = S.distance / S.maxDist

T.x = Cos( Rad(S.angle-90) ) * (S.maxDist * S.percent)
T.y = Sin( Rad(S.angle-90) ) * (S.maxDist * S.percent)

elseif “ended”== phase or “cancelled” == phase then
T.x0 = 0
T.y0 = 0
T.isFocus = false
–display.getCurrentStage():setFocus( nil )
display.getCurrentStage():setFocus( nil, event.id )

S.Timer = timer.performWithDelay( 33, S.onRelease, 0 )
S.Timer.MyStick = S
end
end[/lua] [import]uid: 49236 topic_id: 24143 reply_id: 97816[/import]