Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to bring the "self" from a listener to another function?
Started by gammabeam Nov 18 2010 09:03 PM

6 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

gammabeam

[GLOBAL: userInfoPane.html]
gammabeam
  • Enthusiast

  • 33 posts
  • Guests

Hi... it's me again... :P

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

-- ball collisionslocal 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	endendfunction removeBall(????)        ????:removeSelf()end


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!
uid: 11130 topic_id: 3784 reply_id: 303784


[TOPIC: post.html]
#2

jmp909

[GLOBAL: userInfoPane.html]
jmp909
  • Corona Geek

  • 1,328 posts
  • Corona SDK

this is one way... there are probably others..

stick the removeBall function before your ballCollides function

function removeBall(event)   local ball = event.source.ball -- this equals t2.ball .. see below   ball:removeSelf()end


then pass a parameter on the transition itself

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


[TOPIC: post.html]
#3

gammabeam

[GLOBAL: userInfoPane.html]
gammabeam
  • Enthusiast

  • 33 posts
  • Guests

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.

uid: 11130 topic_id: 3784 reply_id: 11764


[TOPIC: post.html]
#4

jmp909

[GLOBAL: userInfoPane.html]
jmp909
  • Corona Geek

  • 1,328 posts
  • Corona SDK

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
local ball=display.newImage("ball.png")function doSomething(obj)   print(obj==ball) -- true!endtransition.to(ball, {time=1000, onComplete=doSomething)


you can call that "obj" variable whatever you want... your original code shouldve worked, if you just replaced ??? with whatever variable you decide
uid: 6645 topic_id: 3784 reply_id: 11766


[TOPIC: post.html]
#5

gammabeam

[GLOBAL: userInfoPane.html]
gammabeam
  • Enthusiast

  • 33 posts
  • Guests

LOL we're kind of missing the concept here :P

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


[TOPIC: post.html]
#6

gammabeam

[GLOBAL: userInfoPane.html]
gammabeam
  • Enthusiast

  • 33 posts
  • Guests

Dammit, I've done it.
whatBall = selftransition.to(self, { time = 200, alpha = 0, onComplete = removeBall})


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!
uid: 11130 topic_id: 3784 reply_id: 11768


[TOPIC: post.html]
#7

jmp909

[GLOBAL: userInfoPane.html]
jmp909
  • Corona Geek

  • 1,328 posts
  • Corona SDK

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
uid: 6645 topic_id: 3784 reply_id: 12047



[topic_controls]
[/topic_controls]