transition.to back and forth... Stack Overflow OOP

Hello,
 
I am trying to make it so that a ‘virus’ in my game moves continuously… I am using transition.to to make it move to a random x,y coordinate, and onComplete it should move again to a new coordinate. 
 
For some reason the way I have it set up keeps giving me a stack overflow and it doesn’t wait for the delay that is set up… it just fires both functions repeatedly.
 
Any help will be GREATLY appreciated.
 

function Virus:moveAgain() print("moveAgain") local destinationX = math.random(45,275) local destinationY = math.random(45,435) self.\_transition = transition.to( self.\_visual, { time = 2000, alpha = 1, x = destinationX, y = destinationY, onComplete = Virus:moveVirus() } ) end function Virus:moveVirus(destinationX, destinationY) print("moveVirus") local destinationX = destinationX or math.random(45,275) local destinationY = destinationY or math.random(45,435) --print("Im in moveVirus... I will move in X:"..destinationX.." and Y: "..destinationY) self.\_transition = transition.to( self.\_visual, { time = 2000, alpha = 1, x = destinationX, y = destinationY, onComplete = Virus:moveAgain() } ) end

Hi @guillopuyol,

I think you could consolidate this to one function, not two. Instead of setting an exact destination X/Y, maybe you could make it choose a random position from the current location of the virus. So, on each completion of the transition, it simply picks a new location and moves there.

Best regards,

Brent

Brent,

thanks for the suggestion. That was my first option and I had the same result. This is the code I used originally and it still doesn’t wait the 2 seconds before calling the function over again and it results in stack overflow.
 

function Virus:moveVirus(destinationX, destinationY) print("moveVirus") local destinationX = destinationX or math.random(45,275) local destinationY = destinationY or math.random(45,435) --print("Im in moveVirus... I will move in X:"..destinationX.." and Y: "..destinationY) self.\_transition = transition.to( self.\_visual, { time = 2000, alpha = 1, x = destinationX, y = destinationY, onComplete = Virus:moveVirus() } ) end

Any ideas?

I think that 

“onComplete = Virus:moveVirus()”

is being executed immediately for every recursive iteration, thus causing an infinite loop.

edit: OK, after playing with the code I figured it out.

Virus.moveVirus () should not have the parenthesis. This is causing it to get called automatically.  Instead, you need to pass a reference to the function by removing the parenthesis.

onComplete = Virus:moveVirus

Hi @guillopuyol,

I think you could consolidate this to one function, not two. Instead of setting an exact destination X/Y, maybe you could make it choose a random position from the current location of the virus. So, on each completion of the transition, it simply picks a new location and moves there.

Best regards,

Brent

Brent,

thanks for the suggestion. That was my first option and I had the same result. This is the code I used originally and it still doesn’t wait the 2 seconds before calling the function over again and it results in stack overflow.
 

function Virus:moveVirus(destinationX, destinationY) print("moveVirus") local destinationX = destinationX or math.random(45,275) local destinationY = destinationY or math.random(45,435) --print("Im in moveVirus... I will move in X:"..destinationX.." and Y: "..destinationY) self.\_transition = transition.to( self.\_visual, { time = 2000, alpha = 1, x = destinationX, y = destinationY, onComplete = Virus:moveVirus() } ) end

Any ideas?

I think that 

“onComplete = Virus:moveVirus()”

is being executed immediately for every recursive iteration, thus causing an infinite loop.

edit: OK, after playing with the code I figured it out.

Virus.moveVirus () should not have the parenthesis. This is causing it to get called automatically.  Instead, you need to pass a reference to the function by removing the parenthesis.

onComplete = Virus:moveVirus