Sesuai janji postingan sebelumnya, kali ini tentang menampilkan deret fibonacci sesuai suku yang dimasukkan. Langsung aja ke langkah kerjanya.
2) Buat sebuah input text, sebuah dynamic text, sebuah tombol, dan static text(opsional sih..) pada stage.
Lihat gambar berikut, perhatikan objek yang dipilih dan propertinya (klik gambar untuk memperbesar) :
3) Buat layer baru, tambahkan script :
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.TextEvent;
import flash.events.KeyboardEvent;
var suku,a,b,c:int;
suku_txt.restrict = "0-9";
ok_btn.addEventListener(MouseEvent.CLICK,fibo);
function fibo(e:MouseEvent):void
{
hasil_txt.text = "";
suku = Number(suku_txt.text);
a = 1;
b = 1;
if (suku == 1)
{
hasil_txt.appendText(a.toString());
}
else if (suku == 2)
{
hasil_txt.appendText(a.toString());
hasil_txt.appendText(",");
hasil_txt.appendText(b.toString());
}
else if (suku > 2)
{
hasil_txt.appendText(a.toString());
hasil_txt.appendText(",");
hasil_txt.appendText(b.toString());
hasil_txt.appendText(",");
for (var i:int=3; i<=suku; i++)
{
c = a + b;
a = b;
b = c;
hasil_txt.appendText(c.toString());
hasil_txt.appendText(",");
}
hasil_txt.text = hasil_txt.text.substring(0, hasil_txt.text.length-1);;
}
}
4) Test movie
Selamat mencoba, dan juga yang paling penting... dipahami LOGIKA-nya.. okeeehh..
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.TextEvent;
import flash.events.KeyboardEvent;
var suku,a,b,c:int;
suku_txt.restrict = "0-9";
ok_btn.addEventListener(MouseEvent.CLICK,fibo);
function fibo(e:MouseEvent):void
{
hasil_txt.text = "";
suku = Number(suku_txt.text);
a = 1;
b = 1;
if (suku == 1)
{
hasil_txt.appendText(a.toString());
}
else if (suku == 2)
{
hasil_txt.appendText(a.toString());
hasil_txt.appendText(",");
hasil_txt.appendText(b.toString());
}
else if (suku > 2)
{
hasil_txt.appendText(a.toString());
hasil_txt.appendText(",");
hasil_txt.appendText(b.toString());
hasil_txt.appendText(",");
for (var i:int=3; i<=suku; i++)
{
c = a + b;
a = b;
b = c;
hasil_txt.appendText(c.toString());
hasil_txt.appendText(",");
}
hasil_txt.text = hasil_txt.text.substring(0, hasil_txt.text.length-1);;
}
}
4) Test movie
0 komentar :
Posting Komentar