플래시로 외부 파일 데이터를 가져오기 위한 웹서버와 통신하기 두번째 샘플 소스 코드입니다. 플래시 액션스크립트 초급자를 위해 학습용으로 부담없이 테스트할 수 있도록 만들었으므로 차근 차근 살펴 보시면 이해에 도움이 될 것이라 생각됩니다. 아래 예제 처럼 LoadVars 객체를 이용해서 통신을 하는 것이 먼저 소개해드렸던 XML 객체를 이용하는 것 보다는 좀더 손쉽게 사용될 수 있습니다. 그러나 전달해야할 데이터가 복잡한 구조를 가질 경우 XML 객체를 이용하는 것 보다 혼란스러울 수 있으며 눈으로 손쉽게 데이터를 해석하기 어려울 수 있습니다. 아래 제공하는 샘플 소스 코드를 필요에 맞게 수정하고 개선해보면 어떻게 사용하는 것인지 이해하기 쉽습니다. 잘 익혀두었다가 사용자의 데이터를 등록하고 갱신하는 응용 프로그램 모양의 플래시를 만드는데 활용해보세요.
[product.txt]
title=system&attribute=name|price|total|date&product=desktop|380000|10|2008-10-01,monitor|270000|5|2008-10-04,printer|160000|2|2008-09-28,notebook|870000|15|2008-10-06,mouse|12000|20|2008-09-20
[product_txt.fla]
System.useCodepage = true;
var product_vars = new LoadVars();
product_vars.onLoad = function(success) {
if (success) {
var attribute_str = this.attribute;
var product_str = this.product;
var attribute_array = this.attribute.split('|');
var tmp_array = product_str.split(',');
var product_array = [tmp_array[0].split('|'), tmp_array[1].split('|'), tmp_array[2].split('|'), tmp_array[3].split('|'), tmp_array[4].split('|')];
tf_textarea.text += '[text file tester]\n';
tf_textarea.text += '01: ' + this.title + '\n';
tf_textarea.text += '02: ' + this.attribute + '\n';
tf_textarea.text += '03: ' + this.product + '\n';
tf_textarea.text += '\n';
tf_textarea.text += '04: ' + this.attribute.split('|')[0] + '\n';
tf_textarea.text += '05: ' + this.attribute.split('|')[1] + '\n';
tf_textarea.text += '06: ' + this.attribute.split('|')[2] + '\n';
tf_textarea.text += '07: ' + this.attribute.split('|')[3] + '\n';
tf_textarea.text += '\n';
tf_textarea.text += '08: ' + this.product.split(',')[0] + '\n';
tf_textarea.text += '09: ' + this.product.split(',')[1] + '\n';
tf_textarea.text += '10: ' + this.product.split(',')[2] + '\n';
tf_textarea.text += '11: ' + this.product.split(',')[3] + '\n';
tf_textarea.text += '12: ' + this.product.split(',')[4] + '\n';
tf_textarea.text += '\n';
tf_textarea.text += '13: ' + this.product.split(',')[0].split('|')[0] + '\n';
tf_textarea.text += '14: ' + this.product.split(',')[0].split('|')[1] + '\n';
tf_textarea.text += '15: ' + this.product.split(',')[0].split('|')[2] + '\n';
tf_textarea.text += '16: ' + this.product.split(',')[0].split('|')[3] + '\n';
tf_textarea.text += '\n';
tf_textarea.text += '17: ' + tmp_array[1].split('|')[0] + '\n';
tf_textarea.text += '18: ' + tmp_array[1].split('|')[1] + '\n';
tf_textarea.text += '19: ' + product_array[1][2] + '\n';
tf_textarea.text += '20: ' + product_array[1][3] + '\n';
tf_textarea.text += '\n';
textarea += '21: ' + product_array[2][0] + '\n';
textarea += '22: ' + product_array[2][1] + '\n';
textarea += '\n';
//trace(this.title);
} else {
//trace('error');
}
}
product_vars.load("product.txt");
[product_txt.swf]
[product_txt.html]
<embed src="http://www.hompydesign.com/flash/sample/product_txt.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="520" height="420" base="http://www.hompydesign.com/flash/sample/"></embed>
웹프로그래머의 홈페이지 정보 블로그 http://hompy.info
[product.txt]
title=system&attribute=name|price|total|date&product=desktop|380000|10|2008-10-01,monitor|270000|5|2008-10-04,printer|160000|2|2008-09-28,notebook|870000|15|2008-10-06,mouse|12000|20|2008-09-20
[product_txt.fla]
System.useCodepage = true;
var product_vars = new LoadVars();
product_vars.onLoad = function(success) {
if (success) {
var attribute_str = this.attribute;
var product_str = this.product;
var attribute_array = this.attribute.split('|');
var tmp_array = product_str.split(',');
var product_array = [tmp_array[0].split('|'), tmp_array[1].split('|'), tmp_array[2].split('|'), tmp_array[3].split('|'), tmp_array[4].split('|')];
tf_textarea.text += '[text file tester]\n';
tf_textarea.text += '01: ' + this.title + '\n';
tf_textarea.text += '02: ' + this.attribute + '\n';
tf_textarea.text += '03: ' + this.product + '\n';
tf_textarea.text += '\n';
tf_textarea.text += '04: ' + this.attribute.split('|')[0] + '\n';
tf_textarea.text += '05: ' + this.attribute.split('|')[1] + '\n';
tf_textarea.text += '06: ' + this.attribute.split('|')[2] + '\n';
tf_textarea.text += '07: ' + this.attribute.split('|')[3] + '\n';
tf_textarea.text += '\n';
tf_textarea.text += '08: ' + this.product.split(',')[0] + '\n';
tf_textarea.text += '09: ' + this.product.split(',')[1] + '\n';
tf_textarea.text += '10: ' + this.product.split(',')[2] + '\n';
tf_textarea.text += '11: ' + this.product.split(',')[3] + '\n';
tf_textarea.text += '12: ' + this.product.split(',')[4] + '\n';
tf_textarea.text += '\n';
tf_textarea.text += '13: ' + this.product.split(',')[0].split('|')[0] + '\n';
tf_textarea.text += '14: ' + this.product.split(',')[0].split('|')[1] + '\n';
tf_textarea.text += '15: ' + this.product.split(',')[0].split('|')[2] + '\n';
tf_textarea.text += '16: ' + this.product.split(',')[0].split('|')[3] + '\n';
tf_textarea.text += '\n';
tf_textarea.text += '17: ' + tmp_array[1].split('|')[0] + '\n';
tf_textarea.text += '18: ' + tmp_array[1].split('|')[1] + '\n';
tf_textarea.text += '19: ' + product_array[1][2] + '\n';
tf_textarea.text += '20: ' + product_array[1][3] + '\n';
tf_textarea.text += '\n';
textarea += '21: ' + product_array[2][0] + '\n';
textarea += '22: ' + product_array[2][1] + '\n';
textarea += '\n';
//trace(this.title);
} else {
//trace('error');
}
}
product_vars.load("product.txt");
[product_txt.swf]
[product_txt.html]
<embed src="http://www.hompydesign.com/flash/sample/product_txt.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="520" height="420" base="http://www.hompydesign.com/flash/sample/"></embed>
웹프로그래머의 홈페이지 정보 블로그 http://hompy.info
product.txt
product_txt.fla