Corona SDK:间隔生成的对象

时间:2013-11-30 20:53:20

标签: corona

到目前为止,我不确定这是否是最好的方法,但这就是我想出来的。我试图用自己的名字产生三个单词,以便他们可以有一个单独的触摸事件。一个是正​​确答案,另外两个将是错误的并且会有相应的声音。 无论如何,我现在的挑战是试图将三个单词彼此均匀分开,因为它们只是叠在一起。每个单词图像的大小相同(150x75)。如何让它们在彼此之间均匀分布?

    local content = require "content"
--chooses a random number according to the maximum number available in the table
local rnd = math.random
local maxSightwords = 3

print (rnd)

local wordIndex = rnd(1, #content)
    print (wordIndex)

--on tap of a star the name of the star prints
local function wordTap (event)
    print(event.target.name)
end

local function makeaWord()
    --chooses a random word from the content table
    local wordIndex = rnd(1, #content)
    print (wordIndex)
    local word = (content[wordIndex].word)
    --uses the word index variable to get a random word image
    local sightword = display.newImage("images/words/"..word..".png")
    sightword.x = display.contentWidth/2
    sightword.y = display.contentHeight/2
    --sets the words name as the name of the word
    sightword.name = word
    sightword:addEventListener ("tap", wordTap)
    print (wordIndex)

end

for v = 1, maxSightwords do
    makeaWord (v)
end

makeaWord()

以下是我的尝试:

local content = require "content"
--chooses a random number according to the maximum number available in the table
local rnd = math.random
local maxSightwords = 3
local gap = 10 -- gap between words
local _w =  display.contentWidth
local _h = display.contentHeight
local playOrder


local wordIndex = rnd(1, #content)

--on tap of a star the name of the star prints
local function wordTap (event)
    print(event.target.name)
end

local function makeaWord(v)
    --chooses a random word from the content table
    local wordIndex = rnd(1, #content)
    print (wordIndex)
    local word = (content[wordIndex].word)
    --uses the word index variable to get a random word image
    local sightword = display.newImageRect("images/words/"..word..".png", 150, 75)
    sightword.x = (_w/2-(sightword.contentWidth+gap))+((sightword.contentWidth+gap)*v)
    sightword.y = display.contentHeight/2
    --sets the words name as the name of the word
    sightword.name = word
    sightword:addEventListener ("tap", wordTap)
    print (wordIndex)

end

for v = 1, maxSightwords do
    makeaWord (v)
end

makeaWord()

这些单词接近我需要它的位置,但我仍然得到错误,我也得到了上角的第四个单词。

1 个答案:

答案 0 :(得分:1)

您可以将它们水平排列为:

local _w =  display.contentWidth
local _h = display.contentHeight
local maxSightwords = 3
local gap = 10 -- gap between words

local function makeaWord(v) -- get 'v' here
    local sightword = display.newRect(0,0,150,75)
    sightword.x = -(_w/2-(sightword.contentWidth+gap))+((sightword.contentWidth+gap)*v)
    --[[ or even simply hardcode as:
       sightword.x = -80+(160*v) -- for iPhone 
    ]]--
    sightword.y = display.contentHeight/2
end

for v = 1, maxSightwords do
    makeaWord (v)
end

继续编码...............:)