Corona SDK: Make code reusable

This is a short update on our Rolly Bear World project. I’m starting to create all the different levels for Rolly Bear World, and realized that some of the code will be reused across all levels. To avoid bugs in future changes I decided to make some of the code and functions available on a global level so that all separate level files can reuse these functions. I created a separate lua file called platformListeners.lua, which contain the listener functions for the platforms in the game. These are:

- movePlatform (the function to drag the platform around the screen)
- rotatePlatform (the function to rotate the platform on the screen).

In each level file we need to require this newly created .lua file, so that each individual lua level file can actually access these functions.

So make sure, in case you’re making this change as well to include in the level01.lua, level02.lua files:

local platformlisteners = require ("platformListeners")

The code listed below has been removed from level01.lua and added to the new .lua file called platformListeners.lua, I also made some additional changes which I explain below.


adjustlevel = true 
totalnumRotationalerts = {} -- table to store all alerts



 function rotatePlatform(event)
	 alerttouched = event.target

if adjustlevel == true then 
	    	
        if (event.phase == "began") then
                display.getCurrentStage():setFocus( alerttouched )
 				
 				-- here the first position is stored in x and y 	         
              
	       elseif (event.phase == "moved") then

	       				platformTouched.x2 = event.x
	                    platformTouched.y2 = event.y
						
						angle1 = 180/math.pi * math.atan2(platformTouched.y1 - platformTouched.y , platformTouched.x1 - platformTouched.x)
	                    angle2= 180/math.pi * math.atan2(platformTouched.y2 - platformTouched.y , platformTouched.x2 - platformTouched.x)
	                  
	                    differencebetweenangles = angle1 - angle2
	 						
	 				    --rotate it
	                     platformTouched.rotation = platformTouched.rotation - differencebetweenangles
	                     
	                        
	                     platformTouched.x1 = platformTouched.x2
	                     platformTouched.y1 = platformTouched.y2


	       	elseif event.phase == "ended" or event.phase == "cancelled"  then

	       	display.remove( rotationalert )
	       	rotationalert = nil 

	       	display.getCurrentStage():setFocus( nil )

           end 

	end 

end 


function movePlatform(event)
	 platformTouched = event.target

	 if adjustlevel == true then 
	    	
        if (event.phase == "began") then
                display.getCurrentStage():setFocus( platformTouched )
 				
 				display.remove( rotationalert )
 				--rotationalert = nil 
 		
 				-- here the first position is stored in x and y 	         
                platformTouched.startMoveX = platformTouched.x
				platformTouched.startMoveY = platformTouched.y

				platformTouched.x1 = event.x
                platformTouched.y1 = event.y


        		elseif (event.phase == "moved") then

                -- here the distance is calculated between the start of the movement and its current position of the drag	 
                	platformTouched.x = (event.x - event.xStart) + platformTouched.startMoveX
					platformTouched.y = (event.y - event.yStart) + platformTouched.startMoveY

						
                elseif event.phase == "ended" or event.phase == "cancelled"  then

              	
              		if platformTouched.x >= platformboard.x + 0.5*(platformboard.x) then
	              		rotationalert = display.newImage ("images/rotation.png")
	                	rotationalert.x = platformTouched.x
	                	rotationalert.y = platformTouched.y 
	                	rotationalert.alpha = 0.5 
	                	totalnumRotationalerts [#totalnumRotationalerts+1] = rotationalert
	                	print(#totalnumRotationalerts)
	                	rotationalert:addEventListener ("touch", rotatePlatform)
                	end 

	
                	display.getCurrentStage():setFocus( nil )
                end
                 return true
        end
end 

As already mentioned I made a few more changes. I noticed a small feature was still missing in the gameplay, that is, when the user launch Rolly Bear, the user should not be able to move or rotate the platforms anymore, in the current status of the game this was still possible (shame on me). Therefore I create a variable called adjustlevel and set its initial value to true (adjustlevel = true) (line 2). In the movePlatform and rotatePlatform I check with an if-statement if this value is still true and thus allow the functions to be executed (line 8 and 47). When the user launch Rolly Bear I set this variable to false, making a “Shall not pass” in the movePlatform and rotatePlatform functions (the function to launch Rolly Bear is part of the level files, not listed above).

Another small change I made was how I deal with the rotationAlerts (the image which points the user towards rotating the platforms). Up till now every time a user drags or moves a platform a rotationAlert was created, but I decided to add them to a table so I can manage them better later on (e.g. removing them all from the screen). So on top of the platformListeners.lua file you find the table totalnumRotationalerts = {} (line 3), and each time a rotationAlert is created it is added to this table (line 75).

totalnumRotationalerts [#totalnumRotationalerts+1] = rotationalert

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*