Luaのコルーチンを使ったサンプルです。コルーチンは、関数の途中で処理を中断して呼び出し側に処理を戻し、また再度同じ箇所から処理を行える機能との事です。
状態を変更しながらの制御、フレーム毎のアップデート処理が思い場合にフレーム分割して計算、ジェネレーターとして数値の取得、Tweenといった処理をスッキリ書けそうです。local 変数を関数の中に定義し、スコープを限定してstatic変数のように何度も使いまわせるのも良さそうです。
このサンプルではコルーチン部分で、動画のスピードを上げ下げしています。
autowatch = 1 gc = 1 local mov1 = jit.new("jit.qt.movie",this.drawto) mov1.adapt = 1; mov1.rate = 0.1; mov1:read("bball.mov") local frame = jit.matrix(); local plane = jit.new("jit.gl.videoplane",this.drawto); plane.scale = 0.8 plane.automatic = 1; --coroutine local update_co = coroutine.create( function() -- 動画再生スピードを上げる while true do if mov1.rate < 50. then mov1.rate = mov1.rate + 0.1; mov1:matrixcalc(frame.name,frame.name) else break; end coroutine.yield () end -- 動画再生スピードを下げる while true do if mov1.rate > 0 then mov1.rate = mov1.rate - 0.1; mov1:matrixcalc(frame.name,frame.name) else break; end coroutine.yield () end return end ) function draw() if( coroutine.status(update_co) ~= "dead") then print(coroutine.resume(update_co)) print(coroutine.resume(update_co)) print(coroutine.resume(update_co)) else print(coroutine.status(update_co)); end plane:jit_matrix(frame.name) end
coroutine.wrap( function ) で既存の関数をコルーチンとして扱う事もできますが、coroutine.* 系の関数を使えなくなるようなので、基本的には coroutine.create を用いてつくるのが良さそうです。