[Resolved] Problem with continuous touch

Hi there! :slight_smile:

I have a few buttons wich will apply different force to my character. Got that code working good by having an array with different force values, then I do this:

[lua]local function onTouch( event )

local touched = event.target --event.target is the button touched
if ( canjump == “true” and event.phase == “began” ) then
character:applyForce( touched.xForce, touched.yForce, stick.x, stick.y )
end

end

–and I got the listeners applied using a loop
for i=1,#touchObjects do
touchObjects[i]:addEventListener ( “touch”, onTouch )
end[/lua]

Works really good. However, now I want the charcter to keep jumping when button is pressed. I got the code for this using just a single button, and it works great, but how can I refernce the “touched.xForce” from outside of the onTouch function?

Here’s my continuous touch function

[lua]function movechar()

if ( canjump == “true” and touched == “yes” ) then
character:applyForce( 0, -580, stick.x, stick.y ) --THIS works fine
character:applyForce( touched.xForce, touched.yForce, stick.x, stick.y ) --THIS is not working since “touched” does not keep the value, returns nil.
elseif touched == false then
return false
end
end

local jumptimer
jumptimer = timer.performWithDelay(30, movechar, 0)[/lua]
So is there any work around for this?

All my best! [import]uid: 187595 topic_id: 32695 reply_id: 332695[/import]

Can’t you just make a variable outside of the functions, like so:

  
--the new variables  
local myTouchX = 0  
local myTouchY = 0  
  
local function onTouch( event )  
   
 local touched = event.target --event.target is the button touched  
  
myTouchX = touched.xForce  
myTouchY = touched.yForce  
  
 if ( canjump == "true" and event.phase == "began" ) then  
 character:applyForce( touched.xForce, touched.yForce, stick.x, stick.y )  
 end  
   
end  
   
 --and I got the listeners applied using a loop  
for i=1,#touchObjects do  
 touchObjects[i]:addEventListener ( "touch", onTouch )  
end  
  
function movechar()  
  
if ( canjump == "true" and touched == "yes" ) then  
 character:applyForce( 0, -580, stick.x, stick.y ) --THIS works fine  
 character:applyForce( myTouchX, myTouchY, stick.x, stick.y ) --Now using the variables  
elseif touched == false then  
 return false  
 end  
end  
   
   
local jumptimer  
jumptimer = timer.performWithDelay(30, movechar, 0)  

Obviously you’ll also need to reset them or whatever you need to do at some point. [import]uid: 84115 topic_id: 32695 reply_id: 129977[/import]

Fantastic! Worked great! Thanks a lot! [import]uid: 187595 topic_id: 32695 reply_id: 129978[/import]

Can’t you just make a variable outside of the functions, like so:

  
--the new variables  
local myTouchX = 0  
local myTouchY = 0  
  
local function onTouch( event )  
   
 local touched = event.target --event.target is the button touched  
  
myTouchX = touched.xForce  
myTouchY = touched.yForce  
  
 if ( canjump == "true" and event.phase == "began" ) then  
 character:applyForce( touched.xForce, touched.yForce, stick.x, stick.y )  
 end  
   
end  
   
 --and I got the listeners applied using a loop  
for i=1,#touchObjects do  
 touchObjects[i]:addEventListener ( "touch", onTouch )  
end  
  
function movechar()  
  
if ( canjump == "true" and touched == "yes" ) then  
 character:applyForce( 0, -580, stick.x, stick.y ) --THIS works fine  
 character:applyForce( myTouchX, myTouchY, stick.x, stick.y ) --Now using the variables  
elseif touched == false then  
 return false  
 end  
end  
   
   
local jumptimer  
jumptimer = timer.performWithDelay(30, movechar, 0)  

Obviously you’ll also need to reset them or whatever you need to do at some point. [import]uid: 84115 topic_id: 32695 reply_id: 129977[/import]

Fantastic! Worked great! Thanks a lot! [import]uid: 187595 topic_id: 32695 reply_id: 129978[/import]

Glad I could help, it’s always the simplest solutions that are the hardest to find :slight_smile: [import]uid: 84115 topic_id: 32695 reply_id: 130007[/import]

Glad I could help, it’s always the simplest solutions that are the hardest to find :slight_smile: [import]uid: 84115 topic_id: 32695 reply_id: 130007[/import]