简体   繁体   中英

multiple copies of the same object that can be interacted with separately

I am trying to create multiple copies of the same display object with individual properties that can be accessed from anywhere. Particularly, I want to be able to tap and interact with each of them. My code so far is below:-

centerX = display.contentCenterX
centerY = display.contentCenterY

local function onGemTouch(event)
    print("to see if each object can be tapped")
end

local spawnTable = {}


local function gamebegin(params)

    ball = display.newImageRect(params.image, 62,62)
    ball.x = centerX - 4*60 +2*(params.x-1)*60
    ball.y = centerY -3*60 + (params.y-1)*60
    ball.ballTable = params.ballTable
    ball.index = #ball.ballTable+1
    ball.myName = ball.index
    ball.ballTable[ball.index] = ball

    return ball
end
--i have four images that I want to use randomly--

for i = 1, 4 do
    for j = 1,7 do
        local spawn = gamebegin({image = "images/" .. math.random(4)  ..".png", x=i, y = j, ballTable = spawnTable})
    end
end

ball.tap = onGemTouch
ball:addEventListener( "tap", ball )

The code works in the sense that there is no error and the copies apppear in the grid that I want but only the last copy of the object can be clicked. So obviously I am doing something wrong here. Any help will be appreciated.

Without knowing much else about your code (or Corona), I think the right syntax to add a listener is:

ball:addEventListener("tap", onGemTouch)

Since you want to add this to every ball, you either need it inside of the loop creating the balls, or you need it inside the function creating the ball. If we put it inside the function creating the ball, we get:

local function gamebegin(params)
    ball = display.newImageRect(params.image, 62,62)
    ...
    ball:addEventListener("tap", onGemTouch)
    ball.ballTable[ball.index] = ball

    return ball
end

Since gamebegin appears to be just creating a new ball, why not rename it to addBall ? And if you're adding it to the ballTable and never using the return value, just don't return anything:

local function addBall(params)
    ball = display.newImageRect(params.image, 62,62)
    ball.x = centerX - 4*60 +2*(params.x-1)*60
    ball.y = centerY -3*60 + (params.y-1)*60
    ball.ballTable = params.ballTable
    ball.index = #ball.ballTable+1
    ball.myName = ball.index
    ball:addEventListener("tap", onGemTouch)

    ball.ballTable[ball.index] = ball
end

for i = 1, 4 do
    for j = 1,7 do
        addBall({
            image = "images/" .. math.random(4)  ..".png", 
            x=i, 
            y = j, 
            ballTable = spawnTable})
    end
end

Adding suggestions from my comments on your answer:

local centerX = display.contentCenterX
local centerY = display.contentCenterY
local offset = 60
local imageSideLength = 62
local spawnTable = {}

local function onGemTouch(event)
    print("to see if each object can be tapped")
end

local function getBall(imagePath, x, y)
    local ball = display.newImageRect(imagePath, imageSideLength, imageSideLength)
    ball.x = centerX - 4*offset + 2*(x - 1)*offset
    ball.y = centerY - 3*offset + (y - 1)*offset

    return ball
end

for i = 1, 4 do
    for j = 1, 7 do
        local ball = getBall("images/" .. math.random(4) .. ".png", i, j)
        ball:addEventListener("tap", onGemTouch)
        table.insert(spawnTable, ball)
    end
end

Thanks for your reply Millie. Your method might just work but I had stumbled upon another solution in the meantime

Turns out I needed to add addEventListener("tap", onGemTouch) to the spawnTable. But I'll try out your method - it seems easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM