How to bring the "self" from a listener to another function?

Hi… it’s me again… :stuck_out_tongue:

I’m trying to come up with a solution to this:

[lua]-- ball collisions
local function ballCollides( self, event )
if ( event.phase == “ended” ) then
–print( self.name … ": collision ended with " … event.other.name )
if event.other.name == “The Square” then
self.bonus = self.bonus + 1
print("Ball bonus: " … self.bonus)
elseif event.other.name == “The Ground” then
self.bouncesToDisable = self.bouncesToDisable - 1
print("Bounces to disable Ball: " … self.bouncesToDisable)
if self.bouncesToDisable < 0 then
transition.to(self, { time = 500, alpha = 0, onComplete = removeBall})
end
end
end
end

function removeBall(???)
???:removeSelf()
end[/lua]

Basically, it checks the collisions on a ball.

  • if it hits the square, a bonus is awarded
  • if it hits the ground a few times, there’s an alpha transition, and at the end I wanted it removed.

Problem is, since I’m making the function ready for my tons-o-balls creator, I can’t simply put a “ball:removeSelf()” on my function.

So, how can I pass the “self” value to this new function? I couldn’t find anything in the documentation.
The messy way I’m trying to avoid is creating a FOR testing the alpha of each ball and removing those with alpha=0… But I think there’s a cleaner way of doing it! [import]uid: 11130 topic_id: 3784 reply_id: 303784[/import]

this is one way… there are probably others…

stick the removeBall function before your ballCollides function

[lua]function removeBall(event)
local ball = event.source.ball – this equals t2.ball … see below
ball:removeSelf()
end[/lua]

then pass a parameter on the transition itself

[lua]local t2 = transition.to(self, { time = 500, alpha = 0, onComplete = removeBall})
t2.ball = self[/lua] [import]uid: 6645 topic_id: 3784 reply_id: 11548[/import]

Hey JMP!

Thanks for the answer!
However, that doesn’t seem to work. The terminal states I’m asking for a nil value.
If I use a “print(event.source)” I also get a “nil”.

Reading the documentation, I notice the event.source is tied only to timer events. Could this be a lead on this problem?

I’ll be tweaking this, trying to find an answer. If I find anything, I’ll post it here.

[import]uid: 11130 topic_id: 3784 reply_id: 11764[/import]

sorry you’re right about event.source, that’s for timers

for transitions the “event” parameter in the callback function is the object itself

so [lua]local ball=display.newImage(“ball.png”)

function doSomething(obj)
print(obj==ball) – true!
end

transition.to(ball, {time=1000, onComplete=doSomething)[/lua]

you can call that “obj” variable whatever you want… your original code shouldve worked, if you just replaced ??? with whatever variable you decide [import]uid: 6645 topic_id: 3784 reply_id: 11766[/import]

LOL we’re kind of missing the concept here :stuck_out_tongue:

The problem is, I want to remove that specific ball that triggered the the ballsCollide function! [import]uid: 11130 topic_id: 3784 reply_id: 11767[/import]

Dammit, I’ve done it.
[lua]whatBall = self
transition.to(self, { time = 200, alpha = 0, onComplete = removeBall})[/lua]

Then I call the “whatBall” to remove it!
It has to be global, though…

I think that’ll do it, now I’ll test it with multiple balls! [import]uid: 11130 topic_id: 3784 reply_id: 11768[/import]

I don’t understand. When you call transition.to(self,…) that’s already passing the specific instance to the onComplete function. Are you sure self is referring to the ball in your collision function? If it is your original code should work anyway . Just replace your ??? with any variable name . Don’t forget to define your removeball function before your oncollision function in your code [import]uid: 6645 topic_id: 3784 reply_id: 12047[/import]