Transition cancel problems

hello,
i am currently trying to make a group of fish fly down in groups of five.

My Problem is that i am canceling the fish in two places. The first place is in collision. When a collision touches the penguin it deletes. As in my code, you can see that i tried to cancel the following transition. The second place is in the transition. Once it completes its transition it deletes it!

Here is my code,

 function createFish () -- every 5 seconds this is called   
 randomstart = math.random(0,\_W) --- randomstart is where the starting x coordinate is for the set of fish  
 function startFish() -- in the 5 seconds this is called 5 times for 5 fishes to spawn  
  
 randomnumber2 = math.random(0,30) -- randomnumber2 is a random number for every single fish so they don't all spawn in a straight line.  
 fishey= display.newImageRect("fish.png", 45, 32) -- spaws fish  
 fishey.x = randomstart + randomnumber2  
 fishey.y = -50  
 physics.addBody(fishey) --- adds physics to fish  
  
  
 fisheyMove = transition.to(fishey, { time=2000, y=\_H + 150, ---moves the fish  
 onComplete = function(self) --- moves the fish  
 self.parent:remove(self); ----moves the fish  
 self = nil; --- moves the fish  
 end  
 })   
  
  
 fishey.collision = function(self, event) --- collision statement  
 if(event.phase == "began") then -- if collision begins...  
 if(event.other.type == "penguin") then -- and collides with the penguin then  
  
  
 audio.play(chew) --- play the sound  
 self:removeSelf()  
 self = nil  
 transition.cancel(fisheyMove) -- trys to cancel the fish  
  
 end  
  
 end  
 end  
 fishey:addEventListener("collision", fishey)  
  
  
 end  
 timer.performWithDelay(100, startFish, 5 )  
  
  
  
 end  
  
  
  
   
 timer.performWithDelay(100, createFish, -1 )  

Thanks for looking!

[import]uid: 24708 topic_id: 25376 reply_id: 325376[/import]

What exactly is the issue you are having?

If it is an error because the object is trying to be removed twice, do;

if fishey then
fishey:removeSelf()

rather than just
fishey:removeSelf()

That way it checks if it exists before trying to get rid of it.

If that isn’t the issue then please, elaborate further :slight_smile:

Peach [import]uid: 52491 topic_id: 25376 reply_id: 102511[/import]

Thanks for a quick reply, and sorry for not giving enough info!

That was definitely one of two problems i see when i test it.
I think the other problem is that i call startFish() in a group of five. I think the problem is when one of the fish is in the collision, and deletes while another fish hits the end of the transition… is it possible that it cant properly distinguish between the fishes?
Thanks [import]uid: 24708 topic_id: 25376 reply_id: 102519[/import]

Yes, funny you should ask that, I didn’t want to jump in with a suggestion for a table immediately without first knowing what the issue is. (I’m always a little worried about overwhelming people!)

So, right, yes!

BEFORE your spawn function;
[lua]local fishey = {}[/lua]

Then within your spawn function change;
[lua]fishey= display.newImageRect(“fish.png”, 45, 32)[/lua]
to;
[lua]fishey[#fishey+1]= display.newImageRect(“fish.png”, 45, 32)[/lua]

And then every “fishey” after that in your code up there change to;
[lua]fishey[#fishey][/lua]

Let me know how you go with that - it’s naming them all fishey1, fishey2 … fishey1000, etc.

Peach :slight_smile: [import]uid: 52491 topic_id: 25376 reply_id: 102543[/import]