Announcement

Collapse
No announcement yet.

Scaleform 4/AS3 - Working with AS3 Packages

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Scaleform 4/AS3 - Working with AS3 Packages

    Hey everyone! This short tutorial will explain how to create a very simple AS3 package containing a single class, and a single public variable that can be accessed from UnrealScript. Packages are a way to organize your AS3 classes, and work similarly to namespaces.

    Step 1 - Creating the AS3 Package
    1. In Flash, click on File, then selected New. Or press CTRL+N.
    2. Now select ActionScript 3.0 Class in the New Document window. Be sure to select ActionScript 3.0 Class, and not just ActionScript 3.0.
    3. Press OK.
    4. Now name your class MySampleHudClass, and press OK. This will generate a new AS3 file named MySampleHudClass.as which looks like this:

      Code:
      package  {
      	
          public class MySampleHudClass {
      
              public function MySampleHudClass() {
      	    // constructor code
      	}
      
          }
      	
      }
    5. Let's modify the class. First, let's add a directory path to the package declaration. This path points to the directory structure where this AS3 file will be found, relative to the Flash file that will make use of it.

      Code:
      package com.scaleform.hud {
      So, this AS3 file will be saved at \com\scaleform\hud\MySampleHudClass.as. And remember, this is a relative path - relative to the FLA file that will use it. In other words, the com directory lives at the same level as the FLA file. See image for clarification:



    6. Ok, next we need to let the package know where to find MovieClip, since our class will extend MovieClip:

      Code:
      import flash.display.MovieClip;
    7. Then we modify the class declaration so that it in fact extends MovieClip:

      Code:
      public class MySampleHudClass extends MovieClip {
    8. Inside the class, let's add a publicly accessible variable (a variable we can get to and modify from UnrealScript):

      Code:
      public var SomeNumber:int;
    9. And finally, inside the class's constructor, let's execute the constructor function of MovieClip (the super of our class), and set SomeNumber to a default value. And let's also trace out the value of SomeNumber to the log window.

      Code:
      public function MySampleHudClass() {
          super();
      			
          SomeNumber = 5;
          trace("SomeNumber: " + SomeNumber);
      }
      You should end up with this:

      Code:
      package com.scaleform.hud {
      	
          import flash.display.MovieClip;
      	
          public class MySampleHudClass extends MovieClip {
      
      	public var SomeNumber:int;
      		
      	public function MySampleHudClass() {
      	    super();
      			
      	    SomeNumber = 5;
                  trace("SomeNumber: " + SomeNumber);
      	}
      
          }
      	
      }
    10. Save the file as \com\scaleform\hud\MySampleHudClass.as.




    Step 2 - Assigning the AS3 File as the Document Class of a FLA File
    1. Next, open your HUD FLA file. This tutorial does not explain how to create a HUD FLA file.
    2. With nothing selected on the stage of the FLA file, open the Properties panel.
    3. In the field "Class:", type: com.scaleform.hud.MySampleHudClass
    4. Now save the file, then publish it using the Scaleform Launcher.
    5. You should see no compile errors, but you will hopefully see: "SomeNumber: 5" in the log window.




    Step 3 - UnrealScript
    1. Now, open your UnrealScript HUD Class. Again, this tutorial does not explain how to create and instantiate an UnrealScript HUD class.
    2. In the class's Init() or Start() function, add a new local variable at the very top:

      Code:
      local int testNumber;
    3. Now, let's get the value of SomeNumber and store it in testNumber:

      Code:
      testNumber = GetVariableObject("root").GetInt("SomeNumber");
      `log("Test Number: " @ testNumber);
    4. Now, save this file, and recompile your scripts.
    5. Finally, be sure to reimport your FLA file into UDK.



    When you run the game with the UDK log window open, you should now see the message: Test Number: 5 displayed when your HUD is displayed!

    #2
    Opening the demo files (the ones at UDK\Development\Flash\AS3\CLIK\demos), I noticed none of them declare a Document Class (as this tutorial instructs), but yet they read the class file (.as) from D:\UDK\Development\Flash\AS3\CLIK\demos\com\scalef orm\demos. How come?

    Edit: found the answer -- the document class is declared in the each file's container movie clip (e.g. the movieclip "Window_MainDemo", for the file Window_MainDemo.fla).

    Comment


      #3
      Hi Matt,

      I know this isn't related to the thread, but i was hoping you or someone could help me. For some reason none of my CLIK buttons work, I have the eventlisteners in unrealscript yet they don't work or the widget is never initialized, I would appreciate help, thanks.

      CODE:
      class BloodCoronet_MENU extends GFxMoviePlayer;

      var GFxClikwidget BtnSingleplayer, BtnMultiplayer, BtnSettings, BtnExit;

      function bool Start(optional bool StartPaused = false) {
      Super.Start();
      Advance(0);

      return true;
      }

      event bool WidgetInitialized(name WidgetName, name WidgetPath, GFxObject Widget) {
      local bool bWasInitialized;
      bWasInitialized = false;
      switch(WidgetName)
      {
      case ('BtnSingleplayer'):
      BtnSingleplayer = GFxClikwidget(Widget.GetObject("BtnSingleplayer", class'GFxClikwidget'));
      BtnSingleplayer.AddEventListener('CLIK_buttonClick ', OnSingleplayerClicked);
      bWasInitialized = true;
      break;
      case ('BtnMultiplayer'):
      BtnMultiplayer = GFxClikwidget(Widget.GetObject("BtnMultiplayer", class'GFxClikwidget'));
      BtnMultiplayer.AddEventListener('CLIK_buttonClick' , OnMultiplayerClicked);
      bWasInitialized = true;
      break;
      case ('BtnSettings'):
      BtnSettings = GFxClikwidget(Widget.GetObject("BtnSettings", class'GFxClikwidget'));
      BtnSettings.AddEventListener('CLIK_buttonClick', OnSettingsClicked);
      bWasInitialized = true;
      break;
      case ('BtnExit'):
      BtnExit = GFxClikwidget(Widget.GetObject("BtnExit", class'GFxClikwidget'));
      BtnExit.AddEventListener('CLIK_buttonClick', OnExitClicked);
      bWasInitialized = true;
      break;
      default:
      return Super.WidgetInitialized(WidgetName, WidgetPath, Widget);
      }
      return bWasInitialized;
      }

      function OnSingleplayerClicked(GFxClikwidget.EventData e) {

      }

      function OnMultiplayerClicked(GFxClikwidget.EventData e) {

      }

      function OnSettingsClicked(GFxClikwidget.EventData e) {

      }

      function OnExitClicked(GFxClikwidget.EventData e) {
      ConsoleCommand("exit");
      }

      defaultproperties {
      WidgetBindings.Add((WidgetName='BtnSingleplayer', WidgetClass=class'GFxClikwidget'));
      WidgetBindings.Add((WidgetName='BtnMultiplayer', WidgetClass=class'GFxClikwidget'));
      WidgetBindings.Add((WidgetName='BtnSettings', WidgetClass=class'GFxClikwidget'));
      WidgetBindings.Add((WidgetName='BtnExit', WidgetClass=class'GFxClikwidget'));
      }

      Comment

      Working...
      X