trying to make eyes blink every 10 secs using movieclip.newAnim()

Hello,

I’m new at Lua, so a newbie.
I’m trying to make the eyes of my drawn character blink using movieclip.newAnim()

This is my code for now:
local movieclip = require “movieclip”
myAnim = movieclip.newAnim(
{
“eyeshalfclosed-pad.png”,
“eyesclosed-pad.png”
}
)
myAnim.x = display.contentWidth /126 * 57
myAnim.y = display.contentHeight / 81 * 42
myAnim:play()

Now the eyes blink constantly.

I’d like it to blink once every 10- secs. I’ve tried
timer.performWithDelay
but I guess I’m doing it wrong. Can anyone help me in the right direction? [import]uid: 100901 topic_id: 19968 reply_id: 319968[/import]

Something like this should work, using the runtime listener means you can easily turn the blinking on or off by setting the eyeBlank variable, and you can adjust the length of time the eyes stay open/shut with the eyeCloseLength and eyeBlinkInterval variables.
[lua]local eyeTimer = system.getTimer()
local eyeBlink = true
local eyeCloseLength = 500
local eyeBlinkInterval = 10000

myAnim:stopAtFrame(1)

local gameLoop = function ()

if eyeBlink == true then

local t = system.getTimer()
local elapse = t - eyeTimer

if elapse > eyeBlinkInterval then

myAnim:stopAtFrame(2)

local openEyes = function ()

myAnim:stopAtFrame(1)

end

timer.performWithDelay(eyeCloseLength, openEyes)

eyeTimer = system.getTimer()

end

end

end

Runtime:addEventListener(“enterFrame”, gameLoop) [import]uid: 93133 topic_id: 19968 reply_id: 77761[/import]

Thanks alot Nick!

My husband will now explain to me what you did in your code, so I understand what I did wrong.
[import]uid: 100901 topic_id: 19968 reply_id: 77768[/import]

Actually I would use a timer.performWithDelay() call to trigger every 10 seconds.

Then there should be options on the play() command to cause it not to loop, but just play the animation once. But with only 2 frames it’s going to be hardly noticeable. So you need to have the timer kick off every 10 seconds, calls a function that plays only the closed eye frame, and that function will have another timer.performWithDelay() for the number of millseconds you want to be closed, and it calls a function that plays the open eyes version.
[import]uid: 19626 topic_id: 19968 reply_id: 77778[/import]

Thanks Robmiracle,

That was my original idea, but I couldn’t get it to work.
I’m quite new at this, so I guess I was messing the function up i think. [import]uid: 100901 topic_id: 19968 reply_id: 77784[/import]

Something like this:

[code]

olhosplayer=movieclip.newAnim({ “images/olhos_normal.png”, “images/olhos_fechados.png”})

olhosplayer.framePosition = “abertos”

local playerOlhosAnimation = function()

local function abrirolhos()
if olhosplayer.framePosition == “fechados” then
olhosplayer:stopAtFrame( 1 )
olhosplayer.framePosition = “abertos”
end
end

if olhosplayer.framePosition == “abertos” then
olhosplayer:stopAtFrame( 2 )
olhosplayer.framePosition = “fechados”
timer.performWithDelay( 300, abrirolhos, 1)
end
end

player.olhosanimTimer = timer.performWithDelay( 5000, playerOlhosAnimation, 0 )
[/code] [import]uid: 91584 topic_id: 19968 reply_id: 78107[/import]

Thank you idea.factory,

One question tho.
In your code i see:
if olhosplayer.framePosition == “fechados”

But above i see :

olhosplayer.framePosition = “abertos”

So that I don’t quite understand [import]uid: 100901 topic_id: 19968 reply_id: 78157[/import]

Hehehe, sorry. I understand now.

I am very new at programming, so it takes longer for me to understand. But I did understand it this time without asking my husband to explain to me :slight_smile: [import]uid: 100901 topic_id: 19968 reply_id: 78158[/import]

fechados is closed in portuguese :smiley: [import]uid: 91584 topic_id: 19968 reply_id: 78684[/import]

Hi

Is this possible to do with spriteSheets instead of using moveclip animation? I have tried, but I can’t get it to work properly. I would really appreciate if anybody could guide me in the right direction.

Thank you in advance :slight_smile:
[import]uid: 122802 topic_id: 19968 reply_id: 97908[/import]

You can do this with sprites. I use spritehelper.
You can make the animation in spritehelper and set the speed.
Or you can build the setup in your lua editor. Just copy the frames needed in the tables as many as you want.
[import]uid: 100901 topic_id: 19968 reply_id: 97909[/import]

Hi

Thank you for your quick response. I have just brought Sprite helper and I have made and saved the animation, but I don’t know how to get it into my game. I am not used to work with sprites, so I would really appreciate if you could give me an example.

Thank you very much in advance.

[import]uid: 122802 topic_id: 19968 reply_id: 98062[/import]

[code]
local sprite = require(“sprite”)
–assign spritesheet made with spritehelper info
local spritesheetName = require(“krab01”)

local spriteData = spritesheetNaam.getSpriteSheetData()
–location of image file
local sheet1 = sprite.newSpriteSheetFromData( “krab01.png”, spriteData )
–create spriteset
local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 10)
–set animation
sprite.add( spriteSet1, “krab01”, 1, 23, 1000, 0 )
–assign display object
local krab01 = sprite.newSprite( spriteSet1 )
krab01.x=250
krab01.y=296
–assign animation settings
krab01:prepare(“krab01”)
–play animation
krab01:play()
[/code] [import]uid: 100901 topic_id: 19968 reply_id: 98064[/import]

Hi T.alberga

Thank you for the example. I have got every thing to work pretty well except I don’t know how to make it blink every 10 second - can you help me with that? Right now my character is only blinking once and then it stops on the last frame.

Thank you in advance, I appreciate your help.
[import]uid: 122802 topic_id: 19968 reply_id: 98510[/import]

Hello,

sprite.add( spriteSet1, "krab01", 1, 23, 1000, 0 )   

the 0 at the end makes it loop.

What I do is I edit the lua file generated by spritehelper.
I just copy the sprites I need. I make several copies of the frame with the eye open in the table.
That way it keeps showing the eyes open for several frames. At the end I add the eyes closed frame and done.

[import]uid: 100901 topic_id: 19968 reply_id: 98546[/import]

Hi T.alberga

Thank you very much for your help, it is working now.

[import]uid: 122802 topic_id: 19968 reply_id: 99383[/import]