A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: Passing variables into a class in .as file

  1. #1
    Junior Member
    Join Date
    Apr 2006
    Posts
    29

    Passing variables into a class in .as file

    Hi.

    This may be a simple question but not sure how to do it.

    I have purchased a video player that derives all its code from external .as files (stored in com/flashaman/Videoplayer.as). Problem is that it's designed to be for a single video file where I would like to use multiple instances of the player in various areas of my flash file.

    The code within the Videoplayer.as file is:

    Actionscript Code:
    package com.flashaman.videoplayer
    {
        public class Videoplayer extends MovieClip
        {      
            // - VARIABLES
            private var _playerWidth:int = 640;
            private var _playerHeight:int = 390;
            private var _currentVideo:int = 0;
           
            private var _videoURL:String = "video/showreel.mov";
            private var _imageURL:String = "image/showreel.jpg";

    Specifically the only variables I need to modify are _videoURL and _imageURL. How can I modify these files from within my Flash file?

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    I am not good with Classes, but try referring it to a variable in your Flash. Change your variables in your .as files to this:

    Actionscript Code:
    private var _videoURL:String = _root.video_url;
    private var _imageURL:String = _root.image_url;

    and then in your Flash, type this in the Main Stage, on a Frame:

    Actionscript Code:
    video_url = "video/showreel.mov";
    image_url = "image/showreel.jpg";

    I tried something similar, it worked, but not flawlessly. My class constructor was called twice, and the first time it didn't register the variables in my Flash file, but it did the next time. Why that happened, is because the .as file was loaded BEFORE the Flash file's content.
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    This is not going to work like this.

    What I would suggest is to make a setter in that so that you can change it from anywhere you want.


    arkitx

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Location
    Australia
    Posts
    21
    To expand on setters, this is how you would define it in your class:

    Code:
    public function set videoURL(videoURL:String):void{
    _videoURL = videoURL
    }
    So with that, you can reference the holder for the VideoPlayer and edit it like you would any other property, like x or width.

    E.g.
    (For this, I'm just using "videoPlayer" as an instance of the VideoPlayer class you're using)
    Code:
    videoPlayer.videoURL = "new URL here";

  5. #5
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Thanks, Danny22, was helpful for me as well
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  6. #6
    Junior Member
    Join Date
    Apr 2006
    Posts
    29
    Thanks for the suggestions thus far - I have created new projects to play with the methods discussed, however to make things more challenging the Videoplayer class discussed above is not instantiated through a constructor, rather through linking a symbol (movie clip) to the class. The instantiation is made when you place that symbol on the stage and hit that frame. From what I've read you can't pass parameters into this class. Any ideas on this?

  7. #7
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    Class

    Actionscript Code:
    public var _videoURL:String;// = "video/showreel.mov";
    public var _imageURL:String;// = "image/showreel.jpg";

    Timeline

    Actionscript Code:
    mcVideoplayer._videoURL = "video/showreel.mov";
    mcVideoplayer._imageURL = "image/showreel.jpg";




    arkitx
    Last edited by arkitx; 04-16-2012 at 11:09 AM.

  8. #8
    Junior Member
    Join Date
    Apr 2006
    Posts
    29
    Quote Originally Posted by arkitx View Post
    Class

    Actionscript Code:
    public var _videoURL:String;// = "video/showreel.mov";
    public var _imageURL:String;// = "image/showreel.jpg";

    Timeline

    Actionscript Code:
    mcVideoplayer._videoURL = "video/showreel.mov";
    mcVideoplayer._imageURL = "image/showreel.jpg";

    No, this didn't do it. Comes up with error "1120: Access of undefined property mcVideoPlayer". Because I'm not creating an instance of mcVideoplayer (it's being created through a symbol that's linked to the class) I can't reference it.


    arkitx

  9. #9
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    I am 1000%+ SURE the process is CORRECT.

    mcVideoplayer._videoURL = "video/showreel.mov";
    mcVideoplayer._imageURL = "image/showreel.jpg";


    This code will go on Timeline where the symbol is located. and mcVideoplayer should be the instance name of that symbol/MovieClip.

    And you also have to declare that property in you Videoplayer Class, which is:

    public var _videoURL:String;// = "video/showreel.mov";
    public var _imageURL:String;// = "image/showreel.jpg";


    Save the Class first, then test your Movie.



    arkitx

  10. #10
    Junior Member
    Join Date
    Apr 2006
    Posts
    29
    Wow, freaking awesome! Thanks arkitx... I've been going at this for nearly a week!! The problem was that the MovieClip didn't have an instance name, so it wasn't being referenced. So placing this MC on the stage is what is creating the instance of the class right?

    Final question - if I wanted to include a back button on the video player that jumped to a different scene "MainMenu" how would I go about that? I could create the button as an additional component in the video player scene which would both call the 'video finished' function and then jump scene, but it would be nice to call the function from within the class. I could then pass a variable into the class such as what scene I want it to jump to. I've tried this...

    Actionscript Code:
    this.gotoAndPlay(0, "MainMenu");

    ...from within the class but it doesn't work. Any ideas on this?

  11. #11
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    Hope you will find most of your answers from this attachment.


    arkitx
    Attached Files Attached Files

  12. #12
    Junior Member
    Join Date
    Apr 2006
    Posts
    29
    Quote Originally Posted by arkitx View Post
    Hope you will find most of your answers from this attachment.


    arkitx
    Awesome, thanks! Great resource. One thing though - it's written in AS2 so the line
    Code:
    this._parent.gotoAndStop(targetFrame,"SceneEND");
    doesn't work. I've used
    Code:
    MovieClip(this.parent).gotoAndPlay(0, "MainMenu");
    which works perfectly.

    Thanks very much for your help!

  13. #13
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    Sorry for AS2 example.

    Here is the AS3 example.

    I forgot that your code was written in AS3.



    arkitx
    Attached Files Attached Files

  14. #14
    Junior Member
    Join Date
    Apr 2006
    Posts
    29
    Thanks arkitx, working fine now. Do you know why the video would be starting to play prior to it showing on screen? You can hear it playing, but you can't see anything. Depending on the size of the video I'm loading it can be anywhere from 0.2 of a second to over a second.

  15. #15
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    Is this happened from the first FLV you navigate to Play?

    arkitx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center