Tutorial 12: Integrating Macromedia Flash with VB

The goal of this example is to just give you an example of how to set up a Visual Basic program that will not only run a Flash SWF file, but also open two paths of communication:

First thing's first, you can download my project and all of this documentation in:

Okay let's build this thing from scratch (or nearly scratch):

  1. First thing you need is a Shockwave Flash file (.SWF). These can be created using Macromedia Flash, and more recently, some other third party software (Swish, Swift3D, 3D Studio Max). For the moment, I will assume that you already have a compiled .swf file, and show you how to integrate it into Visual Basic using the Shockwave component. The end of this tutorial will contain step-by-step instructions on how to create the file in Flash for those of you who are interested (and ambitious!). I will be using the following flash file: Bounce.swf
  2. Start Visual Basic, create a new project and add a Shockwave Flash component:
If this component isn't available, it's because you don't have the Flash Player installed on your machine. You can download it for free at www.macromedia.com
  1. Name your Shockwave Player component xWin.
  2. Next I added a bunch of simple controls that I will use later to control different parts of my Flash movie. If you are just playing your movie with no interaction, skip to the next point. For my example, I threw in two slider controls for the x and y coordinates of the sphere (which also have textboxes hooked up to them) and a third textbox to input a value for the angle. Lastly, I added a nice ol' command button to start the whole thing!
  3. Double click anywhere on your form, or switch to the code window and put in the following code:
    Private Sub Form_Load()
    	Form1.ScaleMode = 3	'This is just so that everything works in pixels and not twips
    	xWin.Movie = App.Path & "\Bounce.swf"		
    				'The name and location of our .swf file
    	xWin.Left = 0		'The rest of these commands are just to dynamically size your 
    				'xWin window to be the same size as the flash movie. (Makes everything 
    				'in proportion and what-not). But they aren't essential
    	xWin.Top = 0										
    	xWin.Width = 550
    	xWin.Height = 400
    	xWin.Play		'This starts your movie!
    End Sub
  4. That's it! If you just need to play your flash movie, then you can stop here! But to establish some communication between the two parts, continue on to the next steps.
  5. How Flash Talks to Visual Basic
    If you need triggers in Flash to signal the Visual Basic container to perform certain actions, this is how you go about it:
    1. I'm going to assume you have coded a simple button into your Flash movie and have assigned an action to the on release event. The big secret here is to use the built-in Flash method called FSCOMMAND in your ActionScript (just a fancy name for Flash's underlying code). So you're button code will look something like this:
    2. You don't need anything for the first argument (but keep the empty quotes), and for the second argument, you can include anything you like: A string, integer, float, or even a Flash variable name from elsewhere in your movie.
    3. The next step is to flip back to Visual Basic and set up the method that will catch the FSCommand that the Flash Movie is throwing. You do this with the member function of the Shockwave Player object called <object_name>_FSCommand. The code for this is pretty straightforward too, it just receives the arguments that are passed from the movie! My code looks like this:
      Private Sub xWin_FSCommand(ByVal command As String, ByVal args As String)
      	MsgBox args
      End Sub	 
    4. That's it for having Flash send VB information! I told ya it was easy ;-) Read on to see how to do it the other way around...

  6. How Visual Basic Talks to Flash
    This task is slightly more difficult. For simple tasks such as changing the values of variables, this is pretty much as easy as step 7, but for tasks like invoking a method written on the Flash side, from Visual Basic, we need to be a bit sneakier.
    1. For variable assignment: Simply use the SetVariable method of the Shockwave Player:
      xWin.SetVariable "xVal", Text3.Text
      xWin.SetVariable "yVal", Text2.Text
      xWin.SetVariable "angle", Text1.Text
      The first arguement is the name of the variable inside the Flash movie (variables can be declared through ActionScript or by naming something like a textfield). The second arguement is the value which you wish to assign the variable! I told ya! Easy as pie!
    2. To invoke a remote method: This is where it gets a little tricky. You see, as far as I can tell, there is no way of directly calling a Flash method by name. Every method in the Shockwave object pertains to getting/setting and movie manipulation. Fortunately for all of us, there is ways around this :-)
      1. We're going to rely on the fact that Flash movies can be built in a heirarchy, that is to say, parent movies can have child movies and accessing methods, frames and variables on different levels of the tree is in fact quite easy. It's all done through the dot operator <.> (ActionScript will start to look a LOT like C++ to you!)
      2. Create a symbol within your root movie clip which is itself a movie clip, thereby becoming the child clip of your original parent clip. How nice, eh? Your flash heirarchy should look something like this (don't panic now if this Flash stuff makes more sense, remember I'm going to dedicate the last section to strictly building the Flash side of things):
      3. Create a stop frame (a frame with the stop(); method in it) in the first frame of layer #1, then create a second layer which has an action in the second frame to call a method. So the code for this frame looks like this:
        This is where you use the dot operator! The first part of the function represents our root movie (called Scene1) and the second part is method which is a member of that movie.
      4. In the first frame of the root movie, write a method such as this:

      5. function moveBall() {
        mikeBall._x = xVal;
        mikeBall._y = yVal;
        mikeBall._rotation = angle;
      6. Now all you have to do is go back to Visual Basic, and throw in the code which starts the sub-movie playing, which in turn invokes the method! Voila! Here it is:

        xWin.TPlay "_root.mikeBall"

  7. Building the Flash file, step-by-step:
    This little tidbit is still in the making! I'll get it up as soon as I have a chance!

    And that, as we say, is it.

    by Mike Hornby-Smith