i need help!

i cant end my app, can someone help me?

i want that when i move(drag) local circle inside local circle1 , on the screen appears the text “you win”, how can i write it??

this is my code:

local function dragMe(event)
local t=event.target
if event.phase==“moved” then
t.x=event.x
t.y=event.y
end
end

 

local circle = display.newCircle(W, H-200, 30)
circle:setFillColor(2,227,112)

 

local circle1 = display.newCircle(W, H+200, 50)
circle1:setFillColor(0, 0, 0, 0)
circle1.strokeWidth =3

 

circle:addEventListener( “touch”, dragMe)

 

You are asking quite a few questions.   First, you need to know when the two circles touch.  This is called a collision and you need to know how to  detect a collision detection.   Please see this blog post:

http://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Once you’ve detected the collision, then you need to put text on the screen.  See the display.newText() API call for that:

http://docs.coronalabs.com/api/library/display/newText.html

ok!!!:slight_smile: thanks

The text  not appears, where is the error?

code:

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()

Try text.text =“you win”

No the text not appears!

change the variable name of your text object

txt = display.newText("", 0, 0, "Arial", 30) txt.x = W txt.y = H

txt.text = "You Win"

no… it dosen’t work

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() &nbsp;&nbsp;&nbsp; menuPrincipale() end function&nbsp; menuPrincipale() &nbsp;&nbsp;&nbsp; schermataMenuGroup = display.newGroup() &nbsp;&nbsp;&nbsp; play = display.newText("Play!", 0, 0, "Arial", 30) &nbsp;&nbsp;&nbsp; play.x = W &nbsp;&nbsp;&nbsp; play.y = H &nbsp;&nbsp;&nbsp; play:setTextColor(255,255,255) &nbsp;&nbsp;&nbsp; play.name = "play" &nbsp;&nbsp;&nbsp; play:addEventListener("tap", loadGame) &nbsp;&nbsp;&nbsp; schermataMenuGroup:insert(play) end function loadGame(event) &nbsp;&nbsp;&nbsp; if event.target.name == "play" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- Transizione e rimozione del Listener &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(schermataMenuGroup,{time = 0, alpha=0, onComplete = addGameScreen}) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; play:removeEventListener("tap", loadGame) &nbsp;&nbsp;&nbsp; end end local function dragMe(event) &nbsp;&nbsp;&nbsp; local t=event.target &nbsp;&nbsp;&nbsp; if event.phase=="moved" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.x=event.x &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.y=event.y &nbsp;&nbsp;&nbsp; end end function addGameScreen() &nbsp;&nbsp;&nbsp; circle = display.newCircle(W, H-200, 30) &nbsp;&nbsp;&nbsp; circle:setFillColor(2,227,112) &nbsp;&nbsp;&nbsp; circle1 = display.newCircle(W, H+200, 50) &nbsp;&nbsp;&nbsp; circle1:setFillColor(0, 0, 0, 0) &nbsp;&nbsp;&nbsp; circle1.strokeWidth =3 &nbsp;&nbsp;&nbsp; text = display.newText("", 0, 0, "Arial", 30) &nbsp;&nbsp;&nbsp; text.x = W &nbsp;&nbsp;&nbsp; text.y = H &nbsp;&nbsp;&nbsp; circle:addEventListener( "touch", dragMe) end local function hasCollidedCircle( circle, circle1 ) &nbsp;&nbsp;&nbsp; if ( circle == nil ) then&nbsp; --make sure the first object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if ( circle == nil ) then&nbsp; --make sure the other object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp; local dx = circle.x - circle1.x &nbsp;&nbsp; local dy = circle.y - circle1.y &nbsp;&nbsp; local distance = math.sqrt( dx\*dx + dy\*dy ) &nbsp;&nbsp; local objectSize = (circle1.contentWidth/2) + (circle.contentWidth/2) &nbsp;&nbsp; if ( distance \< objectSize ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true &nbsp;&nbsp; end &nbsp;&nbsp; return false end if hasCollidedCircle then &nbsp;&nbsp;&nbsp; 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 &nbsp;&nbsp;&nbsp; text = "you win" end
  1. 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.

  1. 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.

here is an other code

error: attempt to index local circle a nill value !!

[code]

–schermata iniaziale
local play
–schermata di gioco
local circle

 
local circle1
local levelnum
local lvelText
local winText
–altro
local W = display.contentWidth / 2
local H = display.contentHeight / 2

function main()
menuPrincipale() 
end

function menuPrincipale()
  schermataMenuGroup = display.newGroup()

 Play = display.newText(“PLAY!”,0,0, “Arial” ,30)
 Play:setReferencePoint(display.CenterReferencePoint)
 Play.x = W; Play.y = H + 50
 Play.name = “playbutton”
 
 schermataMenuGroup:insert(Play)
 
  Play:addEventListener(“tap”, loadGame)
 
 end
 
 function loadGame(event)
 if event.target.name == “playbutton” 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

if t.x == circle1.x and t.y == circle1.y then
   --if both circles x’s and y’s are the same then…
  print(“Circles On Each Other”)
 --create text
 winText = display.newText(“You Win!”, 0, 0, “Arial”, 20)
winText.x = W
winText.y = H

 

 end
 end
end

    
function addGameScreen()

 circle = display.newCircle(W, H-130, 50)
 circle.strokeWidth = 3

 
 circle1 = display.newCircle(W, H+130, 50)
 circle1:setFillColor(0, 0, 0, 0)
 circle1.strokeWidth =3
 end

  circle:addEventListener( “touch”, dragMe)

main()

[b] [[b]/code]

Sorry about the code confusion.  Typo on my part.  To close your code block it’s [/code].  The BB code to bold the first [ got in the code some how.

Next, it’s really helpful for you to include the entire error including the backtrace with line numbers and then point out the line number where the error occurred.  Make sure to read and understand this blog post:

http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

In this case, your problem is here:

function addGameScreen() &nbsp;&nbsp;&nbsp; circle = display.newCircle(W, H-130, 50) &nbsp;&nbsp;&nbsp; circle.strokeWidth = 3 &nbsp; &nbsp;&nbsp;&nbsp; circle1 = display.newCircle(W, H+130, 50) &nbsp;&nbsp;&nbsp; circle1:setFillColor(0, 0, 0, 0) &nbsp;&nbsp;&nbsp; circle1.strokeWidth =3 end &nbsp; circle:addEventListener( "touch", dragMe) &nbsp; main()

In this case, your activity is started when the function main() gets called.  From there a cascading set of events happens which includes calling your function addGameScreen().  The problem is before main() runs you have this line of code:

circle:addEventListener( “touch”, dragMe)

The object circle has not been created yet because addGameScreen() hasn’t run yet since:

circle:addEventListener( “touch”, dragMe)

happens before:

main()

The logical thing to me would be to move your addEventListener() inside of addGameScreen() as such:

function addGameScreen() &nbsp;&nbsp;&nbsp; circle = display.newCircle(W, H-130, 50) &nbsp;&nbsp;&nbsp; circle.strokeWidth = 3 &nbsp; &nbsp;&nbsp;&nbsp; circle1 = display.newCircle(W, H+130, 50) &nbsp;&nbsp;&nbsp; circle1:setFillColor(0, 0, 0, 0) &nbsp;&nbsp;&nbsp; circle1.strokeWidth =3 &nbsp;&nbsp;&nbsp; circle:addEventListener( "touch", dragMe)&nbsp;&nbsp; --\<------ move it here &nbsp; end &nbsp; main()

Thanks!!!

And which is the best metode to add a level in the code??

Thanks??

Which is the best metode to add and change levels in a game?

You are asking quite a few questions.   First, you need to know when the two circles touch.  This is called a collision and you need to know how to  detect a collision detection.   Please see this blog post:

http://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Once you’ve detected the collision, then you need to put text on the screen.  See the display.newText() API call for that:

http://docs.coronalabs.com/api/library/display/newText.html

ok!!!:slight_smile: thanks

The text  not appears, where is the error?

code:

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()

Try text.text =“you win”

No the text not appears!

change the variable name of your text object

txt = display.newText("", 0, 0, "Arial", 30) txt.x = W txt.y = H

txt.text = "You Win"

no… it dosen’t work