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 ) if( self.x \< 0 ) then Runtime:removeEventListener( "enterFrame", self ); self:removeSelf(); self = nil; else if ( self.x \<= sloth.x ) then if( self.y == sloth.y ) then stopScrollBackground(); Runtime:removeEventListener( "touch", onScreenTouch ); --spawnController( "stop" );\*\*\*\*\* end end self.x = self.x - self.speed; end end local function spawnObstacles() local obs; local obsName; local obsW; local obsH; local objNum = mRand( 3 ); if ( objNum == 1 ) then obsName = "logo"; obsW = 10; obsH = 10; elseif ( objNum == 2 ) then obsName = "logo"; obsW = 20; obsH = 20; else obsName = "logo"; obsW = 30; obsH = 30; end obs = display.newImageRect( "images/" .. obsName .. ".png", obsW, obsH ); obs.x = \_W + \_CX; --math.randomseed( os.time() ) local obsY = mRand( 2 ); if( obsY == 1 ) then obs.y = \_H \* 0.41; else obs.y = \_H \* 0.41 + 50; obs.yScale = -1; end obs.speed = mRand( 6, 8 ); spawnedObjects[#spawnedObjects + 1] = obs; --spawnedObjects[#spawnedObjects].enterFrame = scrollBranch;\*\*\*\*\* Runtime:addEventListener( "enterFrame", spawnedObjects[#spawnedObjects] ); end local function spawnController( action, params ) if ( spawnTimer and ( action == "start" or action == "stop" ) ) then timer.cancel( spawnTimer ) end if ( action == "start" ) then local spawnTime = params.spawnTime or 1000 spawnTimer = timer.performWithDelay( spawnTime, function() spawnObstacles(); end, 0 ) elseif ( action == "pause" ) then timer.pause( spawnTimer ) elseif ( action == "resume" ) then timer.resume( spawnTimer ) end end
If you have further question or do not understand my code please reply here I’ll answer in seconds : )
Thanks,
Itay