[Resolved] placing LOTS of objects, each to apply slightly different force to character

Hi!

I need to place lots of touch handlers, giving slighty different force to my ball in the game. I know the code for this, but doing it as my code shows below would be way to many lines. I need to place 6 rows of circles, with 10 in each row…

Here’s the code for just 2 of them, just to see what I’m trying to achieve.

I probably should put the x and y parameters in a table, and doing the same thing with the force that going to be applied. But I haven’t figured out how to do this properly. Do I have to have 60 functions in the .lua file for this?

Any help would be highly appreciated!!

Thanks so much!

[lua] local c11 = display.newCircle( 250, 280, 6 )
c11:setFillColor(128,128,128)

local c12 = display.newCircle( 230, 280, 6 )
c12:setFillColor(128,128,128)

function mc11(event)
if (canjump == “true”) then
ball:applyForce( 0, -450, ball.x, ball.y )
ball:applyTorque( 12 )
end
end

function mc12(event)
if (canjump == “true”) then
ball:applyForce( 0, -650, ball.x, ball.y )
ball:applyTorque( 10 )
end
end

c11:addEventListener ( “touch”, mc11 )
c12:addEventListener ( “touch”, mc12 )[/lua] [import]uid: 187595 topic_id: 32202 reply_id: 332202[/import]

Hi there,
Certainly 60 functions would be insane. :slight_smile: Fortunately you can accomplish your goal with 1 function.
Like this:

--create a table to store your objects:  
local touchObjects = {}  
--now create your objects  
touchObjects[1] = display.newCircle( 250, 280, 6 )  
touchObjects[1]:setFillColor(128,128,128)  
touchObjects[1].xForce = 100 ; touchObjects[1].yForce = 230 --specify x/y force for each  
touchObjects[1].torque = 12 --also specify the torque it will apply  
touchObjects[2] = --repeat process...  
  
local function onTouch( event )  
  
 local touched = event.target --event.target is the circle touched  
 if ( canjump == "true" and event.phase == "began" ) then  
 ball:applyForce( touched.xForce, touched.yForce, ball.x, ball.y )  
    ball:applyTorque( touched.torque )  
  end  
  
end  
  
--apply listeners using a loop  
for i=1,#touchObjects do  
 touchObjects[i]:addEventListener ( "touch", onTouch )  
 --you can, and must, eventually remove the listeners in a similar way, on scene cleanup  
end  

That should do it, unless I made a small syntax error (typing on my iPad, it happens sometimes!)

Best regards,
Brent Sorrentino [import]uid: 9747 topic_id: 32202 reply_id: 128240[/import]

Brent, Many thanks!

Worked great! [import]uid: 187595 topic_id: 32202 reply_id: 128248[/import]

Hi there,
Certainly 60 functions would be insane. :slight_smile: Fortunately you can accomplish your goal with 1 function.
Like this:

--create a table to store your objects:  
local touchObjects = {}  
--now create your objects  
touchObjects[1] = display.newCircle( 250, 280, 6 )  
touchObjects[1]:setFillColor(128,128,128)  
touchObjects[1].xForce = 100 ; touchObjects[1].yForce = 230 --specify x/y force for each  
touchObjects[1].torque = 12 --also specify the torque it will apply  
touchObjects[2] = --repeat process...  
  
local function onTouch( event )  
  
 local touched = event.target --event.target is the circle touched  
 if ( canjump == "true" and event.phase == "began" ) then  
 ball:applyForce( touched.xForce, touched.yForce, ball.x, ball.y )  
    ball:applyTorque( touched.torque )  
  end  
  
end  
  
--apply listeners using a loop  
for i=1,#touchObjects do  
 touchObjects[i]:addEventListener ( "touch", onTouch )  
 --you can, and must, eventually remove the listeners in a similar way, on scene cleanup  
end  

That should do it, unless I made a small syntax error (typing on my iPad, it happens sometimes!)

Best regards,
Brent Sorrentino [import]uid: 9747 topic_id: 32202 reply_id: 128240[/import]

Brent, Many thanks!

Worked great! [import]uid: 187595 topic_id: 32202 reply_id: 128248[/import]