Search code examples
actionscript-3buttonframescene

ActionScript 3.0: Moving to another scene by clicking a button


I have created the first scene of my small project. Now I want to move to the second scene of the app. The start one is called startScene, the second one playScene. Here is the code of the class linked to the first scene:

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");
    }
}

}

When I click the button that I created through code I'm always on the startScene and I can't move to the playScene. I have got a frame on startScene and a frame on playScene. What's the problem and how can I solve it? Thank you!


Solution

  • First things first.

    You have done gotoAndPlay so it will keep iterating between all the scenes on frame 1

    gotoAndStop(1,"playScene");
    

    If you don't want all the stuffs you have added on start scene you should remove them or set their visibility to false because addchild adds the object to the stage

    This is what i did.I made the following variables global

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

    and changed the onclick function a bit

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

    well i think this should work