newImage events

Looking for tips on how I might pull this off…

I planning on creating multiple images based on user preferences. I would first like to be able to write text over the object (pulling from a word bank). Then when the user touches the image be able to tell what that text is.

For example… If i have a crate.png image and would like to dynamical right “Hello”. I would create 5 instances of the display.newImage(“crate”) and then add the letters. Let’s say the use touches the “H” of hello, i would like to know that the “H” box was selected.

Thanks,
Alan [import]uid: 11072 topic_id: 3578 reply_id: 303578[/import]

[lua]local letters = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”

local function onTap(event)

– bring crate to fromt
event.target.parent:insert(event.target)

– get letter of crate tapped
print("you touched " … event.target.letter)

– event is handled
return true

end

local function addCrate(letter)

– create container
group = display.newGroup()

– add image to group
img = display.newImage(group, “crate.png”)

–add letter to group
txt = display.newText(group, letter, 23, 10, native.systemFontBold, 24)
txt:setTextColor(255, 255, 255)

– dynamically assign a variable
group.letter = letter

– place crate randomly
group.x=math.random()*320
group.y=math.random()*480

– add tap event
group:addEventListener(“tap”, onTap)

end
local function main()

for i=1, 50, 1 do

local rnd = math.ceil(math.random() * #letters)
local letter = string.sub(letters, rnd, rnd)

addCrate(letter)

end

end

main()[/lua] [import]uid: 6645 topic_id: 3578 reply_id: 10811[/import]

There are many many many ways to pull this off.

Off the top of my head (pseudo code, won’t work as-is):

[lua]local secretWord = “Hello”
–Create a table to identify the number of boxes for letters
local boxes = {}

–Then create a for loop to create the boxes and add listeners
for i=1, i < string.len(secretWord), 1 do
boxes[i] = display.newImage( “images/box.png”, x, y )
boxes[i]:addEventListener(“tap”, boxHitListener)

–Might as well add something here to create the blank slots
–that will be updated when the letter is show.
– Could be 26-letter sprites that change frames,
– table with binary objects, or something else.
end

–Need a listener to do something with the hits
local boxHitListener = function(event)
Scan to determine which box
for i=1, i < string.len(secretWord), 1 do
if (event.target == boxes[i]) then
–Got Index
–Show Letter at Index
end
end
end[/lua] [import]uid: 11024 topic_id: 3578 reply_id: 10814[/import]