jeudi 24 décembre 2015

actionscript Storing a value after a button is clicked

I don't program a lot. Sometimes I program a bit of Actionscript 3. Now I wanted to create an flash enviroment where people can click on buttons and play a video. I think I almost figured it out with the following script:

public class main extends MovieClip 
{
    private var drukknop1:knopje;
    private var drukknop2:knopje;
    private var drukknop3:knopje;
    private var drukknop4:knopje;
    private var drukknop5:knopje;
    private var drukknop6:knopje;
    private var drukknop7:knopje;
    private var drukknop8:knopje;
    private var drukknop9:knopje;
    private var drukknop10:knopje;
    private var videoclip:FLVPlayback = new FLVPlayback();
    private var buttonClicked:Number = 0;

    public function main() 
    {
        // plaatsen van buttons on stage
        drukknop1 = new knopje();
        drukknop1.name = "inst_knop1";
        drukknop1.x = 50;
        drukknop1.y = 70;
        addChild(drukknop1);
        drukknop1.buttonMode = true;
        drukknop1.addEventListener(MouseEvent.MOUSE_UP, _flv_component);
        drukknop1.addEventListener(MouseEvent.MOUSE_DOWN, ButtonClicked);

        drukknop2 = new knopje();
        drukknop2.name = "inst_knop2";
        drukknop2.x = 150;
        drukknop2.y = 70;
        addChild(drukknop2);
        drukknop2.buttonMode = true;
        drukknop2.addEventListener(MouseEvent.MOUSE_UP, _flv_component);
        drukknop2.addEventListener(MouseEvent.MOUSE_DOWN, ButtonClicked);

        drukknop3 = new knopje();
        drukknop3.name = "inst_knop3";
        drukknop3.x = 250;
        drukknop3.y = 70;
        addChild(drukknop3);
        drukknop3.buttonMode = true;
        drukknop3.addEventListener(MouseEvent.MOUSE_UP, _flv_component);
        drukknop3.addEventListener(MouseEvent.MOUSE_DOWN, ButtonClicked);

        drukknop4 = new knopje();
        drukknop4.name = "inst_knop4";
        drukknop4.x = 350;
        drukknop4.y = 70;
        addChild(drukknop4);
        drukknop4.buttonMode = true;
        drukknop4.addEventListener(MouseEvent.MOUSE_UP, _flv_component);
        drukknop4.addEventListener(MouseEvent.MOUSE_DOWN, ButtonClicked);

        drukknop5 = new knopje();
        drukknop5.name = "inst_knop5";
        drukknop5.x = 450;
        drukknop5.y = 70;
        addChild(drukknop5);
        drukknop5.buttonMode = true;
        drukknop5.addEventListener(MouseEvent.MOUSE_UP, _flv_component);
        drukknop5.addEventListener(MouseEvent.MOUSE_DOWN, ButtonClicked);

        drukknop6 = new knopje();
        drukknop6.name = "inst_knop6";
        drukknop6.x = 550;
        drukknop6.y = 70;
        addChild(drukknop6);
        drukknop6.buttonMode = true;
        drukknop6.addEventListener(MouseEvent.MOUSE_UP, _flv_component);
        drukknop6.addEventListener(MouseEvent.MOUSE_DOWN, ButtonClicked);

        drukknop7 = new knopje();
        drukknop7.name = "inst_knop7";
        drukknop7.x = 650;
        drukknop7.y = 70;
        addChild(drukknop7);
        drukknop7.buttonMode = true;
        drukknop7.addEventListener(MouseEvent.MOUSE_UP, _flv_component);
        drukknop7.addEventListener(MouseEvent.MOUSE_DOWN, ButtonClicked);

        drukknop8 = new knopje();
        drukknop8.name = "inst_knop8";
        drukknop8.x = 750;
        drukknop8.y = 70;
        addChild(drukknop8);
        drukknop8.buttonMode = true;
        drukknop8.addEventListener(MouseEvent.MOUSE_UP, _flv_component);
        drukknop8.addEventListener(MouseEvent.MOUSE_DOWN, ButtonClicked);

        drukknop9 = new knopje();
        drukknop9.name = "inst_knop9";
        drukknop9.x = 850;
        drukknop9.y = 70;
        addChild(drukknop9);
        drukknop9.buttonMode = true;
        drukknop9.addEventListener(MouseEvent.MOUSE_UP, _flv_component);
        drukknop9.addEventListener(MouseEvent.MOUSE_DOWN, ButtonClicked);

        drukknop10 = new knopje();
        drukknop10.name = "inst_knop10";
        drukknop10.x = 950;
        drukknop10.y = 70;
        addChild(drukknop10);
        drukknop10.buttonMode = true;
        drukknop10.addEventListener(MouseEvent.MOUSE_UP, _flv_component);
        drukknop10.addEventListener(MouseEvent.MOUSE_DOWN, ButtonClicked);

    }
    private function _flv_component(event:MouseEvent):void
    {
        //plaatsen van de video player door drukken op de knop 
        drukknop1.buttonMode = false;
        drukknop1.removeEventListener(MouseEvent.MOUSE_UP, _flv_component);

        videoclip.x = (stage.stageWidth/2)-(videoclip.width/2);
        videoclip.y = (stage.stageHeight/2)-(videoclip.height/2);
        videoclip.width = 640;
        videoclip.height = 400;
        videoclip.autoPlay = true;
        videoclip.skin = "MinimaSilverPlayBackSeekMute.swf";

        if(buttonClicked ==1)
        {videoclip.load("Functieomschrijvingscene.flv");}
        else if(buttonClicked ==2)
        {videoclip.load("2.flv");}
        else if(buttonClicked ==3)
        {videoclip.load("3.flv");}
        else if(buttonClicked ==4)
        {videoclip.load("4.flv");}
        else if(buttonClicked ==5)
        {videoclip.load("5.flv");}
        else if(buttonClicked ==6)
        {videoclip.load("6.flv");}
        else if(buttonClicked ==7)
        {videoclip.load("7.flv");}
        else if(buttonClicked ==8)
        {videoclip.load("8.flv");}
        else if(buttonClicked ==9)
        {videoclip.load("9.flv");}
        else if(buttonClicked ==10)
        {videoclip.load("10.flv");}

        videoclip.play();
        addChild(videoclip);
        videoclip.addEventListener(Event.COMPLETE,videoComplete);
    }
    function videoComplete(event:Event):void
    {
        //Verwijderen van FLVPlayback component
        trace ("video is klaar");
        removeChild(videoclip);
        buttonClicked = 0;
        trace (buttonClicked);

    }
    function ButtonClicked(Event:MouseEvent):void
    {
        trace ("ButtonClicked functie werkt.");
        trace (buttonClicked);

        // het opslaan van een nummer van een button
        if(event.target.name == drukknop1)
        {buttonClicked = 1;}
        else if(event.target.name == drukknop2)
        {buttonClicked = 2;}
        else if(event.target.name == drukknop3)
        {buttonClicked = 3;}
        else if(event.target.name == drukknop4)
        {buttonClicked = 4;}
        else if(event.target.name == drukknop5)
        {buttonClicked = 5;}
        else if(event.target.name == drukknop6)
        {buttonClicked = 6;}
        else if(event.target.name == drukknop7)
        {buttonClicked = 7;}
        else if(event.target.name == drukknop8)
        {buttonClicked = 8;}
        else if(event.target.name == drukknop9)
        {buttonClicked = 9;}
        else if(event.target.name == "inst_knop10")
        {buttonClicked = 10;}
    }
}

}

In the function "ButtonClicked, I want to check which button is clicked. After that I want to store a value in the private var "Buttonclicked" So that based on this number I know which FLV needs to be loaded. But when I export I get an error "Access of Undefined property event". This probably has got to do with my "If" statement in the functions but I have no clue on how to solve this.

Could someone please help me out? I know programming with classes would be much neater, but I have to little experience in this.

Thanks in advance, Harold

Aucun commentaire:

Enregistrer un commentaire