Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How can I use event.complete/listeners properly? — Gideros Forum

How can I use event.complete/listeners properly?

AxlFlameAxlFlame Member
edited March 2013 in Game & application design
Hi everyone,
As always I'm stuck with a new issue, this time, regarding the use of the event: EVENT_COMPLETE dispatched when an Movie Clip ends.
My problem is this: I have this animation of an eraser in my game class (BonecosDesenho) that is called by another .lua when an obstacle in my game is touched by the player. After the eraser's animation ends, i want to remove it from the screen, as it will happen again and again.

How can I do that? I found on the API that when a movie clip ends it dispatches the 'event_complete' event. I thought it was perfect for me, but I'm having too much trouble understanting where should I put the listener and how to manage the removal of the animation from the screen, as its created by a function and somehow when I tried to use removeChild it said that my container for the movie clip was null...

Any help would be deeply appreciated!


This is the function that creates the animation and put it where the obstacle is:
function BonecosDesenho:animaBorracha(eventx, eventy)
	local borrachaApagando = TexturePack.new(
	"images/Minigames/Bonecos/Desenho/Sprite Sheets/EraserSS.txt",
	"images/Minigames/Bonecos/Desenho/Sprite Sheets/EraserSS.png")
 
	self.borracha = MovieClip.new({
		{1,3,Bitmap.new(borrachaApagando:getTextureRegion("Eraser1.png"))},
		{4,6,Bitmap.new(borrachaApagando:getTextureRegion("Eraser2.png"))},
		{7,9,Bitmap.new(borrachaApagando:getTextureRegion("Eraser3.png"))},
		{10,12,Bitmap.new(borrachaApagando:getTextureRegion("Eraser4.png"))},
		{13,15,Bitmap.new(borrachaApagando:getTextureRegion("Eraser3a.png"))},
		{14,16,Bitmap.new(borrachaApagando:getTextureRegion("Eraser2a.png"))},
		{17,19,Bitmap.new(borrachaApagando:getTextureRegion("Eraser1a.png"))},
		{20,22,Bitmap.new(borrachaApagando:getTextureRegion("Eraser5.png"))},
		{23,25,Bitmap.new(borrachaApagando:getTextureRegion("Eraser1b.png"))},
		{26,28,Bitmap.new(borrachaApagando:getTextureRegion("Eraser2b.png"))},
		{29,31,Bitmap.new(borrachaApagando:getTextureRegion("Eraser3b.png"))},
		{32,34,Bitmap.new(borrachaApagando:getTextureRegion("Eraser6.png"))},
		{35,37,Bitmap.new(borrachaApagando:getTextureRegion("Eraser7.png"))},
		{38,40,Bitmap.new(borrachaApagando:getTextureRegion("Eraser8.png"))}
	})
 
	self.borracha:setStopAction(40)
 
	self.contBorracha = self.borracha
	self:addChild(self.contBorracha)
	self.contBorracha:setX(_G.enemy:getX()-50)
	self.contBorracha:setY(eventy-140)
	self.contBorracha:setVisible(true)
end
And this is the function that calls it on the other .lua I mentioned:
function bDobstaculos:apagaInimigo(event, flag)
 
	flag = isTouched
 
	if enemy:hitTestPoint(event.x, event.y) and flag == true then
		self:getParent():animaBorracha(event.x, event.y)
		self.fixture = nil;
		b2World:destroyBody(self.body);
		self.body = nil;
		self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self);
		self:getParent():removeChild(self);	
	end
 
	isTouched = nil
end

Comments

  • I think it would help if you could simplify your code and remove any unnecessary lines (+ use english and explicit words so we have a better understanding of what you are trying to achieve).
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    That should be somewhere along the lines:
    Add this inside animaBorracha method
    self.borracha:addEventListener(Event.COMPLETE, self.removeAnim, self)
    And then additional method of BonecosDesenho
    function BonecosDesenho:removeAnim()
        self:removeChild(self.borracha)
    end
  • edited March 2013
    try this:
    self.borracha:addEventListener(Event.COMPLETE, 
    	function()
    		self.borracha:removeFromParent()
    		self:removeFromParent()
    		self = nil
    		--print("movieclip removed")
    	end
    )

    Likes: AxlFlame

    +1 -1 (+1 / -0 )Share on Facebook
  • Thanks a lot, @Ar2rsawseen and @Thanquan1512! Works perfectly.

    Now that I see the solution I feel a little embarassed, haha. I had a hard time understanting where should I put the listeners and using events properly.

    Anyways, thanks again!
Sign In or Register to comment.