Hi,
I am adapting Ed’s “they need to be fed example” and trying to make a platform game using tiles. The problem I am having is that the collision is being fired (ended) with every new tile, which makes sense, but it is causing an issue .
The issue occurs when trying to figure out if the “player” is grounded or not. In the example below you can see the wheel goes from being “grounded” to “not grounded” as it moves across the tiles. I want the “player” to behave differently depending on whether it is on the ground or not (similar to the ‘foot’ in Ed’s example). Can someone point out where I am going wrong, or if there is a different approach I should be using.
Thanks,
Craig
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- hide status bar display.setStatusBar( display.HiddenStatusBar ) display.setDefault( "background", 197/255, 224/255, 220/255 ) local widget = require "widget" system.activate( "multitouch" ) local physics = require "physics" --physics.setDrawMode( "hybrid" ) physics.start() physics.setGravity( 0, 9.8 ) -- constants local screenW, screenH = display.actualContentWidth, display.actualContentHeight local centerX, centerY = display.contentCenterX, display.contentCenterY local originX, originY = display.screenOriginX, display.screenOriginY -- variables local horizontal = 0 local vertical = 0 local level = display.newGroup() ----------------------------------------------------------------------------------------- -- draw the block tiles for i = 1, 50 do local ground = display.newRect( 0, 0, 50, 50 ) ground:setFillColor( 1, 1, 1) ground.strokeWidth = 3 ground:setStrokeColor( 1, 0, 0 ) ground.x, ground.y = i\*50 - 100, originY + screenH physics.addBody( ground, "static", { density=1, friction=0.3, bounce=0 } ) level:insert( ground ) end local wheel1 = display.newImageRect( "wheel.png", 30, 30 ) wheel1.x, wheel1.y = 100, 100 physics.addBody( wheel1, "dynamic", { density=1, friction=5, bounce=0.2, filter={ groupIndex=-1 }, radius=15 } ) wheel1.collision = function( self, event ) -- Abort early if player has been removed somehow if( self.removeSelf == nil ) then return true end -- Detect if wheel is on the ground if( event.phase == "began" ) then print( "grounded" ) elseif( event.phase == "ended" ) then print( "not grounded" ) end return true end wheel1:addEventListener( "collision" ) level:insert( wheel1 ) ----------------------------------------------------------------------------------------- local function controlHandler( event ) local id = event.target.id local phase = event.phase if phase == "began" or phase == "moved" then if id == "forward" then wheel1:applyTorque( 10 ) elseif id == "backward" then wheel1:applyTorque( -10 ) end end end local btnForward = widget.newButton{ defaultFile = "btn\_arrow.png", width=50, height=50, id="forward", onEvent=controlHandler } btnForward.x = originX + screenW - btnForward.height \* 0.5 - 5 btnForward.y = originY + screenH - btnForward.height \* 0.5 - 5 local btnBackward = widget.newButton{ defaultFile = "btn\_arrow.png", width=50, height=50, id="backward", onEvent=controlHandler } btnBackward.rotation = 180 btnBackward.x = btnForward.x - btnForward.width - 5 btnBackward.y = btnForward.y