Problem in positioning function(runner game)

I am making sort of a runner game which the player avoids obstacles in order to beat his highscore.

Trying not to use the physics engine in the game hope it’s possible.

The only problem for me right now that when the obstacle “collide” with the player I cannot call the spawnController function because it cannot recognize it. Cannot change spawnController position becuase if I’ll put it upword where the collision function can recognize it another function will be problematic… its a loop.

Ill paste the relevant code down here hope someone can find a creative and clean solution to this issue.

local function scrollBranch( self ) &nbsp; &nbsp; if( self.x \< 0 ) then &nbsp; &nbsp; &nbsp; &nbsp; Runtime:removeEventListener( "enterFrame", self );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self:removeSelf(); &nbsp; &nbsp; &nbsp; &nbsp; self = nil; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; if ( self.x \<= sloth.x ) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( self.y == sloth.y ) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stopScrollBackground(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Runtime:removeEventListener( "touch", onScreenTouch ); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --spawnController( "stop" );\*\*\*\*\* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; self.x = self.x - self.speed; &nbsp; &nbsp; end end local function spawnObstacles() &nbsp; &nbsp; local obs; &nbsp; &nbsp; &nbsp; &nbsp; local obsName; &nbsp; &nbsp; &nbsp; &nbsp; local obsW; &nbsp; &nbsp; &nbsp; &nbsp; local obsH; &nbsp; &nbsp; local objNum = mRand( 3 ); &nbsp; &nbsp; if ( objNum == 1 ) then &nbsp; &nbsp; &nbsp; &nbsp; obsName = "logo"; &nbsp; &nbsp; &nbsp; &nbsp; obsW = 10; &nbsp; &nbsp; &nbsp; &nbsp; obsH = 10; &nbsp; &nbsp; elseif ( objNum == 2 ) then &nbsp; &nbsp; &nbsp; &nbsp; obsName = "logo"; &nbsp; &nbsp; &nbsp; &nbsp; obsW = 20; &nbsp; &nbsp; &nbsp; &nbsp; obsH = 20; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; obsName = "logo"; &nbsp; &nbsp; &nbsp; &nbsp; obsW = 30; &nbsp; &nbsp; &nbsp; &nbsp; obsH = 30; &nbsp; &nbsp; end &nbsp; &nbsp; obs = display.newImageRect( "images/" .. obsName .. ".png", obsW, obsH ); &nbsp; &nbsp; obs.x = \_W + \_CX; &nbsp; &nbsp; --math.randomseed( os.time() ) &nbsp; &nbsp; local obsY = mRand( 2 ); &nbsp; &nbsp; if( obsY == 1 ) then &nbsp; &nbsp; &nbsp; &nbsp; obs.y = \_H \* 0.41; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; obs.y = \_H \* 0.41 + 50; &nbsp; &nbsp; &nbsp; &nbsp; obs.yScale = -1; &nbsp; &nbsp; end &nbsp; &nbsp; obs.speed = mRand( 6, 8 ); &nbsp; &nbsp; spawnedObjects[#spawnedObjects + 1] = obs; &nbsp; &nbsp; --spawnedObjects[#spawnedObjects].enterFrame = scrollBranch;\*\*\*\*\* &nbsp; &nbsp; Runtime:addEventListener( "enterFrame", spawnedObjects[#spawnedObjects] ); end local function spawnController( action, params ) &nbsp; &nbsp; if ( spawnTimer and ( action == "start" or action == "stop" ) ) then &nbsp; &nbsp; &nbsp; &nbsp; timer.cancel( spawnTimer ) &nbsp; &nbsp; end &nbsp; &nbsp; if ( action == "start" ) then &nbsp; &nbsp; &nbsp; &nbsp; local spawnTime = params.spawnTime or 1000 &nbsp; &nbsp; &nbsp; &nbsp; spawnTimer = timer.performWithDelay( spawnTime, function() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spawnObstacles();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end, 0 ) &nbsp; &nbsp; elseif ( action == "pause" ) then &nbsp; &nbsp; &nbsp; &nbsp; timer.pause( spawnTimer ) &nbsp; &nbsp; elseif ( action == "resume" ) then &nbsp; &nbsp; &nbsp; &nbsp; timer.resume( spawnTimer ) &nbsp; &nbsp; end end

If you have further question or do not understand my code please reply here I’ll answer in seconds : )

Thanks,

Itay

The only problem for me right now that when the obstacle “collide” with the player I cannot call the spawnController function because it cannot recognize it. 

Do you ask how to detect collision between obstacle and player?

Nah… I can detect the collision beween them. I marked the problematic code with “*****”. I cannot call spawnController with scrollBranch function and cannot change the order of them as far as I know. I’m asking if theres a simple way to make that work.

I get what you mean. I suggest that  you use forward declaration of function. Code below

local&nbsp;spawnController ... function spawnController( action, params ) ... end

Should work now.

Thank you man!

The only problem for me right now that when the obstacle “collide” with the player I cannot call the spawnController function because it cannot recognize it. 

Do you ask how to detect collision between obstacle and player?

Nah… I can detect the collision beween them. I marked the problematic code with “*****”. I cannot call spawnController with scrollBranch function and cannot change the order of them as far as I know. I’m asking if theres a simple way to make that work.

I get what you mean. I suggest that  you use forward declaration of function. Code below

local&nbsp;spawnController ... function spawnController( action, params ) ... end

Should work now.

Thank you man!