Building Level 6-8 of RollyBear World

In the last post I provided the explanation of Level 1 -5 of Rolly Bear World, in this consecutive post I will do the same for Level 6 -8. I will only explain the key changes to the code. In case you missed some of the core functionalities build in Rolly Bear World, make sure you visit this page with all available tutorials.

Level 6
Level six is not much different from level 3 with the exception that this time the spring is fixed in the gameplay and not an available platform for the user to change. Just look at the code of level 6 and you will see we use the exact same concepts for creating the animated spring object only now as a fixed object.

Level 7

Level 7 is a bit more interesting, that is, I introduced here the bowling ball. The bowling ball can be used by the user to crash objects which are between Rolly Bear and his path or goal (the treasure chest). So this requires a few changes because when the user set-ups the level we don’t want the bowling ball to be influenced by the physics engine (“static”), but as soon as the user launch Rolly Bear, the bowling ball needs to drop and crush everything on its path :).

The bowling ball is created like any other platform the user can drag and drop on the screen. See line 2 were it is inserted into the table platformNames:

--- creation of this levels platforms 
platformNames = {"platform-brown256", "platform-brown256","platform-brown256",  "bowling-ball64" };

for x =1, #platformNames do 
	platformNum = platformNames[x]
	platform = display.newSprite( myPlatformSheet , {frames={sheetInfo:getFrameIndex(platformNum)}} ) 
	platform.x = display.screenOriginX + 100
	platform.y = 150 + 75 * x 
	platform.myName = platformNum 
	totalnumPlatforms [#totalnumPlatforms+1] = platform
	platform.platformNum = platformNum
	physics.addBody( platform, physicsData:get(platformNum))
	platform.bodyType = "static"  
	platform:addEventListener("touch", movePlatform)
	group:insert(platform)
	print(platform.myName)
end

I mentioned when the user launch Rolly Bear the bowling ball is allowed to be subjected to gravity. But wait a second, this is not completely true. The bowling ball is only allowed to drop when the following two statements are true:
- The user clicks to launch Rolly Bear
- The user actually positioned the ball on the screen (as the user might decide not to move the ball outside the platformboard on the left).

To check if the second statement is true I created the variable allowDynamic (which initial value is set to false). In the checkLocation function I check if the user actually moved the bowling ball on to the screen and set allowDynamic to true. When the user drags the bowling ball back to the platformboard the value is set back to false.

function checkLocation()
	--- check location to enlarge platforms outside the board
	for x = 1, #totalnumPlatforms do

		local platformScale = totalnumPlatforms[x]
		if platformScale.x > platformboard.x + 0.5*(platformboard.x) then
			allowDynamic = true 
			platformScale.xScale = 1.0
			platformScale.yScale = 1.0
		end

		if platformScale.x <= platformboard.x + 0.5*(platformboard.x) then
		    if x == 4 then -- 4 is defined in the table of platforms as the bowling ball
			platformScale.xScale = 0.9
			platformScale.yScale = 0.9
			else 
			platformScale.xScale = 0.4
			platformScale.yScale = 0.9		
		end 
		platformScale.rotation = 0
		allowDynamic = false  
		end

	end
end

Now when we Launch Rolly Bear (funtion launchRollyBear) we need to set the bowling ball from “static” to “dynamic” when allowDynamic is true. Note that the bowlingball object in platformNames is the 4th member of the table.

 for x = 1, #platformNames do
  if platformNames[4] and allowDynamic == true then 
	platform.bodyType = "dynamic"
	end
end 

The last part we need to do is to remove the crate when the bowling ball hits the crate.

 function onCollision(event)
 			--- events which happen when rollbear collides with the treasure chest

 			if (event.object1.myName=="bowling-ball64" and event.object2.myName=="crate64") then
 				display.remove(crate)
 				crate = nil 
 			end

Level 8
Level 8 continues to build on the bowling ball function. There are minor changes as this time we use more than one crate and then program needs to know which crate has been hit.

I crated 3 crates:

local crate = display.newSprite( myPlatformSheet , {frames={sheetInfo:getFrameIndex("crate64")}} )
createObjectprops(crate, brickplatformMid.x - crate.width, brickplatformMid.y, 0 , display.BottomCenterReferencePoint, "crate64", 1.2, 1.2, "0_crate64" )

local crate1 = display.newSprite( myPlatformSheet , {frames={sheetInfo:getFrameIndex("crate64")}} )
createObjectprops(crate1, brickplatformMid.x, brickplatformMid.y, 0 , display.BottomCenterReferencePoint, "crate64", 1.2, 1.2, "1_crate64" )

local crate2 = display.newSprite( myPlatformSheet , {frames={sheetInfo:getFrameIndex("crate64")}} )
createObjectprops(crate2, brickplatformBottom.x, brickplatformBottom.y, 0 , display.BottomCenterReferencePoint, "crate64", 1.2, 1.2, "2_crate64" )

Note that createObjectprops is a global function defined as:

function createObjectprops (nameObj, x, y, rotation, referencepoint, resource, xscalev, yscalev, myName)
	nameObj:setReferencePoint (referencepoint)
	nameObj.x = x 
	nameObj.y = y 
	nameObj.rotation = rotation or 0.0 
	physics.addBody( nameObj, "static", physicsData:get(resource))
	nameObj.xScale = xscalev or 1.0
	nameObj.yScale = yscalev or 1.0 
	nameObj.myName = myName
	myStaticgroup:insert(nameObj)
end

In the collision function we just need to check which crate has been hit by the bowling ball

 function onCollision(event)
 			--- events which happen when rollbear collides with the treasure chest

 			if (event.object1.myName=="0_crate64" and event.object2.myName=="bowling-ball64")
 				or
				(event.object1.myName=="1_crate64" and event.object2.myName=="bowling-ball64") 
				or
				(event.object1.myName=="2_crate64" and event.object2.myName=="bowling-ball64") 
			then
 				display.remove(event.object1)
 			end

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>

*