Hello,
I’m creating a game where I need a dynamic object (ball) set as “isSensor=true” to interact with static objects (walls) that are scrolling across the screen. So basically, I’ve got a vertical screen scroller of static objects (walls) moving from bottom to top. I then have a dynamic object (ball) set to isSensor=true. The dynamic object (ball) can be moved back and forth horizontally with the finger placed anywhere on the screen. The dynamic object (ball) needs to create an onCollision event whenever it hits the vertically scrolling “static” objects (walls). The “ball” needs to remain a dynamic object set to “isSensor=true” so that it doesn’t get moved around by any collision events.
When I’m actively moving the dynamic object (ball) on screen and it hits the moving “static” object (walls) I get a collision event, however if I lift my finger from the screen and the “ball” hits the moving “wall” I don’t get a collision event. It is totally ignored. This also happens if I keep my finger on the screen but don’t move the “ball”, no collision event. The collision event only occurs if the “ball” is moving when it contacts the scrolling “wall” objects.
Has anyone seen this problem before? I tried changing the “static” objects (walls) to “dynamic” but that caused problems with gravity. The “walls” would start to shift and not scroll properly as well as not record collisions appropriately. What am I doing wrong, anyone have any ideas to get the collision event to fire whether I’m moving the dynamic object (ball) or not when it encounters a wall???
Below is a simple example of what I’m talking about. It works fine other than not firing a collision event when my finger (or mouse) is not on screen actively moving the ball into a wall.
Thanks to anyone who is able to help,
Guy
– Code in main.lua –
require(“physics”)
physics.start(true)
–physics.setDrawMode(‘hybrid’)
physics.setDrawMode(‘normal’)
physics.setGravity(0, 0)
group=display.newGroup()
local w = display.contentWidth
local h = display.contentHeight
local x,y = 1,2
– global function to calculate pixels when placing objects on the screen
– gives x and y coordinants entered as percents and converts to the
– correct pixel location on the screen.
function perc(n,xy) – Calculates screen coordinates based on width of 320 and hieght of 480
if xy==1 then – x value passed as n
return (n/320)*display.contentWidth
end
if xy==2 then – y value passed as n
return (n/480)*display.contentHeight
end
end
local ball = display.newCircle(group,160,100,10)
ball.name=“ball”
ball:setFillColor(9/250, 228/250, 0)
physics.addBody(ball,“dynamic”,{radius=10, density=0.3, bounce=1, friction=0})
ball.isSensor=true
– Code to create scrolling vertical walls using vertices and display.newPolygon() –
local wall1LeftVertices =
{0,0, w/3,0, perc(108,x),0, perc(110,x),0, perc(105,x),perc(2,y), perc(103,x),perc(20,y), perc(95,x),perc(29,y), perc(94,x),perc(35,y),perc(87,x),perc(42,y),perc(86,x), perc(45,y),perc(81,x), perc(51,y),perc(79,x),perc(52,y),
perc(76,x), perc(56,y),perc(76,x), perc(108,y),perc(80,x), perc(118,y),perc(84,x), perc(122,y),perc(85,x), perc(142,y),perc(83,x), perc(147,y),perc(76,x), perc(156,y),perc(76,x), perc(164,y),perc(69,x), perc(172,y),perc(67,x), perc(180,y),perc(67,x), perc(190,y),
perc(70,x), perc(200,y),perc(75,x), perc(205,y),perc(75,x), perc(215,y),perc(85,x), perc(227,y),perc(86,x), perc(252,y),perc(79,x), perc(259,y),perc(75,x), perc(269,y),perc(75,x), perc(289,y),perc(69,x), perc(295,y),perc(67,x), perc(302,y),
perc(60,x), perc(309,y),perc(57,x), perc(318,y),perc(57,x), perc(323,y),perc(48,x), perc(329,y),perc(40,x), perc(337,y),perc(39,x), perc(360,y),perc(30,x), perc(369,y),perc(30,x), perc(394,y),perc(34,x), perc(406,y),perc(40,x), perc(413,y),
perc(40,x), perc(425,y),perc(42,x), perc(429,y),perc(59,x), perc(444,y),perc(60,x), perc(447,y),perc(71,x), perc(459,y),perc(77,x), perc(462,y), perc(94,x), perc(462,y),perc(105,x), perc(476,y), w/3,h, 0,h}
local wall1RightVertices =
{ 0,0, w/3,0, w/3,h, 0,h, 0,perc(464,y),perc(-2,x), perc(465,y),perc(-3,x), perc(459,y),perc(-11,x), perc(452,y),perc(-13,x), perc(439,y),perc(-20,x), perc(432,y),perc(-23,x), perc(423,y),
perc(-31,x), perc(415,y),perc(-38,x), perc(412,y),perc(-38,x), perc(398,y),perc(-40,x), perc(396,y),perc(-40,x), perc(388,y),perc(-45,x), perc(382,y),perc(-45,x), perc(364,y),perc(-38,x), perc(354,y),perc(-38,x), perc(278,y),
perc(-31,x), perc(268,y),perc(-30,x), perc(258,y),perc(-30,x), perc(210,y),perc(-33,x), perc(206,y),perc(-33,x), perc(197,y),perc(-45,x), perc(184,y),perc(-45,x), perc(177,y),perc(-39,x), perc(169,y),perc(-40,x), perc(151,y),
perc(-38,x), perc(148,y),perc(-34,x), perc(142,y),perc(-32,x), perc(135,y),perc(-33,x), perc(126,y),perc(-28,x), perc(119,y),perc(-29,x), perc(101,y),perc(-22,x), perc(94,y),perc(-20,x), perc(88,y),perc(-20,x), perc(46,y),perc(-15,x), perc(43,y),
perc(-13,x), perc(37,y),perc(-6,x), perc(34,y),perc(-4,x), perc(30,y) }
local wall2LeftVertices =
{ 0,0, w/3,0, perc(102,x), perc(12,y), perc(97,x),perc(18,y), perc(91,x),perc(26,y), perc(82,x),perc(34,y), perc(79,x),perc(39,y), perc(80,x),perc(46,y), perc(75,x),perc(50,y), perc(71,x),perc(60,y), perc(62,x),perc(69,y), perc(61,x),perc(77,y),
perc(51,x),perc(95,y), perc(44,x),perc(102,y), perc(38,x),perc(116,y), perc(34,x),perc(119,y), perc(26,x),perc(127,y), perc(24,x),perc(140,y), perc(22,x),perc(202,y), perc(19,x),perc(214,y), perc(15,x),perc(218,y), perc(13,x),perc(224,y),
perc(7,x),perc(230,y), perc(6,x),perc(249,y), perc(8,x),perc(273,y), perc(21,x),perc(286,y), perc(24,x),perc(293,y), perc(24,x),perc(324,y), perc(35,x),perc(335,y), perc(42,x),perc(346,y), perc(44,x),perc(375,y), perc(53,x),perc(388,y),
perc(61,x),perc(404,y), perc(61,x),perc(416,y), perc(71,x),perc(425,y), perc(81,x),perc(448,y), perc(91,x),perc(458,y), perc(97,x),perc(468,y),
w/3,h, 0,h}
local wall2RightVertices =
{ 0,0, w/3,0, w/3,h, 0,h,
perc(14,x),perc(461,y), perc(23,x),perc(455,y),
perc(25,x),perc(439,y), perc(31,x),perc(433,y), perc(33,x),perc(426,y),
perc(33,x),perc(402,y), perc(25,x),perc(390,y), perc(25,x),perc(386,y), perc(22,x),perc(381,y), perc(22,x),perc(374,y), perc(15,x),perc(367,y), perc(13,x),perc(359,y), perc(3,x),perc(351,y), perc(3,x),perc(343,y), perc(13,x),perc(335,y),
perc(14,x),perc(331,y), perc(14,x),perc(305,y), perc(4,x),perc(294,y), perc(-4,x),perc(280,y), perc(-4,x),perc(269,y), perc(-5,x),perc(260,y), perc(-15,x),perc(248,y), perc(-22,x),perc(243,y), perc(-27,x),perc(243,y), perc(-31,x),perc(231,y),
perc(-40,x),perc(222,y), perc(-49,x),perc(216,y), perc(-51,x),perc(200,y), perc(-50,x),perc(194,y), perc(-47,x),perc(190,y), perc(-47,x),perc(181,y), perc(-39,x),perc(165,y), perc(-38,x),perc(161,y), perc(-34,x),perc(156,y), perc(-32,x),perc(145,y),
perc(-21,x),perc(95,y), perc(-21,x),perc(83,y), perc(-15,x),perc(71,y), perc(-4,x),perc(56,y), perc(-3,x),perc(46,y), perc(2,x),perc(33,y), perc(14,x),perc(14,y), perc(14,x),perc(3,y),}
– Create the scrolling walls –
local wallL1, wallL2, wallR1, wallR2
wallL1 = display.newPolygon(group,0,0, wall1LeftVertices)
wallL1.anchorX = 0; wallL1.anchorY = 1
physics.addBody(wallL1, “static”, {wall1LeftVertices})
wallL1:toBack()
wallL1.x = 0; wallL1.y = h
wallL1.name=“wall”
wallL1:setFillColor(190/250, 127/250, 0/250)
wallR1 = display.newPolygon(group,0,0, wall1RightVertices)
wallR1.anchorX=1; wallR1.anchorY=1
physics.addBody(wallR1, “static”, {wall1RightVertices})
wallR1:toBack()
wallR1.x = w; wallR1.y=h
wallR1.name=“wall”
wallR1:setFillColor(190/250, 127/250, 0/250)
wallL2 = display.newPolygon(group,0,0, wall1LeftVertices)
wallL2.anchorX = 0; wallL2.anchorY = 1
physics.addBody(wallL2, “static”, {wall2LeftVertices})
wallL2:toBack()
wallL2.x = 0; wallL2.y=h*2
wallL2.name=“wall”
wallL2:setFillColor(190/250, 127/250, 0/250)
wallR2 = display.newPolygon(group,0,0, wall2RightVertices)
wallR2.anchorX=1; wallR2.anchorY=1
physics.addBody(wallR2, “static”, {wall2RightVertices})
wallR2:toBack()
wallR2.x=w; wallR2.y=h*2
wallR2.name=“wall”
wallR2:setFillColor(190/250, 127/250, 0/250)
– create an overlay for touching the screen to move the ball –
local overlay = display.newRect(group,0,perc(31,2),display.contentWidth,display.contentHeight)
overlay.anchorX = 0; overlay.anchorY = 0
overlay.x=0; overlay.y=perc(31,2)
overlay:setFillColor(0,0,0,0.1) – make invisible
local wallSpeed = 3 – used to set scroll speed
function moveWalls()
wallL1.y, wallR1.y= wallL1.y - wallSpeed, wallR1.y - wallSpeed
wallL2.y, wallR2.y = wallL2.y - wallSpeed, wallR2.y - wallSpeed
if wallL1.y <= 0 then
wallL1.y = wallL2.y + h; wallL1.x = 0
wallR1.y = wallR2.y + h; wallR1.x = w
elseif wallL2.y <= 0 then
wallL2.y = wallL1.y + h; wallL2.x = 0
wallR2.y = wallR1.y + h; wallR2.x = w
end
end --moveWalls()
Runtime:addEventListener( “enterFrame”, moveWalls )
– Function to move the ball left or right –
local function moveball(event)
if event.phase==“began” then
dif = event.x - ball.x – get difference in finger location vs ball location to figure how to move it.
end
if event.phase==“began” or event.phase==“moved” then
ball.x = event.x - dif
if ball.x > display.contentWidth - ball.width/2 then
ball.x = display.contentWidth - ball.width/2
end
end
end --moveball()
overlay:addEventListener(“touch”,moveball)
– Function to get the collision events between the ball and scrolling walls –
local function onCollision(e)
if e.phase == “began” then
if (e.object1.name==“wall”) and (e.object2.name==“ball”) then
print(“The Ball hit the Wall”)
end – “wall”
end – “began”
end – onCollision
Runtime:addEventListener( “collision”, onCollision )