transition loop problem

Alternately, you can do this if the bullet gets destroyed:

function bullet.onComplete( self ) if( self.removeSelf == nil ) then return end -- not a valid object any more self.x = self.x0 self.y = self.y0 transition.to( bullet, { time=2000, x=300, onComplete = bullet } ) end

Now, when you do this or the bullet is otherwise destroyed the transition will stop on its own at the next onComplete:

display.remove( bullet )

@Roaminggamer : thx sir… sry for late replay
btw i just modify ur code a bit and stil got error when go to other scene…

here my code :
[lua]
bullet = display.newImageRect(“bullet”,30,30)
bulet.x=enemy.x
bulet.y=enemy.y
physic.adBody(bulet,“dynamic”)
bullet.gravityScale=0
bullet.isSensor=true

transition.to(bullet,{time=2000,x=bullet.x-400,delay=4000,onComplete=loopBullet})

function loopBullet()
   bullet.x =bullet.x+400
transition.to(bullet,{time=2000,x=bullet.x-400,delay=4000,onComplete=loopBullet})
  end
[/lua]

everything actualy work… but i duno how to cancel “onComplete” when go to another scene??

its keep calling bullet and got some error later :frowning:

1 - Use a table listener not a global function.

2 - In general you’re not using the keyword local when you should.  If you have to use a global to make your code work, then you’re not understanding and dealing with scope properly.

(I am assuming the above code is untested because of the typos: bullet vs bulet)

This is something like what you need:

local bullet = display.newImageRect("bullet",30,30) physic.adBody( bullet, "dynamic" ) bullet.gravityScale=0 bullet.isSensor=true bullet.x = enemy.x bullet.y = enemy.y bullet.x0 = bullet.x -- track original position of bullet bullet.y0 = bullet.y -- track original position of bullet function bullet.onComplete( self ) -- 'self' is bullet -- Extra saftey code. This will trigger if 'self' is no longer a valid object. if( self.removeSelf == nil ) then return end -- restore to original position self.x = self.x0 self.y = self.y0 -- Transition 'self' and pass self for 'onComplete' arg. -- Corona will detect that self (bullet) has a field 'onComplete' containing a reference to -- a function. transition.to( self, { time=2000, x = self.x - 400, delay = 4000, onComplete = self } ) end bullet:onComplete() -- Call onComplete manually to do first transistion. --As soon as bullet is removed via (display.remove() or obj:removeSelf) the transition is canceled.

Note: This is the ‘same’ example (more elaborate) that I gave previously.  Don’t re-write it to be a global function and expect it to work.  A global function will need to be written very differently and I won’t give an example because its a bad practice (for this usage). 

Note: By default composer doesn’t destroy the scene and thus won’t destroy ‘bullet’ for you simply because you go to another scene.  

You’ll have to do it manually or configure Composer to ‘always’ destroy scenes on exit.

https://docs.coronalabs.com/daily/api/library/composer/recycleOnSceneChange.html

The only time Composer automatically destroys scenes if if a memory shortage is detected.  

All other destroy cases are the result of your coding choices.

@Roaminggamer : aww sry for typor sir…
btw its work like charm… the error was solved.
im still using storyboard and should moving to composer soon.
thx for ur explaintation sir :smiley: