Jump to content

Trouble with timer onComplete


Shantred
 Share

Recommended Posts

I'm trying to use a custom timer to "blink" an item on screen and then destroy it. The actual blinking is fine, but I can't seem to destroy it. 

 

The timer fires as soon as the sprite collides with the ground. Here is the code for the timer:

function diamondDecay(diamond){	// We need to blink the diamond every 50ms for 2 seconds	// That means it will blink 40 times	blinkTimer = game.time.create(true);	blinkTimer.repeat(50, 40, itemBlink, this, diamond);	blinkTimer.onComplete.add(removeDiamond, diamond);	blinkTimer.start();}

diamondDecay is the function called on collision with diamond just being the sprite itself. The repeat of the timer executes the appropriate number of times, then when trying to call removeDiamond(which expects a sprite and calls .kill()), the game halts and I get an undefined is not a function error from inside removeDiamond.

 

I tried outputting what removeDiamond is receiving and it seems that it is being passed the timer object rather than 'diamond' like I specified. 

 

Any ideas?

Link to comment
Share on other sites

Just wanted to update this thread saying that I found a solution to my problem, but not through timers.

 

To create the blinking effect, what I ended up having to do is create a 2 frame spritesheet of the diamond where the second frame was blank and add an animation. When the diamond collides with a platform, it begins this animation and kills the sprite when the animation finishes. Here is what the code looks like:

function update() {        //....	game.physics.arcade.collide(diamonds, platforms, diamondDecay, null, this);        //....}function diamondDecay(diamond){			if ( !diamond.animations.getAnimation('blink').isPlaying ) {		diamond.animations.getAnimation('blink').play(15, false, true);	}	}// This is called elsewhere in update under certain conditionsfunction spawnDiamonds() {	//....	diamond.animations.add('blink', [0,1,0,1,0,1,0,1,0,1,0,1,0,1,0], 15, false);	//....}

That said, I'm not really a fan of how this works and it is a bit hack-ish. Anyone who has suggestions of other or betters ways to do this, I'm all ears.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...