Multitouch To Rotate An Object

Hi! We are creating a bejeweled inspired puzzlegame for a school assignment. Basically what you do is rotate a cluster of 4 bricks on a board to create a combination of 4 (or more) bricks with the same color.

 

To rotate the bricks we have put a listener between each cluster of 4, and by tapping or swiping (depends on the controller you have chosen) on the listener, the bricks rotate. Example:

https://www.dropbox.com/s/5gz5pin3h77pdph/example1.png

 

What we want to create now is a multitouch controller where you hold one finger on the listener, and by tapping on either side, you rotate the cluster accordingly.

 

Here is the code so far:

 

[lua]function multiDetect(e) if e.phase == “began” or e.phase == “stationary” then --When the first finger is touching the screen, I’m saving the coordinates so
–that we can detect if the second finger is to the left or right. print("First finger is touching screen with id: " … tostring(e.id))
print("coor = " … e.xStart) --Saving the id of the event so we can detect if the first finger has been lifted.
firstTouch = e.id;
startX = e.xStart;

–Setting the target to the listener(I dont know if this is the way to do it);
display.getCurrentStage():setFocus(e.target, firstTouch); function spinThatShit(event) if event.phase == “began” and event.id ~= firstTouch then print("Second finger is touching, id: " … tostring(event.id));
print("The xPos of the second finger is: " … event.xStart); if event.xStart > startX then

 

–Second finger is to the right, rotating clockwise
rotateCW(e)

elseif event.xStart < startX then

 

–Second finger is to the left, rotating counterclockwise
rotateCCW(e) end
end
end

 

–Listening for the second finger
Runtime:addEventListener(“touch”, spinThatShit) elseif e.phase == “ended” or e.phase == “cancelled” and e.id == firstTouch then

 

–The first finger has been lifted.

–Setting the id to zero.
firstTouch = 0;
display.getCurrentStage():setFocus(e.target, nil);
print(“Lifting first finger”)

–Removing the eventlistener for the second touch.
Runtime:removeEventListener(“touch”, spinThatShit) end[/lua]

 

The problem is that we need to press outside the board. If you hit other listeners it wont rotate.
Also, if you press other listeners it detects that the first finger has been lifted. So when you actually lift the first finger the endedphase has been run twice. What we need is a way to be able to press wherever we want on the board with the second finger, and maybe make the other listeners inactive while the first finger is pressing the board. In theory the only value we need is the x-pos of the second finger to know which way to rotate the bricks. Hope some of you are able to help! 

 

Thanks!

LOL, nice name for a function.

 

How about this theory?

  1. Inside your function, when you put your finger down the first time, create a full-screen display object with a touch listener.
  2. If you lift your finger, that full-screen display object disappears.
  3. But, if you keep your finger down on it, the full-screen object will wait for the second touch.
  4. When it receives a new (second) touch, it keeps focus on that as well and stores the initial angle between both fingers.
  5. When you lift either finger, it gets the last angle it registered, which you will then determine the direction of the spin.

Thanks for the reply!  Creating a new display object to handle the second touch worked, but the there still is a problem. 
It seems like the listeners below the new displayobject still registers touches, as if the touch is going “through” the displayobject and hitting the listeners under. Is there a way to prevent this without having to deactivate the listeners? There are 49 of them, and I don’t know if that takes up unnecessary power.

Eureka! I discovered that the new displayobject had to:
[lua]return true;[/lua]

or else the touch was able to travel through it! Thanks for the help mister BeyondtheTech!
You are truly beyond my technical knowledge level!
 

LOL, nice name for a function.

 

How about this theory?

  1. Inside your function, when you put your finger down the first time, create a full-screen display object with a touch listener.
  2. If you lift your finger, that full-screen display object disappears.
  3. But, if you keep your finger down on it, the full-screen object will wait for the second touch.
  4. When it receives a new (second) touch, it keeps focus on that as well and stores the initial angle between both fingers.
  5. When you lift either finger, it gets the last angle it registered, which you will then determine the direction of the spin.

Thanks for the reply!  Creating a new display object to handle the second touch worked, but the there still is a problem. 
It seems like the listeners below the new displayobject still registers touches, as if the touch is going “through” the displayobject and hitting the listeners under. Is there a way to prevent this without having to deactivate the listeners? There are 49 of them, and I don’t know if that takes up unnecessary power.

Eureka! I discovered that the new displayobject had to:
[lua]return true;[/lua]

or else the touch was able to travel through it! Thanks for the help mister BeyondtheTech!
You are truly beyond my technical knowledge level!
 

hello, forgive for my question, but could someone pass me the code, took weeks trying to make the turn and drag objects style bubble ball and no way I go. If someone could pass me I would appreciate heart.

Sorry for my English!

Sure thing! Here is the code we used, hope you’ll find it useful!

[lua]
function multiDetect(e)
if e.phase == “began” or e.phase == “stationary” and ready == true and firstFinger == false then

firstFinger = true;
clusterHolder = display.newImageRect(“assets/clusterHolder.png”, _W/5, _W/5);
clusterHolder:setReferencePoint(display.CenterReferencePoint);
clusterHolder.x = e.target.x
clusterHolder.y = e.target.y

–When the first finger is touching the screen, I’m saving the coordinates so
–that we can detect if the second finger is to the left or right.

print("First finger is touching screen with id: " … tostring(e.id))
print("coor = " … e.xStart)

–Saving the id of the event so we can detect if the first finger has been lifted.

secondTapObj = display.newRect(0,0,_W,_H)
secondTapObj.alpha = 0
secondTapObj.isHitTestable = true;
firstTouch = e.id;
startX = e.xStart;
TheTarget = e.target;
display.getCurrentStage():setFocus(e.target, firstTouch);

–function spinThatShit(event)
function secondTapObj:touch(event)

if event.phase == “began” and event.id ~= e.id then

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

print("Second finger is touching, id: " … tostring(event.id));
print("The xPos of the second finger is: " … event.xStart);

if event.xStart > e.xStart then

rotateCW(e)

elseif event.xStart < e.xStart then

rotateCCW(e)

end
end
return true;
end

secondTapObj:addEventListener(“touch”, secondTapObj)
– ENDA EN VARIABEL and e.id == firstTouch
elseif e.phase == “ended” or e.phase == “cancelled” then
if clusterHolder ~= nil then
clusterHolder:removeSelf();
clusterHolder = nil;
end
firstFinger = false;
firstTouch = 0;
display.getCurrentStage():setFocus(e.target, nil);
print(“Lifting first finger”)
Runtime:removeEventListener(“touch”, spinThatShit)
secondTapObj:removeSelf();
end

end–ending multiDetect[/lua]

muchas gracias por el código, el problema es que ahora me da un error (bad argument #-2 to ‘insert’ (Proxy expected, got nil))

sorry, had written in Spanish

thank you very much for the code, the problem is that now I get an error (bad argument # -2 to ‘insert’ (Proxy expected, got nil))

hello, forgive for my question, but could someone pass me the code, took weeks trying to make the turn and drag objects style bubble ball and no way I go. If someone could pass me I would appreciate heart.

Sorry for my English!

Sure thing! Here is the code we used, hope you’ll find it useful!

[lua]
function multiDetect(e)
if e.phase == “began” or e.phase == “stationary” and ready == true and firstFinger == false then

firstFinger = true;
clusterHolder = display.newImageRect(“assets/clusterHolder.png”, _W/5, _W/5);
clusterHolder:setReferencePoint(display.CenterReferencePoint);
clusterHolder.x = e.target.x
clusterHolder.y = e.target.y

–When the first finger is touching the screen, I’m saving the coordinates so
–that we can detect if the second finger is to the left or right.

print("First finger is touching screen with id: " … tostring(e.id))
print("coor = " … e.xStart)

–Saving the id of the event so we can detect if the first finger has been lifted.

secondTapObj = display.newRect(0,0,_W,_H)
secondTapObj.alpha = 0
secondTapObj.isHitTestable = true;
firstTouch = e.id;
startX = e.xStart;
TheTarget = e.target;
display.getCurrentStage():setFocus(e.target, firstTouch);

–function spinThatShit(event)
function secondTapObj:touch(event)

if event.phase == “began” and event.id ~= e.id then

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

print("Second finger is touching, id: " … tostring(event.id));
print("The xPos of the second finger is: " … event.xStart);

if event.xStart > e.xStart then

rotateCW(e)

elseif event.xStart < e.xStart then

rotateCCW(e)

end
end
return true;
end

secondTapObj:addEventListener(“touch”, secondTapObj)
– ENDA EN VARIABEL and e.id == firstTouch
elseif e.phase == “ended” or e.phase == “cancelled” then
if clusterHolder ~= nil then
clusterHolder:removeSelf();
clusterHolder = nil;
end
firstFinger = false;
firstTouch = 0;
display.getCurrentStage():setFocus(e.target, nil);
print(“Lifting first finger”)
Runtime:removeEventListener(“touch”, spinThatShit)
secondTapObj:removeSelf();
end

end–ending multiDetect[/lua]

muchas gracias por el código, el problema es que ahora me da un error (bad argument #-2 to ‘insert’ (Proxy expected, got nil))

sorry, had written in Spanish

thank you very much for the code, the problem is that now I get an error (bad argument # -2 to ‘insert’ (Proxy expected, got nil))