Ok, so here is my code as it is now:
--------------------Hit test event--------------------
function hitTestObjects(obj1, obj2)
local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax
return (left or right) and (up or down)
end
--------------------physics--------------------
-- Setup Physics
local physics = require ("physics")
physics.start()
physics.setGravity (0,4)
--------------------ball--------------------
local ballMod = require("ball")
local ballGroup = ballMod.new()
-- Apply Force To Ball
function applyForceToBall( xForce , yForce )
-- Apply Force To Ball
ballGroup:applyForce( xForce , yForce , ballObject.x , ballObject.y )
-- Move Ball To Front
ballGroup:toFront()
end
-- Follow Ball
local function followBall()
-- Move Shadow To Ball
shadow.x = ballObject.x
shadow.y = ballObject.y + 5
end
Runtime:addEventListener( "enterFrame" , followBall )
--------------------Game objective--------------------
local objective = display.newRect(25, 250, 50, 50)
objective:setFillColor(255, 255, 255)
objective.alpha = 0.2
print(hitTestObjects(ballGroup, objective))
and this is my ball module code as it is now:
module(..., package.seeall)
function new()
localGroup = display.newGroup() -- isn't local so can be seen externally
--etc
--------------------ball--------------------
-- Load Shadow
local shadow = display.newCircle( 160 , 120 , 16 )
shadow:setFillColor( 0 , 0 , 0 )
shadow.alpha = 0.2
shadow.yScale = 1
localGroup:insert(shadow)
-- Load Ball
ballObject = display.newImageRect( "ball.png" , 30 , 30 )
ballObject.x = 50
ballObject.y = 50
physics.addBody( ballObject , { density = 1.388997268825 , friction = 4, bounce = 0.1 , radius = 15 } )
localGroup:insert(ballObject)
return localGroup
end
Now, why isn’t this working? What code am I missing? In corona terminal it just says “false” (when the ball goes over the “objective” it should say “true” right? (It does not right now)).
Also, my shadow does not follow the ball now (I know I do not have that part right). How should I change the code to fix this?
Thanks
– [import]uid: 15281 topic_id: 7176 reply_id: 27427[/import]