0
votes

I'm not sure if this is the best way to do this so far, but it's what I've come up with. I am trying to spawn three words each with their own name so that they can have an individual touch event. One will be a correct answer and the other two will be wrong and will have corresponding sounds. Anyway, my challenge right now is trying to space the three words evenly apart from one another as they are just stacked on top of one another. Each word image is the same size (150x75). How do I get them to space evenly apart from one another?

    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()

Here is what I tried:

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()

The words are close to where I need them to be, but I still get the error and I also get the fourth word in the upper corner.

1

1 Answers

1
votes

You can arrange them horizontally as:

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

Keep coding............... :)