Calling Data From A Function

ConnectingDots = {}

function StartAgain()

   FirstDot = display.newImageRect(“Dot.png”, 40, 40)

   FirstDot.x = 300;

   FirstDot.y = 300;

   FirstDot.alpha = 1;

   FirstDot:addEventListener(“touch”, RemoveDots)

   table.insert(ConnectingDots, FirstDot)

   screenGroup:insert(FirstDot)

   – 2nd Dot

   SecondDot = display.newImageRect(“Dot.png”, 40, 40)

   SecondDot.x = 400;

   SecondDot.y = 400;

   SecondDot.alpha = 1;

    SecondDot:addEventListener(“touch”, RemoveDots)

    table.insert(ConnectingDots, SecondDot)

    screenGroup:insert(SecondDot)

end

* How do i call the First Dot alone?

* RemoveDots is a function to erase the dot.

– Thank you!

You might need some clarifications before anyone can really help.  We usually talk about calling functions with certain data (so your subject line doesn’t entirely make sense).

> How do i call the First Dot alone?

It’s not clear what you’re asking here.  As it’s written, you can’t call just FirstDot (I assume you mean “create FirstDot alone”).  You’ve put the creation of both FirstDot and SecondDot into one function … you can either execute that in its entirety or not.  

If I understand correctly you are asking how you can reference the individual dots within, say, the RemoveDots function. 

You would do that like this (I’ve streamlined your code a little to be a bit more dynamic):

[lua]

local ConnectingDots = {}

function RemoveDots (event)

  local obj = event.target

  if event.phase == “began” then

     display.remove(obj)

      obj = nil

  end

end

function StartAgain()

  local dots = 2

  local xp = {300, 400}

  local yp = {300, 400}

  for a = 1, #dots, 1 do

      local i = display.newImageRect(“Dot.png”, 40, 40)

      i.x = xp[a]

      i.y = yp[a]

      i.alpha = 1

      i:addEventListener(“touch”,RemoveDots)

      table.insert(ConnectingDots, i)

      screenGroup:insert(i)

  end

end

[/lua]

i’ll explain to you guys what i want to do. i’m making a tracing application. like letter A.

Dots will be assigned in certain x and y coordinates. to form a letter A.

Now, i want to put dots one by one. meaning, FirstDot then SecondDot. after i’ve touched the first dot, secondDot will now appear.

  • The removedots function is the one that will remove the dots if it is going to be touched.

thaaank you guys! thank you for your effort.

Ok, this will do that. To add more dots just add more X and Y values to the xPos and yPos tables.

[lua]

local ConnectingDots = {}

function RemoveDots (event)

  local obj = event.target

  local id = obj.id

  if event.phase == “began” then

      ConnectingDots[id].alpha = 0

      ConnectingDots[id+1].alpha = 1

  end

end

function StartAgain()

  local xPos = {300, 400}

  local yPos = {300, 400}

  local dots = math.min(#xPos, #yPos)

  for a = 1, dots, 1 do

      local i = display.newImageRect(“Dot.png”, 40, 40)

      i.x = xPos[a]

      i.y = yPos[a]

      i.alpha = 0

      i:addEventListener(“touch”,RemoveDots)

      screenGroup:insert(i)

      i.id = a

      ConnectingDots[a] = i

  end

  ConnectingDots[1].alpha = 1

end

[/lua]

in line 39, that will make a dot appear first? right?

Thaaank you, Nick Sherman. i owe you a lot. :slight_smile:

You might need some clarifications before anyone can really help.  We usually talk about calling functions with certain data (so your subject line doesn’t entirely make sense).

> How do i call the First Dot alone?

It’s not clear what you’re asking here.  As it’s written, you can’t call just FirstDot (I assume you mean “create FirstDot alone”).  You’ve put the creation of both FirstDot and SecondDot into one function … you can either execute that in its entirety or not.  

If I understand correctly you are asking how you can reference the individual dots within, say, the RemoveDots function. 

You would do that like this (I’ve streamlined your code a little to be a bit more dynamic):

[lua]

local ConnectingDots = {}

function RemoveDots (event)

  local obj = event.target

  if event.phase == “began” then

     display.remove(obj)

      obj = nil

  end

end

function StartAgain()

  local dots = 2

  local xp = {300, 400}

  local yp = {300, 400}

  for a = 1, #dots, 1 do

      local i = display.newImageRect(“Dot.png”, 40, 40)

      i.x = xp[a]

      i.y = yp[a]

      i.alpha = 1

      i:addEventListener(“touch”,RemoveDots)

      table.insert(ConnectingDots, i)

      screenGroup:insert(i)

  end

end

[/lua]

i’ll explain to you guys what i want to do. i’m making a tracing application. like letter A.

Dots will be assigned in certain x and y coordinates. to form a letter A.

Now, i want to put dots one by one. meaning, FirstDot then SecondDot. after i’ve touched the first dot, secondDot will now appear.

  • The removedots function is the one that will remove the dots if it is going to be touched.

thaaank you guys! thank you for your effort.

Ok, this will do that. To add more dots just add more X and Y values to the xPos and yPos tables.

[lua]

local ConnectingDots = {}

function RemoveDots (event)

  local obj = event.target

  local id = obj.id

  if event.phase == “began” then

      ConnectingDots[id].alpha = 0

      ConnectingDots[id+1].alpha = 1

  end

end

function StartAgain()

  local xPos = {300, 400}

  local yPos = {300, 400}

  local dots = math.min(#xPos, #yPos)

  for a = 1, dots, 1 do

      local i = display.newImageRect(“Dot.png”, 40, 40)

      i.x = xPos[a]

      i.y = yPos[a]

      i.alpha = 0

      i:addEventListener(“touch”,RemoveDots)

      screenGroup:insert(i)

      i.id = a

      ConnectingDots[a] = i

  end

  ConnectingDots[1].alpha = 1

end

[/lua]

in line 39, that will make a dot appear first? right?

Thaaank you, Nick Sherman. i owe you a lot. :slight_smile: