2016-06-28 96 views
1

我已经创建了一个Flash简单gotoAndStop故事书,但它下面的分支剧情。骑士进入了一座城堡,并且有一扇门。进入大门后,他面对3个门和每扇门,导致另一系列房间。ActionScript 3的gotoAndStop的分支剧情

但是,最后会是一样的。它似乎是一个简单的“gotoAndStop”代码,但在故事的结尾,骑士将获得某种价格(由框架表示),具体取决于他输入的是哪个门。我的问题是,我将如何知道用户点击了哪些按钮,以便我可以“将它停止并停止”到某个框架?用户的最终目的地或奖励框架取决于他点击了哪些按钮。

这是我的样本gotoAndStop代码,我的故事书使用(我在所有帧中使用类似的代码,只是改变了按钮实例名,函数名和帧标签):

nextbutton1.addEventListener(MouseEvent.CLICK, proceedDoor1); 
function proceedDoor1 (e:Event){ 
    gotoAndStop("door1") 
} 

我完整的新手所以请忍受我。谢谢!

+0

将元数据存储在声明为“public var”的某个对象中,以便跨帧保留,然后一旦显示奖励时查询存储的内容并选择显示哪个帧。 – Vesper

回答

1

这里有一个小样机我做(道歉,如果这是一个有点乱)。它要求从1

每个门开始将有一个MouseEvent.CLICK事件侦听器将带你到所需的帧在时间轴上5个空帧。在这种情况下,我将每个门的引导线直接送到奖架上。但是,你总是可以让每个门导致另一组的门,然后让这些门导致的奖品等等,等等

注意,变量只是用于创建按钮,并且不需要。您可以将点击事件侦听器放置在您自己的动画片段上,并获得相同的结果。 (你只需要在代码调整实例名称)

希望这有助于!

Frame1中的代码:

import flash.display.Sprite; 
import flash.text.TextField; 
import flash.events.MouseEvent; 
import flash.events.Event; 

stop(); 

var rec:Sprite = new Sprite(); 
rec.graphics.beginFill(0x00FF00, 1); 
rec.graphics.drawRect(-50, -50, 100, 100); 
addChild(rec); 
rec.x=stage.stageWidth/2; 
rec.y=stage.stageHeight/2; 

var text1:TextField=new TextField(); 
text1.wordWrap=true; 
text1.text="ENTER THE CASTLE"; 
addChild(text1); 
text1.x=rec.x; 
text1.y=rec.y; 

rec.addEventListener(MouseEvent.CLICK,recC); 

function recC (e:Event):void{ 
    //Remove Child not needed if you symbols are placed manually on stage 
    removeChild(rec); 
    removeChild(text1); 
    rec.removeEventListener(MouseEvent.CLICK,recC) 
    nextFrame(); 
} 

式2代码:

import flash.display.Sprite; 
import flash.text.TextField; 
import flash.events.MouseEvent; 
import flash.events.Event; 

stop(); 


var rec2: Sprite = new Sprite(); 
rec2.graphics.beginFill(0x0066CC, 1); 
rec2.graphics.drawRect(-50, -50, 100, 100); 
addChild(rec2); 
rec2.x = stage.stageWidth/5; 
rec2.y = stage.stageHeight/2; 

var rec3: Sprite = new Sprite(); 
rec3.graphics.beginFill(0x66FF99, 1); 
rec3.graphics.drawRect(-50, -50, 100, 100); 
addChild(rec3); 
rec3.x = stage.stageWidth/2; 
rec3.y = stage.stageHeight/2; 


var rec4: Sprite = new Sprite(); 
rec4.graphics.beginFill(0xFF3333, 1); 
rec4.graphics.drawRect(-50, -50, 100, 100); 
addChild(rec4); 
rec4.x = stage.stageWidth/1.2; 
rec4.y = stage.stageHeight/2; 

var text2: TextField = new TextField(); 
addChild(text2); 
text2.text = "Pick a Door Knight!"; 
text2.x = stage.stageWidth/2; 
text2.y = stage.stageHeight/4; 


//Each door has an event listener for a click 
//Once clicked, the door will take you to the appropriate ending 

rec2.addEventListener(MouseEvent.CLICK, rec2C); 

function rec2C(e: Event): void { 
    removeChild(rec2); 
    removeChild(rec3); 
    removeChild(rec4); 
    removeChild(text2); 
    rec2.removeEventListener(MouseEvent.CLICK, rec2C) 
    rec3.removeEventListener(MouseEvent.CLICK, rec3C) 
    rec4.removeEventListener(MouseEvent.CLICK, rec4C) 
    gotoAndStop(3)//This is the frame for Blue 
} 

rec3.addEventListener(MouseEvent.CLICK, rec3C); 

function rec3C(e: Event): void { 
    removeChild(rec2); 
    removeChild(rec3); 
    removeChild(rec4); 
    removeChild(text2); 
    rec2.removeEventListener(MouseEvent.CLICK, rec2C) 
    rec3.removeEventListener(MouseEvent.CLICK, rec3C) 
    rec4.removeEventListener(MouseEvent.CLICK, rec4C) 
    gotoAndStop(4)//This is the frame for Green 
} 

rec4.addEventListener(MouseEvent.CLICK, rec4C); 

function rec4C(e: Event): void { 
    removeChild(rec2); 
    removeChild(rec3); 
    removeChild(rec4); 
    removeChild(text2); 
    rec2.removeEventListener(MouseEvent.CLICK, rec2C) 
    rec3.removeEventListener(MouseEvent.CLICK, rec3C) 
    rec4.removeEventListener(MouseEvent.CLICK, rec4C) 
    gotoAndStop(5)//This is the frame for Red 
} 

3帧的代码:

var text3: TextField = new TextField(); 
addChild(text3); 
text3.wordWrap=true; 
text3.text = "You won the blue prize!"; 
text3.x = stage.stageWidth/2; 
text3.y = stage.stageHeight/2.7; 

addChild(rec2); 
rec2.x=stage.stageWidth/2; 
rec2.y=stage.stageWidth/2; 

帧4的代码:

var text4: TextField = new TextField(); 
addChild(text4); 
text4.wordWrap=true; 
text4.text = "You won the green prize!"; 
text4.x = stage.stageWidth/2; 
text4.y = stage.stageHeight/2.7; 

addChild(rec3); 
rec3.x=stage.stageWidth/2; 
rec3.y=stage.stageWidth/2; 

第5帧代码:

var text5: TextField = new TextField(); 
addChild(text5); 
text5.wordWrap=true; 
text5.text = "You won the red prize!"; 
text5.x = stage.stageWidth/2; 
text5.y = stage.stageHeight/2.7; 

addChild(rec4); 
rec4.x=stage.stageWidth/2; 
rec4.y=stage.stageWidth/2; 
+0

非常感谢!它帮助! :) – Jojo

0

我不知道为什么要使用多帧这可能不是最好的办法。 您只能使用一个框架,当用户点击一个按钮时添加和删除项目...

如果您使用多个框架,您可能只使用一个单击处理程序,并通过检查哪个按钮点击处理程序。

如果你想使用多帧:

关于第一个框架,有一个名为door_1一个按钮(这是DoorButton的一个实例) 让我们想象一下,你有一个扩展的SimpleButton一个DoorButton类。 在DoorButton类中,您可以添加方法来声明“id”和“targetFrame”属性...

DoorButton类:

package { 

import flash.display.SimpleButton; 

    public class DoorButton extends SimpleButton { 

     private var _id; 
     private var _targetFrame; 
     public static const FRAME1:String = "frame1"; 
     public static const FRAME2:String = "frame2"; 
     public static const FRAME3:String = "frame3"; 
     public static const FRAME4:String = "frame4"; 
     public static const FRAME5:String = "frame5"; 
     // ...  
     public function DoorButton(){ 
      super(); 
     } 
     public function set id(id:uint):void { 
      _id = id; 
     } 
     public function get id():uint { 
      return _id; 
     } 
     public function set targetFrame(target:String):void { 
      _targetFrame = target; 
     } 
     public function get targetFrame():String { 
      return _targetFrame; 
     } 

    } 
} 

第1帧代码:

import DoorButton; 
import flash.events.MouseEvent; 

var door1:DoorButton = door_1; 
door1.id = 1; 
door1.targetFrame = DoorButton.FRAME2; 
door1.addEventListener(MouseEvent.CLICK,proceed); 

function proceed(e:MouseEvent):void{ 
    var b:DoorButton = e.target as DoorButton; 
    switch (b.id){ 
     case 1 : 
      trace("should go in : " + b.targetFrame); 
      this.gotoAndStop(2); 
      // call some function here 
      break; 
     case 2 : 
      trace("should go in : " + b.targetFrame); 
      // call some function here 
      break; 
     case 3 : 
      trace("should go in : " + b.targetFrame); 
      // call some function here 
      break; 
     case 4 : 
      trace("should go in : " + b.targetFrame); 
      // call some function here 
      break; 
    } 
} 
stop() 

关于你的第二帧,你有DoorButton的树实例命名为: door_2,door_3,door_4

代码在第2帧上:

var door2:DoorButton = door_2; 
var door3:DoorButton = door_3; 
var door4:DoorButton = door_4; 

door2.id = 2; 
door2.targetFrame = DoorButton.FRAME3; 
door2.addEventListener(MouseEvent.CLICK,proceed); 

door3.id = 3; 
door3.targetFrame = DoorButton.FRAME4; 
door3.addEventListener(MouseEvent.CLICK,proceed); 

door4.id = 4; 
door4.targetFrame = DoorButton.FRAME5; 
door4.addEventListener(MouseEvent.CLICK,proceed); 

然后,您可以为每个帧执行此操作,只需调用“proceed”方法即可达到您想要的效果。 但我建议你避免多个帧反正..

这是真正的基础,你将可以添加一些语句,以避免重新初始化按钮,如果你打算回来的时间表和重播或让用户改变主意,回来打开另一扇门... 我只希望这可以帮助你。

尝试一下,然后随意问。

此致敬礼。

+0

编码在多个帧是非常难以处理纯AS3和OOP编码!... 我只是尝试这个代码,但如果你想改变的东西,你必须隐藏/显示按钮,你会得到一个很多工作! 所有的实例都必须出现在时间轴上,或者每次要重放应用程序时都必须重写代码... 考虑将每个实例保留在第一帧上,并确定忘记基于时间轴的应用程序! 这对我来说很有意思,因为我从不使用多个关键帧。 谢谢你的问题,所以;) – tatactic

+0

是的!感谢您的见解!事实上,很难在多个框架中编码,但它对我有用(尽管这不是最好的方式)。我试图在第一帧编码,但错误仍然弹出。所以基于时间轴的编码对我有效:D – Jojo

+0

当然,但你应该避免它;) – tatactic