Functions

I am currently working on a kids app which has a list of words. The top word has 100% alpha the rest are set at 50%… The first word has a touch function much like this one.

[lua]function testFunction (event)
if event.phase == “began” then
print “pressed”
elseif event.phase == “ended” then
print “released”
end
end[/lua]

When it is touched it plays an animation. When the animation is complete I would like to show 100% alpha on the next word and have it touchable to play an animation. Can anybody show me an example of this being done?

Thanks

Jake

[import]uid: 51459 topic_id: 14806 reply_id: 314806[/import]

[code]

wordArray = {}

wordArray[1] = display.newText(“Word One”, 140, 120, native.systemFont, 16)
wordArray[1].alpha = 1
wordArray[2] = display.newText(“Word Two”, 140, 240, native.systemFont, 16)
wordArray[2].alpha = 0.5
wordArray[3] = display.newText(“Word Three”, 140, 360, native.systemFont, 16)
wordArray[3].alpha = 0.5

local i = 1

function testFunction (event)

if event.phase == “began” then

wordArray[i].alpha = 0.5

elseif event.phase == “ended” then

wordArray[i +1].alpha = 1
print “play animation…”
wordArray[i]:removeEventListener(“touch”, testFunction)
i = i + 1
wordArray[i]:addEventListener(“touch”, testFunction)

end

end

wordArray[i]:addEventListener(“touch”, testFunction)

[/code] [import]uid: 87611 topic_id: 14806 reply_id: 54742[/import]

Thank you so much Lewis.Elliott.92 This is exactly what I needed!!

Jake
[import]uid: 51459 topic_id: 14806 reply_id: 54743[/import]

I just tested it out and I got this error

Runtime error
…ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/349/main.lua:42: attempt to index field ‘?’ (a nil value)
stack traceback:
[C]: ?
…ZS-a189GNQo1k+++TI/-Tmp-/TemporaryItems/349/main.lua:42: in function <…zs-a189gnqo1k>
?: in function <?:215>

here is the code…

[lua]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

wordArray = {}

local i = 1

local loqsprite = require(‘loq_sprite’)

local screenW = display.contentWidth
local screenH = display.contentHeight

local BG = display.newImageRect(“background.png”, 1024, 768)
BG.x = screenW/2; BG.y = screenH/2

wordArray[1] = display.newText(“Word One”, 140, 120, native.systemFont, 16)
wordArray[1].alpha = 1
wordArray[2] = display.newText(“Word Two”, 140, 140, native.systemFont, 16)
wordArray[2].alpha = 0.5
wordArray[3] = display.newText(“Word Three”, 140, 160, native.systemFont, 16)
wordArray[3].alpha = 0.5
local bfactory = loqsprite.newFactory(“boy”)
local boy = bfactory:newSpriteGroup()
boy.x = 480; boy.y = 450

boy:play(“aniBoy ball”)

local bfactory = loqsprite.newFactory(“ball”)
local ball = bfactory:newSpriteGroup()
ball.x = 600; ball.y = 450

function testFunction (event)

if event.phase == “began” then

wordArray[i].alpha = 0.5


elseif event.phase == “ended” then

wordArray[i +1].alpha = 1
print “play animation…”
wordArray[i]:removeEventListener(“touch”, testFunction)
i = i + 1
wordArray[i]:addEventListener(“touch”, testFunction)

end

end

wordArray[i]:addEventListener(“touch”, testFunction)[/lua] [import]uid: 51459 topic_id: 14806 reply_id: 54744[/import] </…zs-a189gnqo1k>

Is that after running it 3 times? You only have 3 values in your array, so every time i tries to reference wordArray[4], it will crash. [import]uid: 87611 topic_id: 14806 reply_id: 54791[/import]

I’ll add another word array… But I have another small issue… I when I click on each word it plays the same animation… How do I get it to play other animations… Can you show me with a print statement saying something like “play animation 2”? [import]uid: 51459 topic_id: 14806 reply_id: 54802[/import]

I would do like this:

[lua]local words = {}
local listener

listener = function( event )

local phase = event.phase
local word = event.target

if ( phase == “ended” ) then

local function complete()

word.alpha = 0.5
word.xScale, word.yScale = 1, 1
word.x = 80

local newIndex = (word.index % 5)+1
words[newIndex].alpha = 1

word:removeEventListener( “touch”, listener )
words[newIndex]:addEventListener( “touch”, listener )
end

– animations
if word.index == 1 then
transition.to ( word, { time = 500, xScale = 1.2, yScale = 1.2, onComplete = complete } )
elseif word.index == 2 then
transition.to ( word, { time = 1000, rotation = 360, onComplete = complete } )
else
transition.to ( word, { time = 1000, x = 400, onComplete = complete } )
end
end
end

for i=1,5 do

local word = display.newText ( "Word " … i, 0, 0, native.systemFont, 16)
word.x, word.y = 80, 50 * i

word.index = i
table.insert ( words, word )
word.alpha = 0.5

end

words[1].alpha = 1
words[1]:addEventListener( “touch”, listener ) [import]uid: 13632 topic_id: 14806 reply_id: 54814[/import]

Hey ojnab Thats was very interesting what you came up with! But It’s not exactly what I am looking for. This is going to be a kids app and theres going to be a set of words that represent certain animations that are show on the screen. Like for example. Click on the word that says “Apple” it plays an animation of an apple being eaten to the core. Click on the next word “Seed” it plays an animation of an apple tree growing. Click on the word “Fall” it shows an apple falling. Click on the word “Worm” it shows a worm popping out of the apple…

With the line of code that you presented I am not sure how to change each word to say what I want. I also don’t want the words to animate I want the picture thats on the screen to animate… So If you can just show me a set of words when you click on each a print statement says something like “play animation 1” clicking on the next word the print statement will say “play animation 1”… etc. If you can show me something simple like that it will help me greatly!

Thanks

Jake [import]uid: 51459 topic_id: 14806 reply_id: 54867[/import]

You need to learn how tables work, and how you can reference them. Check out for loops and tables in the api. You could easy store these animations then reference them using [i]. [import]uid: 87611 topic_id: 14806 reply_id: 54893[/import]

Hey Lewis.Elliott.92 I’m not sure where I can find loops and tables in the api… Can you provide a link or something? [import]uid: 51459 topic_id: 14806 reply_id: 54895[/import]

hi. yes but that is just a small change from what I showed. I just put animations on the text to illustrate where you should place your animation calls.
you can use movieclips for your animations.

[lua]local words = { “Apple”, “Seed”, “Fall”, “Worm” }
local listener

listener = function( event )

local phase = event.phase
local word = event.target

if ( phase == “ended” ) then

local function complete()

word.alpha = 0.5

local newIndex = (word.index % #words)+1
words[newIndex].alpha = 1

word:removeEventListener( “touch”, listener )
words[newIndex]:addEventListener( “touch”, listener )
end

– delay new word highlight if needed
timer.performWithDelay ( 1000, complete )

– animations
if word.index == 1 then print (“play animation 1”)
elseif word.index == 2 then print (“play animation 2”)
elseif word.index == 3 then print (“play animation 3”)
elseif word.index == 4 then print (“play animation 4”)
end
end
end

– draw words
for i=1,#words do

local word = display.newText ( words[i], 0, 0, native.systemFont, 16)
word.x, word.y = 80, 50 * i

word.index = i
words[i] = word
word.alpha = 0.5

end

words[1].alpha = 1
words[1]:addEventListener( “touch”, listener )

[import]uid: 13632 topic_id: 14806 reply_id: 54896[/import]

read first half “programming in lua” by Roberto Ierusalimschy and you will know all there is to know
about tables and loops in lua

[import]uid: 13632 topic_id: 14806 reply_id: 54897[/import]

you can find it online here:
http://www.lua.org/pil/ [import]uid: 13632 topic_id: 14806 reply_id: 54901[/import]

THANKS ojnab! [import]uid: 51459 topic_id: 14806 reply_id: 54907[/import]

Thanks for the link to its got some great stuff!!! :slight_smile: [import]uid: 51459 topic_id: 14806 reply_id: 54912[/import]

Still studying those tables! Not sure how to change the color of the text and how to make the videos play again after the all the words have been selected.

Thanks [import]uid: 51459 topic_id: 14806 reply_id: 56727[/import]

I guess what I need help with is a game loop… Because I am removing the animations upon completion and then I am wanting to bring them back to life once the cycle of words have been all touched… I’m sure its an easy fix. I just have never done it before :frowning: [import]uid: 51459 topic_id: 14806 reply_id: 56733[/import]

I FIGURED OUT THE LOOP I WAS LOOKING FOR CHECK IT OUT!!
[lua]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

wordArray = {}

local i = 1

local loqsprite = require(‘loq_sprite’)

local screenW = display.contentWidth
local screenH = display.contentHeight

local BG = display.newImageRect(“background.png”, 1024, 768)
BG.x = screenW/2; BG.y = screenH/2

local chair = display.newImageRect(“chair.png”, 504, 208 )
chair.x = 488; chair.y = 248

local words = { “Pool”, “Jump”, “Splash”, “Wet”}
local listener

local mfactory = loqsprite.newFactory(“momWet”)
local mom = mfactory:newSpriteGroup()
mom.x = 468 ; mom.y = 68

local jfactory = loqsprite.newFactory(“girl”)
local girl = jfactory:newSpriteGroup()
girl.x = 920 ; girl.y = 750
atrace(xinspect(girl:getSpriteNames()))

function splash()

transition.to( girl, { time=1500, x= 1020, y= 800, alpha = 0 } )
local sfactory = loqsprite.newFactory(“splash”)
local splash = sfactory:newSpriteGroup()
splash.x = 520 ; splash.y = 350
splash:play(“splash splash”)

end

function bringBack ()
girl:prepare(“girlJump bending”)
transition.to( girl, { time=1500, x= 920, y=750, alpha = 1 } )
end

listener = function( event )

local phase = event.phase
local word = event.target

if ( phase == “ended” ) then

local function complete()

word.alpha = 0.5

local newIndex = (word.index % #words)+1
words[newIndex].alpha = 1

word:removeEventListener( “touch”, listener )
words[newIndex]:addEventListener( “touch”, listener )
end

– delay new word highlight if needed
timer.performWithDelay ( 100, complete )

– animations
if word.index == 1 then girl:play(“girlJump bending”); mom:prepare(“momWet sit”)
elseif word.index == 2 then girl:play(“girlJump jump”)
elseif word.index == 3 then splash();mom:prepare(“momWet wet”)
elseif word.index == 4 then mom:play(“momWet wet”); bringBack ()
end
end
end

– draw words
for i=1,#words do

local word = display.newText ( words[i], 0, 0, native.systemFont, 46)
word.x, word.y = 120, 120 * i
word:setTextColor( 255,0,0 )

word.index = i
words[i] = word
word.alpha = 0.5

end

words[1].alpha = 1
words[1]:addEventListener( “touch”, listener )[/lua][/lua]

Thanks again for all the help!! [import]uid: 51459 topic_id: 14806 reply_id: 57336[/import]