transition loop problem

i wanna make bullet moving using transition and loop but its not work.
[lua]
transition.to(bullet, {time=2000, x=300, iterations=-1,transition=easing.continuousLoop})
[/lua]

bullet moving but not looping… its bug??

Try this:

bullet.x0 = bullet.x bullet.y0 = bullet.y function bullet.onRepeat( self ) self.x = self.x0 self.y = self.y0 end transition.to( bullet, { time=2000, x=300, iterations=-1, onRepeat = bullet })

@roaminggamer : thx for replay sir… but still error “arithmetic on field ‘?’ (a table value)”

pls help sir…
this my code
[lua]
bullet=display.newRect(30,30,30,30)
physics.addBody(bullet,“dynamic”)
bullet.x=600
bullet.y=300
bullet.isSensor=true
bullet.gravityScale=0

transition.to(bullet,{time=2000,x=200,iterations=-1,transition=easing.continuousLoop})
camera:add(bullet,1,false)
[/lua]

local bullet = display.newRect(30,30,30,30) bullet.x=600 bullet.y=300 bullet.x0 = bullet.x bullet.y0 = bullet.y physics.addBody(bullet,"dynamic") bullet.isSensor=true bullet.gravityScale=0 camera:add(bullet,1,false) function bullet.onRepeat( self ) self.x = self.x0 self.y = self.y0 end transition.to( bullet, { time=2000, x=300, iterations=-1, onRepeat = bullet })

Also, don’t use ‘transition=easing.continuousLoop’, in fact where did you see this?  I don’t see it listed as an official easing, but maybe I missed it.  Please share a link to the docs/page where you found this.

I don’t use onRepeat.  I’d do what you’re trying this way:

local bullet = display.newRect(30,30,30,30) bullet.x = 600 bullet.y = 300 bullet.x0 = bullet.x bullet.y0 = bullet.y physics.addBody(bullet,"dynamic") bullet.isSensor=true bullet.gravityScale=0 camera:add(bullet,1,false) function bullet.onComplete( self ) self.x = self.x0 self.y = self.y0 transition.to( bullet, { time=2000, x=300, onComplete = bullet } ) end bullet:onComplete()

@Roaminggamer : wow thx for replay sir… ill use it soon…

btw how stop this transition/loop when go to another scene??
this code right??
[lua]
transition.cancel()
[/lua]

the loop still active and give a nil value :frowning:

transition.cancel( bullet ) -- bullet has to be in scope for this to work

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:

Try this:

bullet.x0 = bullet.x bullet.y0 = bullet.y function bullet.onRepeat( self ) self.x = self.x0 self.y = self.y0 end transition.to( bullet, { time=2000, x=300, iterations=-1, onRepeat = bullet })

@roaminggamer : thx for replay sir… but still error “arithmetic on field ‘?’ (a table value)”

pls help sir…
this my code
[lua]
bullet=display.newRect(30,30,30,30)
physics.addBody(bullet,“dynamic”)
bullet.x=600
bullet.y=300
bullet.isSensor=true
bullet.gravityScale=0

transition.to(bullet,{time=2000,x=200,iterations=-1,transition=easing.continuousLoop})
camera:add(bullet,1,false)
[/lua]

local bullet = display.newRect(30,30,30,30) bullet.x=600 bullet.y=300 bullet.x0 = bullet.x bullet.y0 = bullet.y physics.addBody(bullet,"dynamic") bullet.isSensor=true bullet.gravityScale=0 camera:add(bullet,1,false) function bullet.onRepeat( self ) self.x = self.x0 self.y = self.y0 end transition.to( bullet, { time=2000, x=300, iterations=-1, onRepeat = bullet })

Also, don’t use ‘transition=easing.continuousLoop’, in fact where did you see this?  I don’t see it listed as an official easing, but maybe I missed it.  Please share a link to the docs/page where you found this.

I don’t use onRepeat.  I’d do what you’re trying this way:

local bullet = display.newRect(30,30,30,30) bullet.x = 600 bullet.y = 300 bullet.x0 = bullet.x bullet.y0 = bullet.y physics.addBody(bullet,"dynamic") bullet.isSensor=true bullet.gravityScale=0 camera:add(bullet,1,false) function bullet.onComplete( self ) self.x = self.x0 self.y = self.y0 transition.to( bullet, { time=2000, x=300, onComplete = bullet } ) end bullet:onComplete()

@Roaminggamer : wow thx for replay sir… ill use it soon…

btw how stop this transition/loop when go to another scene??
this code right??
[lua]
transition.cancel()
[/lua]

the loop still active and give a nil value :frowning:

transition.cancel( bullet ) -- bullet has to be in scope for this to work