Game objective

[lua]local ballMod = require(“ball”)
local ballGroup = ballMod.new()
ballGroup:applyForceToBall(10,10) [/lua]

localGroup isn’t visible to code outside of it’s own module. that’s the point of “local”

for your previous code to work, you’d have to define it like this

[lua]module(…, package.seeall)

function new()
localGroup = display.newGroup() – isn’t local so can be seen externally
–etc[/lua]

but it would break if you tried to have more than one ball, since you only ever have one localGroup reference available for your module in that case [import]uid: 6645 topic_id: 7176 reply_id: 27382[/import]

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]

well you pretty much took all of the stuff out of your ball module that was working (shadow following ball, force function etc). why did you do that?!

there’s no such thing as ballObject or shadow in your main.lua file, so obviously that follow function will fail. try doing [lua]print(tostring(ballObject))[/lua] in your follow function. it will be nil. as will shadow.

when you make something in a module, other files can’t see it unless you reference via that module

main.lua can see ballMod.shadow, ballMod.localGroup, ballMod.ballObject but it cant see shadow, localGroup or ballObject…

also your physics is on ballObject not ballGroup so you cant :applyForce to ballGroup

you’ve completely changed how it would work now… which is why it doesn’t :slight_smile:

i suggest you read through this again,
http://developer.anscamobile.com/content/modules

and create some simpler projects that use modules with functions, variables etc and test the difference between local functions&variables, non-local functions&variables, and table functions

[import]uid: 6645 topic_id: 7176 reply_id: 27467[/import]