Hey everyone. So I am fairly new to Corona. But i dove headfirst into it by experimenting with a side-scrolling game. I started building the engine with the functionality of being able to jump up through platforms. When I added the screen scroll effect, the platforms started acting weird. Weird as in the physics bodies don’t seem to follow their display. I have been working with this thing and trying different permutations for 2 weeks now and I can’t seem to fix it. Please let me know what you think.
The platforms physics activate depending on where the original display for the platforms was. I can’t seem to figure out why they are being left there. Or why the make new platform functions don’t fire depending on the new platform positions.
I have left the new screen scroll effect code un-commented out so you can see what I added. If you want to see the platform code working pretty much flawlessly I can post that as well.
[code]
–Start up the in-engine physics
local physics = require( “physics” )
physics.start()
-----------------------------set up display groups for screen move.
local p2_moveGroup = display.newGroup() – “p2” is the far background(silhouete).
local p1_moveGroup = display.newGroup() – “p1” is the background main playable area.
local p0_moveGroup = display.newGroup() – “p0” is the group containing the interactable actors.
local p0_subActor = display.newGroup() – Enemies and Fatty
p0_moveGroup:insert(p0_subActor)
local p0_subSet = display.newGroup() – Environment and platforms
p0_moveGroup:insert(p0_subSet)
local UIGroup = display.newGroup()
local p2 = p2_moveGroup
local p1 = p1_moveGroup
local p0 = p0_moveGroup
local p1_ratio = 0.4
local p2_ratio = 0.2
– Set the limits
local worldLimits = { XMin=0 , YMin=0 , XMax=1200 , YMax=900 }
local adjustedXMax = display.contentWidth-worldLimits.XMax
local adjustedYMax = display.contentHeight-worldLimits.YMax
---------------------Set up collision filters for the different platforms and bodies.
–[[
local ninjaCollisionFilter = {categoryBits = 1,maskBits = 10}
local ninjaBodyElement = {density=1.0, friction=0.5, bounce=0,filter = ninjaCollisionFilter}
local groundCollisionFilter = {categoryBits = 8, maskBits = 1}
local groundBodyElement = {friction=0.5 ,bounce=0, filter=groundCollisionFilter}
local platformCollisionFilter = {categoryBits = 2, maskBits = 1}
local platformBodyElement = {friction=0.5 ,bounce=0, filter=platformCollisionFilter}
local platformCollisionFilter2 = {categoryBits = 4, maskBits = 0}
local platformBodyElement2 = {friction=0.5 ,bounce=0, filter=platformCollisionFilter2}
local ground = display.newImage(“ground2.png”)
ground.x=320
ground.y=480
p0_subSet:insert(ground)
physics.addBody(ground, “static”,groundBodyElement)
-----------------------------------------Set up platform tables
local platforms = {}
local storex, storey
----------------------------Add any set piece to physics consideration by .addBody
–(Name of the set piece, “Type of body”, attributes{Density,friction,bounce})
– If you do not set the type of body it will default to dynamic
local area = display.newImage( “face.png” )
area.x = 100
area.y = 380
UIGroup:insert(area)
local joystick = display.newImage( “shuriken.png” )
joystick.x = area.x
joystick.y = area.y
UIGroup:insert(joystick)
local aButton = display.newImage( “aButton.png” )
aButton.x = 800
aButton.y = 350
UIGroup:insert(aButton)
local bButton = display.newImage( “bButton.png” )
bButton.x = 725
bButton.y = 425
UIGroup:insert(bButton)
local ninja = display.newImage( “ninja.png” )
ninja.x = 450
ninja.y = 430
physics.addBody(ninja,ninjaBodyElement)
ninja.isFixedRotation = true
local platform = display.newImage(“platform.png” )
platform:setFillColor(0,255,0)
platform.x = 625
platform.y = 300
p0_subSet:insert(platform)
physics.addBody(platform,“static”,platformBodyElement2)
platforms[#platforms+1] = platform
platform.check = 0
local platform2 = display.newImage( “platform.png” )
platform2:setFillColor(0,255,255)
platform2.x =200
platform2.y = 270
p0_subSet:insert(platform2)
physics.addBody(platform2,“static”,platformBodyElement2)
platforms[#platforms+1] = platform2
platform2.check = 0
----------------------making new platforms when the ninja is above them
offset = 0
local function makenewplatform()
print(“made”)
local platform = display.newImage( “platform.png” )
platform:setFillColor(255,0,0)
platform.x = storex
platform.y = storey
p0_subSet:insert(platform)
physics.addBody(platform,“static”,platformBodyElement)
platforms[#platforms+1] = platform
end
local function makenewplatform2()
print(“made2”)
local platform = display.newImage( “platform.png” )
platform:setFillColor(255,255,0)
platform.x = storex
platform.y = storey
p0_subSet:insert(platform)
physics.addBody(platform,“static”,platformBodyElement2)
platforms[#platforms+1] = platform
end
local function listener()
for i, platform in pairs(platforms) do
if platform ~= nil and platform.check == 0 and ninja.x > platform.x - 100 and ninja.x < platform.x + 100 and ninja.y < platform.y - 20 then
platform.check = 1
storex = platform.x
storey = platform.y
display.remove( platform )
platform = nil
platforms[i] = nil
print(“removed”)
timer.performWithDelay( 0, makenewplatform, 1 )
elseif platform ~= nil and platform.check == 1 and (ninja.x < platform.x - 100 or ninja.x > platform.x + 100 or ninja.y > platform.y + 20) then
platform.check = 0
storex = platform.x
storey = platform.y
display.remove( platform )
platform = nil
platforms[i] = nil
print(“removed”)
timer.performWithDelay( 0, makenewplatform2, 1 )
end
end
end
Runtime:addEventListener(“enterFrame”, listener)
–For the test of using a square, the engine automaitcally uses a square physics shape. For circles or anything more complex, parameters need to be set.
--------------------------------------------Joystick Controls
local function jlis( event )
if event.phase == “began” or event.phase == “moved” then
if joystick.x < area.x + 100 and joystick.x > area.x - 100 then
joystick.x = event.x
else
transition.to( joystick, { time = 100, x = area.x, y = area.y} )
end
if joystick.y < area.y + 100 and joystick.y > area.y - 100 then
joystick.y = event.y
else
transition.to( joystick, { time = 100, x = area.x, y = area.y} )
end
dx = ( area.x - joystick.x ) / 9
dy = ( area.y - joystick.y ) / 9
elseif event.phase == “ended” or event.phase == “cancelled” then
transition.to( joystick, { time = 100, x = area.x, y = area.y} )
dx = 0
dy = 0
end
end
area:addEventListener( “touch”, jlis )
--------------------------------------------Ninja movement
–Checks to see if the nija is already heading that direction
local dir = 1
local vert = 0
local rightValue = 0
local leftValue = 0
–moving the ninja
local function mover ()
if dx ~= nil and dy ~= nil then
if dx > 2 then
leftValue = -1
dir = -1
elseif dx < 2 then
leftValue = 0
end
if dx < -2 then
rightValue = 1
dir = 1
elseif dx > -2 then
rightValue = 0
end
if dy < -6 and vert == 0 and ninja.y<410 then
print(“going down”)
vert = -1
ninja.y = (ninja.y+50)
elseif dy > -6 and vert == -1 then
vert = 0
end
end
end
–making the apply force a function
local function vertChange(event)
if event.phase == “began” and vert ==0 then
vert = 1
end
end
local function landing(event)
if vert == 2 then
vert=0
end
end
local function jump(event)
if event.phase == “began” and vert ==1 then
print(“touch”)
ninja:applyForce( (100*dir),(-1000*vert),ninja.x,ninja.y)
elseif event.phase == “ended” then
print(#platforms)
vert = 2
ninja:applyForce( 0,0,ninja.x,ninja.y)
end
end
–listening to see if the nija is on the ground
local function listenerRL()
if rightValue == 1 and vert==0 then
ninja.x = ninja.x + 2
offset = offset - 7
end
if leftValue == -1 and vert==0 then
ninja.x = ninja.x - 2
offset = offset + 7
end
end
Runtime:addEventListener(“enterFrame”,listenerRL)
Runtime:addEventListener(“enterFrame”,mover)
]]–
-----------------------------------Screen scroll function
local function moveCamera()
if dx ~= nil and dy ~= nil then
if dx > 2 or dx < -2 then
p0.x = offset
p0.y = ninja.y
– if new X position is within world limits, move it there
if ( ninja.x <= worldLimits.XMin and ninja.x >= adjustedXMax ) then
p0.x = ninja.x
print(“fire”)
elseif ( ninja.x < worldLimits.XMin ) then
p0.x = worldLimits.XMin
elseif ( ninja.x > worldLimits.XMax ) then
p0.x = adjustedXMax
end
–position other layers (X) in ratios
p1.x = p0.x * p1_ratio ; p2.x = p0.x * p2_ratio
– if new Y position is within world limits, move it there
if ( ninja.y <= worldLimits.YMin and ninja.y >= adjustedYMax ) then
p0.y = ninja.y
elseif ( ninja.y > worldLimits.YMin ) then
p0.y = worldLimits.YMin
elseif ( ninja.y < worldLimits.YMax ) then
p0.y = adjustedYMax
end
–position other layers (Y) in ratios
p1.y = p0.y * p1_ratio ; p2.y = p0.y * p2_ratio
end
end
end
Runtime:addEventListener( “enterFrame”, moveCamera )
–[[
–trying to call my jump function by making the game listen for a touch
ninja.preCollision = landing
ninja:addEventListener(“preCollision”,ninja)
aButton:addEventListener(“touch”, vertChange)
aButton:addEventListener(“touch”, jump)
]]–
[/code] [import]uid: 67381 topic_id: 13314 reply_id: 313314[/import]