2014-01-17 54 views
0

我有四個選擇題(包含文本字段的精靈),我最初設置爲不可見。點擊一個按鈕後,我將它們顯示出來,然後我希望它們淡入到alpha 0,只剩下正確的答案。下面的代碼實現了這一點 - 但只有當我在addedToStage處理程序上調用initTweens()時纔會完成此操作。如果我嘗試從按鈕單擊事件調用它 - playClickHandler - Alpha漸變不會發生。任何人都能看到爲什麼補間工作添加到階段,但不響應按鈕單擊

謝謝。

package 
{ 
import com.greensock.TimelineLite; 
import com.greensock.TweenLite; 
import com.greensock.easing.*; 

import flash.display.Bitmap; 
import flash.display.GradientType; 
import flash.display.Graphics; 
import flash.display.Shape; 
import flash.display.Sprite; 
import flash.events.Event; 
import flash.events.MouseEvent; 
import flash.geom.Matrix; 
import flash.net.URLRequest; 
import flash.net.navigateToURL; 
import flash.text.TextField; 
import flash.text.TextFieldAutoSize; 
import flash.text.TextFieldType; 
import flash.text.TextFormat; 

import views.Icons; 

[SWF(width="400", height="350", backgroundColor="0xffffff")] 
public class game extends Sprite 
{ 
//sprites 
private var container:Sprite; 
private var question:Sprite = new Sprite(); 
private var answer1:Sprite = new Sprite(); 
private var answer2:Sprite = new Sprite(); 
private var answer3:Sprite = new Sprite(); 
private var answer4:Sprite = new Sprite(); 
private var explanation:Sprite = new Sprite(); 
private var playButton:Sprite; 

//text fields 
private var txtQuestion:TextField = new TextField(); 
private var txtAnswer1:TextField = new TextField(); 
private var txtAnswer2:TextField = new TextField(); 
private var txtAnswer3:TextField = new TextField(); 
private var txtAnswer4:TextField = new TextField(); 
private var txtExplanation:TextField = new TextField(); 
private var vBuffer:Number = 10; 

//strings for textfields 
private var currentQuestion:String; 
private var currentAnswer1:String; 
private var currentAnswer2:String; 
private var currentAnswer3:String; 
private var currentAnswer4:String; 
private var currentExplanation:String; 

private var questionSets:Array = []; 
private var timeline:TimelineLite = new TimelineLite(); 
private var fadeSpeed:Number = 3; 

private var bmpplayButton:Bitmap; 
private var centeredAnswerPosition:Number; 

//create a keyword which will trigger the presentation of a given questionSet 
private var keyWord:String; 

private var questionObj:Object; 

private var textWidth:Number = 400; 
private var textHeight:Number; 

public function game() 
{ 
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler); 
} 

private function addedToStageHandler(e:Event):void 
{ 
bmpplayButton = new Icons.PlayButtonMedium(); 
loadData("amzn"); 
setUpQuestion(); 
//initTweens(); //this works 
} 

private function playClickHandler(e:MouseEvent):void 
{ 
makeVisible(); 
initTweens(); //code runs, but this doesn't work -- the alpha changes don't happen. 
} 


private function makeVisible():void 
{ 
answer1.visible = true; 
answer2.visible = true; 
answer3.visible = true; 
answer4.visible = true; 
explanation.visible = true; 
} 

private function initTweens():void 
{ 
timeline.insert(TweenLite.to(answer1,fadeSpeed, {autoAlpha:0, delay:4}),0); 
timeline.insert(TweenLite.to(answer3,fadeSpeed, {autoAlpha:0, delay:7}),0); 
timeline.insert(TweenLite.to(answer4,fadeSpeed, {autoAlpha:0, delay:10}),0); 
timeline.insert(TweenLite.to(answer2, 2, {x:100,scaleX:2, scaleY:2, delay: 12}),0); 
timeline.insert(TweenLite.to(explanation, fadeSpeed, {autoAlpha:1, delay:14}),0); 
timeline.append(TweenLite.delayedCall(3, clear)); 
} 



private function setUpQuestion():void 
{ 
container = new Sprite(); 
container.name = "container"; 
container.buttonMode = true; 
container.useHandCursor = true; 
addChild(container); 


txtQuestion.name = "txtQuestion"; 
txtQuestion.width = textWidth; 
txtQuestion.wordWrap = true; 
txtQuestion.multiline = true; 
txtQuestion.type = TextFieldType.DYNAMIC; 
txtQuestion.autoSize = TextFieldAutoSize.LEFT; 
question.addChild(txtQuestion); 

var textFormat:TextFormat = new TextFormat("Arial", 14, 0x000000, false, false, false, "", "", "left", 0, 0, 0, 0); 
txtQuestion.setTextFormat(textFormat); 
txtQuestion.defaultTextFormat = textFormat; 
txtQuestion.text = currentQuestion; 

answer1.y = question.y + question.height + vBuffer; 
txtAnswer1.width = textWidth; 
txtAnswer1.wordWrap = true; 
txtAnswer1.multiline = true; 
txtAnswer1.type = TextFieldType.DYNAMIC; 
txtAnswer1.autoSize = TextFieldAutoSize.LEFT; 
txtAnswer1.setTextFormat(textFormat); 
txtAnswer1.defaultTextFormat = textFormat; 
txtAnswer1.text = currentAnswer1; 
answer1.addChild(txtAnswer1); 
answer1.visible = false; 

answer2.y = answer1.y + answer1.height + vBuffer 
txtAnswer2.width = textWidth; 
txtAnswer2.wordWrap = true; 
txtAnswer2.multiline = true; 
txtAnswer2.type = TextFieldType.DYNAMIC; 
txtAnswer2.autoSize = TextFieldAutoSize.LEFT; 
txtAnswer2.setTextFormat(textFormat); 
txtAnswer2.defaultTextFormat = textFormat; 
txtAnswer2.text = currentAnswer2; 
answer2.addChild(txtAnswer2); 
centeredAnswerPosition = stage.stageWidth/2 - answer2.width/2; 
answer2.visible = false; 

answer3.y = answer2.y + answer2.height + vBuffer; 
txtAnswer3.width = textWidth; 
txtAnswer3.wordWrap = true; 
txtAnswer3.multiline = true; 
txtAnswer3.type = TextFieldType.DYNAMIC; 
txtAnswer3.autoSize = TextFieldAutoSize.LEFT; 
txtAnswer3.setTextFormat(textFormat); 
txtAnswer3.defaultTextFormat = textFormat; 
txtAnswer3.text = currentAnswer3; 
answer3.addChild(txtAnswer3); 
answer3.visible = false; 

answer4.y = answer3.y + answer3.height + vBuffer; 
txtAnswer4.width = textWidth; 
txtAnswer4.wordWrap = true; 
txtAnswer4.multiline = true; 
txtAnswer4.type = TextFieldType.DYNAMIC; 
txtAnswer4.autoSize = TextFieldAutoSize.LEFT; 
txtAnswer4.setTextFormat(textFormat); 
txtAnswer4.defaultTextFormat = textFormat; 
txtAnswer4.text = currentAnswer4; 
answer4.addChild(txtAnswer4); 
answer4.visible = false; 

explanation.y = answer4.y + answer4.height + vBuffer; 
explanation.alpha = 0; //hide it 
txtExplanation.width = textWidth; 
txtExplanation.wordWrap = true; 
txtExplanation.multiline = true; 
txtExplanation.type = TextFieldType.DYNAMIC; 
txtExplanation.autoSize = TextFieldAutoSize.LEFT; 
txtExplanation.y = txtAnswer4.y + txtAnswer1.height + vBuffer; 
txtExplanation.setTextFormat(textFormat); 
txtExplanation.defaultTextFormat = textFormat; 
txtExplanation.text = currentExplanation; 
explanation.addChild(txtExplanation); 
explanation.visible = false; 

playButton = new Sprite(); 
playButton.name = "play"; 
playButton.buttonMode = true; 
playButton.useHandCursor = true; 
playButton.addChild(bmpplayButton); 
playButton.x = stage.stageWidth/2 - playButton.width/2; 
playButton.y = explanation.y + explanation.height + 5*vBuffer; 
playButton.addEventListener(MouseEvent.CLICK, playClickHandler); 

container.addChild(question); 
container.addChild(answer1); 
container.addChild(answer2); 
container.addChild(answer3); 
container.addChild(answer4); 
container.addChild(explanation); 
container.addChild(playButton); 
} 


private function loadData(questionSet:String):void 
{ 
switch(questionSet) 
{ 
case "cap": 
currentQuestion = "What is the term for the number of a company's shares currently available for trading?"; 
currentAnswer1 = "The Cap" ; 
currentAnswer2 = "The Float"; 
currentAnswer3 = "The Book"; 
currentAnswer4 = "The Major Leagues"; 
currentExplanation = "If a large percentage of the float is 'short' then it can set up a short squeeze."; 
break; 
case "amzn": 
currentQuestion = "How much has Amazon gone up in the last 10 years?"; 
currentAnswer1 = "100%" ; 
currentAnswer2 = "10000%"; 
currentAnswer3 = "1000%"; 
currentAnswer4 = "400%"; 
currentExplanation = "Yes, it's gone up a hundredfold. Buy and hold!"; 
break; 
} 

} 

private function clear():void 
{ 
question.visible = false; 
answer1.visible = false; 
answer2.visible = false; 
answer3.visible = false; 
answer4.visible = false; 
explanation.visible = false; 
} 
} 
} 

回答

-1

我想出了一個解決方案。

而不是在聲明的時間實例化的:

private var timeline:TimelineLite = new TimelineLite(); 

我只是做了

private var timeline:TimelineLite; 

然後我實例化它在initTweens()函數。那就是訣竅。出於某種原因,時間軸是自動運行的,僅僅是因爲它在聲明時被實例化了,無論它在哪裏被調用。

+0

這還不意義。正如我在回答中所述,這不是正確的解決方案。現在您正在使用autoAlpha但你應該已經收到反正錯誤,因爲您沒有導入插件:'進口com.greensock.plugins.AutoAlphaPlugin;' 不幸的是你這個標註正確,這不是真正的答案的問題是你這是與autoAlpha插件處理可視性的控制。 –

0

的問題是與你的makeVisible()playClickHandler(e:MouseEvent)調用。如果您評論或刪除該行,它應該以與您的ADDED_TO_STAGE事件相同的方式工作。

其原因是在於AutoAlpha插件控制你的知名度,所以你不應該有一個需要調用makeVisible()

來源:http://www.greensock.com/as/docs/tween/com/greensock/plugins/AutoAlphaPlugin.html

+0

嗨,感謝您的評論。我不知道你是否測試過 - 我還沒有。但我確實找到了一個解決方案,我將其作爲答案發布。 – David

+0

我沒有測試它,這是修復 –

+0

我繼續測試,如果我只是一個改變我得到一個錯誤 - 物業autoAlpha不是flash.display.Sprite發現沒有默認值。 – David

相關問題