2012-12-13 86 views
0

這裏是AS3:驗證文本字段被壓在AS3

import flash.events.MouseEvent; 
import flash.net.URLLoader; 
import flash.net.URLRequest; 
import flash.net.URLVariables; 
import flash.net.URLLoaderDataFormat; 
import flash.net.URLRequestMethod; 
import flash.events.Event; 
import flash.display.MovieClip; 


wheeler.guide.visible = false; 
center.addEventListener(MouseEvent.CLICK, rotateHorizontal); 

Stop_mc.addEventListener(MouseEvent.CLICK, stopspin); 

function rotateHorizontal(e:MouseEvent):void 
{ 
    stage.addEventListener(Event.ENTER_FRAME,spinner); 

} 

var mcbtn:MovieClip; 
var prizes:Array = new Array(0,180,0,40,30,0,80,75,0,10,0,25,120,0); 
var prizewon:int; 

for (var i:int=0; i<14; ++i) 
{ 
    mcbtn = wheeler["btn" + (i + 1)]; 

    if (! mcbtn || ! prizes[i]) 
    { 
     continue; 
    } 

    mcbtn.txt.text = "Earn " + String(prizes[i]) + " messages"; 
} 

function spinner(e:Event) 
{ 
    wheeler.rotationZ += 15; 
    pointer_txt.text = " "; 

    if (wheeler.rotationZ > 360) 
    { 
     wheeler.rotationZ = wheeler.rotationZ - 360; 
    } 
    //trace(wheeler.rotationZ); 
} 

function completed(event:Event):void 
{ 
    if (event.target.data.success == "true") 
    { 
     trace("Completed"); 
    } 
} 

function stopspin(e:Event):void 
{ 
    var txt:String; 
    stage.removeEventListener(Event.ENTER_FRAME,spinner); 
    prizewon = -1; 
    prizewon = (14 - Math.floor(wheeler.rotationZ/(360/14))); 

    if (prizes[prizewon - 1]) 
    { 
     txt = "You have earned " + prizes[prizewon - 1] + " Messages"; 
    } 
    else 
    { 
     txt = "You have earned no Messages"; 
    } 

    pointer_txt.text = txt; 

    var phpVar:URLVariables = new URLVariables(); 
    phpVar.code_txt = code_txt.text; 
    phpVar.name_txt = name_txt.text; 
    phpVar.phone = phone_txt.text; 
    phpVar.Result = pointer_txt.text; 
    var phpLoader:URLLoader = new URLLoader(phpFileRequest); 
    var phpFileRequest:URLRequest = new 

URLRequest("http://localhost/spin_webapp/deposit.php"); 
    phpFileRequest.method = URLRequestMethod.POST; 
    phpFileRequest.data = phpVar; 
    phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES; 

    phpLoader.addEventListener(Event.COMPLETE,completed); 
    phpLoader.load(phpFileRequest); 
} 
+0

我建議你加在你的代碼 –

+0

一些inline- \評論我要的碼,手機將TextInput,在某些按下中央動畫片段按鈕之前先填充名稱。 –

+0

嘿,我簡化了標題..你可以恢復回來,如果你喜歡.. –

回答

0

您將需要創建一個事件來處理的文本框的按鍵。

無論何時發生按鍵事件,按鍵事件處理程序函數都會檢查文本字段是否爲NULL或執行其他驗證。如果一切正常,請設置一個標誌,如name_txt__valid=TRUE

然後,如果name_txt__valid, phone_txt__validpointer_txt__valid都是TRUE然後使movieclip按鈕可見,否則隱藏它。

很明顯,用戶必須被告知textfields在/有效,否則他們不會意識到爲什麼按鈕已經消失。


UPDATE:

對不起,我不能測試我下面的代碼的權利,但以下應更好地闡述什麼,我認爲需要做的事情..

語法創建初始化期間的監聽器: 例如。

name_txt.addEventListener(Event.CHANGE,name_txt_ChangeListener); 
//Also lets initialize our flags... 
name_txt__valid=false; 
phone_txt__valid=false; 
pointer_txt__valid=false; 
center.enabled = false; 

參考:AddchangeeventtoTextField

然後只需添加驗證邏輯:

private function name_txt_ChangeListener (e:Event):void { 
... 
    trace("Text field event triggered"); 
    //Check if text is null or invalid 
    //If valid then set a flag like name_txt__valid=TRUE 
    if (name_txt.text == "") { 
    name_txt__valid=false;  
    } else { 
    name_txt__valid=true;  
    } 

    //Then make a function call to decide whether to show/hide the center button. 
    showHideCenterButton(); 
... 
} 

function showHideCenterButton(){ 
... 
    //If name_txt__valid, phone_txt__valid and pointer_txt__valid, 
    // all are TRUE then enable the button. Else disable it like center.enabled = false; 
    if((name_txt__valid==true) && 
     (phone_txt__valid==true) && 
     (pointer_txt__valid==true) 
    ) { 
     center.enabled = true; 
    } else { 
     center.enabled = false; 
    } 
... 
} 
+0

你怎麼能這樣做發生 –

+0

@BbosaFrank我更新了我的帖子.. –

+0

也看到這個:http://stackoverflow.com/questions/4756138/as3-event-listeners-when-data-is-change –