how to access a object from inside another object:tap event

I have a button that when “tapped”, I want to apply an LinearImpuse(upwards on the yAXIS)
to another object called “ball”

When I issue the appliLinearImpulse from within the function button:tap( event)
I get a null error call at this line of code, “ball:applyLinearImpuse(0,-0.2,ball.x,ball.y)”

Guess i need to know how to “pass” that object into that function “button” from the TAP event ?

I tried including the {ball} inside the function call button Argument/Parameter list.
Example, button:addEventListener(“tap”, button, {ball})

But it did not work. :frowning:

Any help would be appreciated.

Thanks

Here is the code…
local physics = require( “physics” )
physics.setDrawMode( “normal” ) – the default Corona renderer, with no collision outline

physics.setPositionIterations(9)

local lid = display.newImage( “top.png” )
lid.x = 125; lid.y = -31
physics.addBody( lid, “static”, { friction=0.0001, bounce=0.2 } )

local ground = display.newImage( “ground.png” )
ground.x = 190; ground.y = 511
physics.addBody( ground, “static”, { friction=0.0001, bounce=0.2 } )

local r_side = display.newImage( “container.png” )
r_side.x = -65 ; r_side.y = 295
physics.addBody( r_side, “static”, { friction=0.5, bounce=0.2 } )

local l_side = display.newImage( “container2.png” )
l_side.x = 385 ; l_side.y = 295
physics.addBody( l_side, “static”, { friction=0.5, bounce=0.2 } )

local ball = display.newImage( “ball.png” )
ball.x = 175; ball.y = 0; ball.rotation = 0
physics.addBody( ball, { density=99, friction=25.5, bounce=.4 } )

local button = display.newImage(“button.png”)
button.x = display.contentWidth/2
button.y = display.contentHeight - 50

function button:tap( event)
ball:applyLinearImpuse(0,-0.2,crate.x,crate.y)
return true
end

button:addEventListener(“tap”, button)
[import]uid: 11094 topic_id: 15340 reply_id: 315340[/import]

Maybe this will help you!!!
[lua]local physics = require(“physics”)
physics.start()
display.setStatusBar( display.HiddenStatusBar )

local top = display.newRect( 0, 0, 320, 10 )
physics.addBody(top, “static”, { friction=0.0001, bounce=0.2 })
local bottom = display.newRect( 0, 470, 320, 10 )
physics.addBody(bottom, “static”, { friction=0.0001, bounce=0.2 })
local left = display.newRect( 0,0 , 10, 480 )
physics.addBody(left, “static”, { friction=0.0001, bounce=0.2 })
local right = display.newRect( 310, 0, 10, 480 )
physics.addBody(right, “static”, { friction=0.0001, bounce=0.2 })

top:setFillColor(120,200,20)
bottom:setFillColor(120,200,20)
left:setFillColor(120,200,20)
right:setFillColor(120,200,20)

local ball = display.newCircle( 170,50,10 )
ball:setFillColor(80,10,200)
–ball.x = 175; ball.y = 0; ball.rotation = 0

–here I changed the density and friction , because you used huge values for those
physics.addBody( ball, { density=0.9, friction=0.5, bounce=0.4 } )

local button = display.newRect(150,460,80,40)
button.x = display.contentWidth/2+100
button.y = display.contentHeight - 40
button.text=display.newText(“Buton1”,button.x-0.25*button.width ,button.y-10,nil,12)

button:setFillColor(250,0,0)
function button:tap( event)

–you can modify the y value im applyLinearImpulse
ball:applyLinearImpulse(0,-2,ball.x,ball.y)
return true
end

button:addEventListener(“tap”, button)[/lua] [import]uid: 65047 topic_id: 15340 reply_id: 56630[/import]

where does that crate come from ?
it should be
[lua]ball:applyLinearImpuse(0,-0.2,ball.x,ball.y) [/lua] [import]uid: 71210 topic_id: 15340 reply_id: 56660[/import]

yes, i thought i had changed all my crate “references” to “ball”. sorry.
but yes they are all indeed named correctly (i.e. ball.x, ball.y)

You know, I guess I could make the “BALL” into a global varaible(table),
since I only have one “ball” per screen/level it would not create that much overhead.

But I really wanted to know how to do it with argument passing.

[import]uid: 11094 topic_id: 15340 reply_id: 56777[/import]

works like a charm.

thanks for showing me how with regular “display” objects.

thanks thanks :smiley: [import]uid: 11094 topic_id: 15340 reply_id: 56779[/import]