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!