ActionScript 3.0:通过单击按钮移动到另一个场景

ActionScript 3.0: Moving to another scene by clicking a button

我已经创建了我的小项目的第一个场景。现在我想转到应用程序的第二个场景。第一个叫做 startScene,第二个叫做 playScene。下面是链接到第一个场景的class的代码:

package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.events.MouseEvent;



public class dragMain extends MovieClip {


    public function dragMain() {
        setWelcomeMessage();
        drawArrow();
        createStartButton();
    }
    // funzione per settare il messaggio di benvenuto
    function setWelcomeMessage(): void {
        // TextField contenete il messaggio di benvenuto
        var welcomeMessage: TextField = new TextField();
        // formattazione per il messaggio di benvenuto
        var welcomeFormat: TextFormat = new TextFormat("Verdana", 40, 0xFF0000);
        // imposto il testo da visualizzare
        welcomeMessage.text = "Welcome, click the button to start playing";
        // 
        welcomeMessage.autoSize = TextFieldAutoSize.LEFT;
        // cerco di centrare il testo ad occhio nella schermata         
        welcomeMessage.x = 500;
        // applico la formattazione al testo
        welcomeMessage.setTextFormat(welcomeFormat);
        // aggiungo il testo allo stage         
        addChild(welcomeMessage);
    }
    // funzione che disegnera' la freccia
    function drawArrow(): void {

        // Sprite che conterra' la freccia disegnata
        var arrow: Sprite = new Sprite();

        arrow.graphics.beginFill(0XFF0000);
        arrow.graphics.moveTo(800, 100); //500,500 // 200,200
        arrow.graphics.lineTo(1000, 100); //700,500 // 400,200
        arrow.graphics.lineTo(1000, 550); //700,950 // 400,650
        arrow.graphics.lineTo(1100, 550); //800,950 // 500,650
        arrow.graphics.lineTo(900, 700); //600,1100// 300,800
        arrow.graphics.lineTo(700, 550); //400,950 // 100,650
        arrow.graphics.lineTo(800, 550); //500,950 // 200,650
        arrow.graphics.lineTo(800, 100); //500,500 // 200,200
        addChild(arrow);
    }

    // funzione per creare il bottone
    function createStartButton(): void {
        var button: Sprite = new Sprite();

        button.graphics.beginFill(0xFF0000);
        button.graphics.moveTo(700, 800);
        button.graphics.lineTo(1100, 800);
        button.graphics.lineTo(1100, 1000);
        button.graphics.lineTo(700, 1000);
        button.graphics.lineTo(700, 800);
        var clickMeMessage: TextField = new TextField();
        clickMeMessage.x = 855;
        clickMeMessage.y = 865;
        var welcomeFormat: TextFormat = new TextFormat("Verdana", 40, 0x000000);
        // imposto il testo da visualizzare
        clickMeMessage.text = "click me!";
        // 
        clickMeMessage.autoSize = TextFieldAutoSize.CENTER;
        // applico la formattazione al testo
        clickMeMessage.setTextFormat(welcomeFormat);

        addChild(button);
        addChild(clickMeMessage);

        button.addEventListener(MouseEvent.CLICK, onClick);

    }
    function onClick(evt: MouseEvent): void {
        gotoAndPlay(1, "playWindow");
    }
}

}

当我单击通过代码创建的按钮时,我总是在 startScene 上,无法移动到 playScene。我在 startScene 上有一个框架,在 playScene 上有一个框架。有什么问题,我该如何解决?谢谢!

试试 gotoAndStop(1, "playWindow");而不是 gotoAndPlay()。我怀疑您的 playScene 的第 1 帧上没有 stop() ,因此 Flash 继续经过该场景。

要事第一。

您已完成 gotoAndPlay,因此它将在第 1 帧的所有场景之间不断迭代

gotoAndStop(1,"playScene");

如果您不想要在开始场景中添加的所有东西,您应该删除它们或将它们的可见性设置为 false,因为 addchild 会将对象添加到舞台

这就是我did.I使以下变量成为全局变量

var welcomeMessage: TextField;
var button: Sprite;
var clickMeMessage: TextField;
var arrow: Sprite; 

并对onclick函数做了一点改动

function onClick(evt: MouseEvent): void {
        button.visible = false;
        clickMeMessage.visible = false;
        arrow.visible = false;
        welcomeMessage.visible = false;
        gotoAndStop(1, "playScene");

    }

好吧,我认为这应该可行