First of all, it would be very helpful for people looking at your code if it was properly formatted. The forum’s edit box where you paste the code in doesn’t know how to format the code unless you type in **[**code] first, then paste in the code then type in [/code] when it’s done. Properly formatted code would very quickly allow folks on here to spot your problem.
local W = display.contentWidth / 2 local H = display.contentHeight / 2 --schermata 1 local schermataMenuGroup local play -- schermata 2 local circle local circle1 local text function main() menuPrincipale() end function menuPrincipale() schermataMenuGroup = display.newGroup() play = display.newText("Play!", 0, 0, "Arial", 30) play.x = W play.y = H play:setTextColor(255,255,255) play.name = "play" play:addEventListener("tap", loadGame) schermataMenuGroup:insert(play) end function loadGame(event) if event.target.name == "play" then -- Transizione e rimozione del Listener transition.to(schermataMenuGroup,{time = 0, alpha=0, onComplete = addGameScreen}) play:removeEventListener("tap", loadGame) end end local function dragMe(event) local t=event.target if event.phase=="moved" then t.x=event.x t.y=event.y end end function addGameScreen() circle = display.newCircle(W, H-200, 30) circle:setFillColor(2,227,112) circle1 = display.newCircle(W, H+200, 50) circle1:setFillColor(0, 0, 0, 0) circle1.strokeWidth =3 text = display.newText("", 0, 0, "Arial", 30) text.x = W text.y = H circle:addEventListener( "touch", dragMe) end local function hasCollidedCircle( circle, circle1 ) if ( circle == nil ) then --make sure the first object exists return false end if ( circle == nil ) then --make sure the other object exists return false end local dx = circle.x - circle1.x local dy = circle.y - circle1.y local distance = math.sqrt( dx\*dx + dy\*dy ) local objectSize = (circle1.contentWidth/2) + (circle.contentWidth/2) if ( distance \< objectSize ) then return true end return false end if hasCollidedCircle then text = "you win" end main()
My first recommendation is to learn a style of code called “Bottom up” programming. You sort of started by putting your main() function at the bottom, but the rest of your code is “Top down” where your function main() is at the top and what it calls is below it. For this to actually work in Lua you either have to define a bunch of forward references or in your case, make the functions all global, which for a larger project is not sustainable. But that’s beside the point.
The reason your text does show up is for multiple reasons that all spin around this block of code:
if hasCollidedCircle then text = "you win" end
- You never call your collision detection during your movement code. You call it once at the bottom in the main chunk before the circle is ever created.
2. That call to the collision detection has an error in it, but not one that would cause an error to be printed. To call a function and have it do work you have to have () at the end as such: hasCollidedCircle()
That will cause the function to do it’s work. Without the () it’s checking the address of the function and yes, hadCollidedCircle returns a value (it’s not nil) so this evaluates to true.
3. text doesn’t exist yet. It won’t exist until you have touched “Play” and your addGameScreen() function gets called. Since this block of code executes before “Play” could possibly be touched (main() hasn’t been called yet), then it does nothing. Why no error since text doesn’t exist? Well Lua is making a global variable named text that has the string “You won” in it.
- If you called hasCoillidedCircle() you must pass in the two objects (they don’t have to be circles, but display objects) which you are not (since you’re not calling the function at all). You create two circles in addGameScreen (which are also global btw).
Basically that block of code above needs to go inside your drag handler and it needs formatted correctly. Then setting text.text to “you win” will work.