Targeting Spawned Objects

I have been working on a project for an EDTECH course I am taking this semester. The basic idea of my game is to be a circuit board where one can place wires, batteries, and light bulbs on the board to create a working circuit. I have been using many tutorials and have been able to spawn objects (the right way), and drag them as well. I am having trouble figuring out how to get to the next part, setting the focus on a spawned object. I have been trying to add this to a touch listener, but when I do so, it ruins my other touch listeners.

What I want to do hear is be able to rotate and delete a spawned object. Here is some code that I have:

[lua] function battery1vMove( event )

local battery1v = event.target

    if event.phase == “began” then

        battery1v.markX = battery1v.x    – store x location of object

        battery1v.markY = battery1v.y    – store y location of object

    elseif event.phase == “moved” then

        local x = (event.x - event.xStart) + battery1v.markX

        local y = (event.y - event.yStart) + battery1v.markY

        battery1v.x, battery1v.y = x, y    – move object based on calculations above        

    elseif event.phase == “ended” then

        local x = (event.x - event.xStart) + battery1v.markX

        local y = (event.y - event.yStart) + battery1v.markY

         a bunch of code here to drop it into a specific container that I have

        end

        end

    return true

end

local function spawn1v(params)

local battery1v = display.newImage(params.image)

    battery1v.xScale = .50

    battery1v.yScale = .50

    battery1v:scale(.50, .50)

    battery1v.x = 300

    battery1v.y = 280

–Set the objects table to a table passed in by parameters

    battery1v.objTable = params.objTable

–Automatically set the table index to be inserted into the next available table index

    battery1v.index = #battery1v.objTable + 1

–Give the object a custom name

    battery1v.myName = "battery1v : " … battery1v.index

–If the object should have a body create it, else dont.

    if params.hasBody then

–Allow physics parameters to be passed by parameters:

        battery1v.density = params.density or 0

        battery1v.friction = params.friction or 0

        battery1v.bounce = params.bounce or 0

        battery1v.isSensor = params.isSensor or false

        battery1v.bodyType = params.bodyType or “dynamic”

        physics.addBody(battery1v, battery1v.bodyType, {density = battery1v.density, friction = battery1v.friction, bounce = battery1v.bounce, isSensor = battery1v.isSensor})

    end

–The battery1vs group

    battery1v.group = params.group or nil

–If the function call has a parameter named group then insert it into the specified group

    if params.group then

        battery1v.group:insert(battery1v)

    end

–Insert the battery1v into the table at the specified index

    battery1v.objTable[battery1v.index] = battery1v

    if (battery1v.index > 1) then

        battery1v:addEventListener(“touch”, battery1vMove)

    end

    startGroup:insert(battery1v)

return battery1v

end

local localGroup = display.newGroup()

–Create a table to hold our spawn1vs

local spawn1vTable = {}

i = 0

local spawn1vs = spawn1v(

{

image = “battery1v.png”,

objTable = spawn1vTable,

hasBody = true,

friction = 0.4,

bounce = 0.4,

bodyType = “static”,

group = localGroup,

}

)

i = i +1

local function new1v

(event) i = 0

local spawn1vs = spawn1v(

{

image = “battery1v.png”,

objTable = spawn1vTable,

hasBody = true,

friction = 0.4,

bounce = 0.4,

bodyType = “static”,

group = localGroup,

}

)

i = i +1

for i = 1, #spawn1vTable do

print(spawn1vTable[i].myName)

end

end

spawn1vTable[i]:addEventListener(“tap”, new1v)

[/lua]

So after I have spawned an object, I want to be able to set the focus on the object the next time I tap it. For example I might drag this battery to a place and then create a wire and drag it. If I want to delete the battery, I would like to be able to tap it and then hit the delete button to remove it.

Any help is greatly appreciated!

FWIW, Ima newb, your mileage may vary.

I don’t spawn objects like you are trying to, I’m not even sure how you are doing it will work, but it may.  From what I can tell, you are spawning a lot of objects that are local to the “local function spawn1v(params)” and they are all “battery1v”.  While it may spawn the object, I’m thinking you are going to have problems removing the objects and focusing on them.

I do it like this

[lua]

local Battery1v = {}

local function battery1vMove( event )

local ID= event.target.ID

   

if event.phase == “began” then

        battery1v[ID].markX = battery1v[ID].x    – store x location of object

        battery1v[ID].markY = battery1v[ID].y    – store y location of object

     

       blah,

      blah,

     end

return true

end

    --Always reference your object with it’s index ID and you will be able to always focus on the object and change it’s parameters and save them.

local function spawnBattery1v(params.image)

i = #Battery1v + 1

Battery1v[i] = display.newImage(params.image)

blah,

blah,

Battery1v[i].ID = i

end

[/lua]

Hope this helps,

Nail

The tutorial I am using is from http://www.coronalabs.com/blog/2011/09/14/how-to-spawn-objects-the-right-way/. I am probably noobier than you are. I understand I need that index value, but isn’t that being done in the local function spawn1v? If I’m understanding the example I used, it is being put into a table with its index. How could I call that index?

Not sure about the tut, but I believe what you are doing is fundamentally wrong if you want to reference the objects later for any reason.

I believe when you spawn and declare an object “local” in a function, it is exactly that, local to the function and not properly referenced by other functions that are not “inside” of the function.

The second problem I see is that you are creating multiple objects with the same name.  I’ve never had any luck with that.

If you want to spawn objects that you can easily reference, they way I outlined above works great. Just index your variable, battery1v[i].

Nail

It was actually very simple. Thank you for your help, Nail. A classmate in my course helped me with this code:

[lua]

    function rotateActive(event)

    local activeObject = scene.activeObject

        if activeObject ~= nil then

            activeObject.rotation = activeObject.rotation + 90

        end

    return true

    end

    function delete(event)

    local activeObject = scene.activeObject

        if activeObject ~= nil then

        activeObject:removeSelf()

        end

    return true

    end

[/lua]

Glad you got it worked out.  Curious, did you figure out what you were doing wrong with Danny’s code or did you did you decide to spawn directly into a specific indexed table like I suggested. 

Nail

I simply added that code I posted. I believe that in my original code, I was creating an index table like you were suggesting. It just looked a bit different. 

FWIW, Ima newb, your mileage may vary.

I don’t spawn objects like you are trying to, I’m not even sure how you are doing it will work, but it may.  From what I can tell, you are spawning a lot of objects that are local to the “local function spawn1v(params)” and they are all “battery1v”.  While it may spawn the object, I’m thinking you are going to have problems removing the objects and focusing on them.

I do it like this

[lua]

local Battery1v = {}

local function battery1vMove( event )

local ID= event.target.ID

   

if event.phase == “began” then

        battery1v[ID].markX = battery1v[ID].x    – store x location of object

        battery1v[ID].markY = battery1v[ID].y    – store y location of object

     

       blah,

      blah,

     end

return true

end

    --Always reference your object with it’s index ID and you will be able to always focus on the object and change it’s parameters and save them.

local function spawnBattery1v(params.image)

i = #Battery1v + 1

Battery1v[i] = display.newImage(params.image)

blah,

blah,

Battery1v[i].ID = i

end

[/lua]

Hope this helps,

Nail

The tutorial I am using is from http://www.coronalabs.com/blog/2011/09/14/how-to-spawn-objects-the-right-way/. I am probably noobier than you are. I understand I need that index value, but isn’t that being done in the local function spawn1v? If I’m understanding the example I used, it is being put into a table with its index. How could I call that index?

Not sure about the tut, but I believe what you are doing is fundamentally wrong if you want to reference the objects later for any reason.

I believe when you spawn and declare an object “local” in a function, it is exactly that, local to the function and not properly referenced by other functions that are not “inside” of the function.

The second problem I see is that you are creating multiple objects with the same name.  I’ve never had any luck with that.

If you want to spawn objects that you can easily reference, they way I outlined above works great. Just index your variable, battery1v[i].

Nail

It was actually very simple. Thank you for your help, Nail. A classmate in my course helped me with this code:

[lua]

    function rotateActive(event)

    local activeObject = scene.activeObject

        if activeObject ~= nil then

            activeObject.rotation = activeObject.rotation + 90

        end

    return true

    end

    function delete(event)

    local activeObject = scene.activeObject

        if activeObject ~= nil then

        activeObject:removeSelf()

        end

    return true

    end

[/lua]

Glad you got it worked out.  Curious, did you figure out what you were doing wrong with Danny’s code or did you did you decide to spawn directly into a specific indexed table like I suggested. 

Nail

I simply added that code I posted. I believe that in my original code, I was creating an index table like you were suggesting. It just looked a bit different.