【问题标题】:Corona SDK: spacing spawned objectsCorona SDK:间隔生成的对象
【发布时间】:2015-05-01 22:45:37
【问题描述】:

我不确定这是否是迄今为止最好的方法,但这是我想出的。我试图生成三个单词,每个单词都有自己的名字,这样它们就可以有一个单独的触摸事件。一个是正​​确的答案,另外两个是错误的,并且会发出相应的声音。 无论如何,我现在的挑战是试图将这三个单词均匀地分开,因为它们只是堆叠在一起。每个单词图像的大小相同(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()

单词接近我需要的位置,但我仍然收到错误,并且我还得到了右上角的第四个单词。

【问题讨论】:

    标签: coronasdk


    【解决方案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
    

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

    【讨论】:

    • 感谢您分享您的知识!当我尝试这个时,我得到一个错误“尝试对本地'v'执行算术”这将来自sightword.x行。
    • 我还有第四个字显示在右上角。
    • 回答您的第一条评论:尝试使用“makeaWord(v)”而不是“makeaWord()”。也就是说,将值传递给函数并在函数内部获取它。检查您的第二个错误是否仍然存在?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 2020-07-23
    • 2023-03-28
    • 2020-11-16
    相关资源
    最近更新 更多