Welcome, Guest. Please login or register.
Did you miss your activation email?
31 Jul 2010, 11:31:09 UTC
Forum home
+  flexdeveloper.eu Forum
|-+  Flex and ActionScript 3.0
| |-+  ActionScript 3.0 (Moderators: JMWhittaker, Jan K, thewarpedcoder, James)
| | |-+  Setting Transitions Programmatically
« previous next »
Pages: [1] Print
Author Topic: Setting Transitions Programmatically (Read 10737 times)
tpaulman
Guest
« on: 16 Sep 2006, 00:04:55 UTC »

Hello All,

I've spent the past couple of days trying to set up a sample with Title Windows, pop-up manager, view states and transitions in Flex 2 -- but I'm really having troubles with the Programatic Transitions.

I am popping up 5 title windows -- four little ones on the left, one big one on the right.

When the user shift-clicks on any of the title windows on the left, that window becomes the big window on the right and the windows on the left reposition themselves accordingly.

I've got the code working for popping up the windows, creating the view states and switching between the states.  The trouble I am having is with setting the transitions programatically.  If I use the MXML to set the transitions, it works fine.  I just can't seem to get the Actionscript equivalent working properly.  

Anyone have any ideas about how I can get this off the ground?  

Thanks in advance!
-Tim

[Code in following Message]
« Last Edit: 31 Aug 2008, 22:56:53 UTC by flexy » Logged
tpaulman
Guest
« Reply #1 on: 16 Sep 2006, 00:06:36 UTC »

Code:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
creationComplete="onInit()">

      <mx:Script>
         <![CDATA[
                  import mx.collections.ArrayCollection;

                  import mx.containers.TitleWindow;
                  
                  import mx.effects.Move;

                  import mx.managers.PopUpManager;

                  import mx.states.SetProperty;
                  import mx.states.State;
                  import mx.states.Transition;
                  
                  public var _NUMWINS:Number = 5;
                  public var windowArray:ArrayCollection = new ArrayCollection();                  
                  
                  /*            
                  onInit -- I am creating five instances of a pop-up title
                  window using the popup manager.  The structure is a column
                  of 4 small windows on the left and one large window on the
                  right.
        
                  When all of the windows have been created, I am calling
                        addStates to add view states.
                  */
                  
                  public function onInit():void
                  {
                        var title:TitleWindow;
                        for(var i:Number=1; i<=_NUMWINS; i++)
                        {
                              title = new TitleWindow();
                              title.name = "to_state"+i.toString();
                              title.id = "to_state"+i.toString();
                              title.title = "Title " + i.toString();
                              title.showCloseButton = true;
                              title.addEventListener(MouseEvent.CLICK, setState);

                              if(i != _NUMWINS)
                              {
                                    title.x = 50;
                                    title.y = 50 + ((i-1)*110);
                                    title.height = 100;
                                    title.width = 200;
                              }else{
                                    title.x = 260;
                                    title.y = 50;
                                    title.height = 430;
                                    title.width = 500;
                              }

                              windowArray.addItem(title);
                              PopUpManager.addPopUp(title, mainCanvas);
                        }
                        addStates();
                  }

                  /*            
                  addStates -- this function creates FIVE different view states
                  based on which title window will be the "BIG" window on the
                  right hand side.

                  state1 - window1 (id="to_state1") is the big window
                  state2 - window2 (id="to_state2") is the big window
                  ...
                  state_[_NUMWINS] - window[_NUMWINS] (id="to_state[_NUMWINS]) is the big window

                  When all of the states have been added, I call the addTransitions
                  method.
                  */

                  public function addStates():void
                  {
                        var state:State;
                        var setXProperty:SetProperty;
                        var setYProperty:SetProperty;
                        var setZProperty:SetProperty;
                        var setHeightProperty:SetProperty;
                        var setWidthProperty:SetProperty;
                        var positionIndex:Number;

                        // create the five states
                        for(var i:Number=1; i<=_NUMWINS; i++)
                        {                        
                              state = new State();
                              state.name ="state"+i.toString();
                              positionIndex = 0;
                              
                              // position every window within that state
                              for(var j:Number=1; j<=_NUMWINS; j++)
                              {
                                    setXProperty = new SetProperty();
                                    setXProperty.target = windowArray.getItemAt(j-1);
                                    setXProperty.name = "x";

                                    setYProperty = new SetProperty();
                                    setYProperty.target = windowArray.getItemAt(j-1);
                                    setYProperty.name = "y";

                                    setHeightProperty = new SetProperty();
                                    setHeightProperty.target = windowArray.getItemAt(j-1);
                                    setHeightProperty.name = "height";

                                    setWidthProperty = new SetProperty();
                                    setWidthProperty.target = windowArray.getItemAt(j-1);
                                    setWidthProperty.name = "width";
            
                                    // if i=j, then this is the BIG window position
                                    if(i == j)
                                    {
                                          setXProperty.value = 260;
                                          setYProperty.value = 50;
                                          setHeightProperty.value = 430;
                                          setWidthProperty.value = 500;

                                    }else{
                                          setXProperty.value = 50;
                                          setYProperty.value = 50 + ((positionIndex)*110);
                                          setHeightProperty.value = 100;
                                          setWidthProperty.value = 200;
                                          positionIndex++;
                                    }

                                    // push all of the overrides into the state array
                                    state.overrides.push(setXProperty);
                                    state.overrides.push(setYProperty);
                                    state.overrides.push(setHeightProperty);
                                    state.overrides.push(setWidthProperty);
                              }
                              // push the state onto the states array
                              states.push(state);
                        }
                        
                        // Add the Transitions
                        addTransitions();
                  }
                  
                  /*      
                  addTransitions is supposed to add the transition via code
                  THIS FUNCTION DOES NOT WORK AT ALL
                  WHAT AM I DOING WRONG??  ANY IDEAS??
                  */
                  public function addTransitions():void
                  {
                        var transition:Transition = new Transition();
                        var move:Move = new Move();
                        move.duration=400;
                        move.targets = [windowArray.getItemAt(0),windowArray.getItemAt(1)];
                        transition.fromState = "*";
                        transition.toState = "*";
                        transition.effect = move;
                  }

                  /*
                  Called when any of the title windows get the CLICK event
                  Sets the state if it is a SHIFT+CLICK                  
                  */
public function setState(event:MouseEvent):void
                  {
                        if(event.shiftKey)
                       {
                              var tmpStr:String = event.currentTarget.name;
                              currentState=tmpStr.slice(3, tmpStr.length);
                        }
                  }
            ]]>
      </mx:Script>
      <mx:Canvas id="mainCanvas" width="100%" height="100%" />

<!--
      ***
      This is the MXML that I am trying to replicate in code via
      the addTransitions function.  If using the MXML variant, it works
      perfectly (despite the fact that Flex throws warnings about my binding.
      When using the addTransitions function, it doesn't work at all.
      ***
    <mx:transitions>
        <mx:Transition id="myTransition" fromState="*" toState="*">
            <mx:Move id="myMove" duration="400" targets="{[windowArray.getItemAt(0),windowArray.getItemAt(1)]}"/>
        </mx:Transition>
    </mx:transitions>
 -->
</mx:Application>

** MOD: IF YOU'VE GOT LOADS OF SPACES AT THE ENDS OF YOUR LINES, PLEASE CAN YOU REMOVE THEM BEFORE POSTING. I HAD TO REMOVE 30+ WHITESPACE CHARACTERS FROM SOME OF YOUR LINES... COMPLETELY UNNECESSARY! **
« Last Edit: 31 Aug 2008, 23:06:12 UTC by flexy » Logged
flexy
Guest
« Reply #2 on: 16 Sep 2006, 11:31:55 UTC »

Ok, just having a play with this code now.
Logged
flexy
Guest
« Reply #3 on: 16 Sep 2006, 15:57:23 UTC »

Ok, it seems to be incredibly difficult to apply transitions to states using AS in the same way you can with MXML. The reason I can see you're trying to use AS is because you have 'n' number of windows, and therefore, need to assign these dynamically to the effect target property. The solution below achieves this, but with a combination of MXML and AS. I've retyped windowArray from an ArrayCollection to an Array, and made it bindable. By doing this, it is possible then to bind this directly to the targets property, and bingo, all of the windows animate.

I appreciate this doesn't address the problem of creating states and their associated transitions in AS; there's very little documentation about this online, so consider yourself a trail-blazer! The code below does the same thing though...
Logged
flexy
Guest
« Reply #4 on: 16 Sep 2006, 15:57:33 UTC »

Code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
      creationComplete="onInit()">
      
      <mx:Script>
            <![CDATA[
                  import mx.collections.ArrayCollection;
                  import mx.containers.TitleWindow;
                  import mx.managers.PopUpManager;
                  import mx.states.SetProperty;
                  import mx.states.State;
                  import mx.states.Transition;
                  import mx.effects.Move;
                  import mx.effects.Parallel;                                    
                  
                  public var _NUMWINS:Number = 5;
                  [Bindable] private var windowArray:Array = new Array();
                  
                  /*
                  onInit -- I am creating five instances of a pop-up title
                  window using the popup manager.  The structure is a column
                  of 4 small windows on the left and one large window on the
                  right.

                  When all of the windows have been created, I am calling
                        addStates to add view states.
                  */
    
                  public function onInit():void
                  {
                        var title:TitleWindow;
                        for(var i:Number=1; i<=_NUMWINS; i++)
                        {
                              title = new TitleWindow();
                              title.name = "to_state"+i.toString();
                              title.id = "to_state"+i.toString();
                              title.title = "Title " + i.toString();
                              title.showCloseButton = true;
                              title.addEventListener(MouseEvent.CLICK, setState);
                              
                              if(i != _NUMWINS)
                              {
                                    title.x = 50;
                                    title.y = 50 + ((i-1)*110);
                                    title.height = 100;
                                    title.width = 200;
                              }else{
                                    title.x = 260;
                                    title.y = 50;
                                    title.height = 430;
                                    title.width = 500;
                              }
                              
                              windowArray.push(title);
                              PopUpManager.addPopUp(title, mainCanvas);
                        }
                        addStates();
                  }
                  
                  /*
                  addStates -- this function creates FIVE different view states
                  based on which title window will be the "BIG" window on the
                  right hand side.

                  state1 - window1 (id="to_state1") is the big window
                  state2 - window2 (id="to_state2") is the big window
                  state_[_NUMWINS] - window[_NUMWINS] (id="to_state[_NUMWINS]) is the big window

                  When all of the states have been added, I call the addTransitions
                  method.
                  */
 
                  public function addStates():void
                  {                  
                        var state:State;
                        var setXProperty:SetProperty;
                        var setYProperty:SetProperty;
                        var setZProperty:SetProperty;
                        var setHeightProperty:SetProperty;
                        var setWidthProperty:SetProperty;
                        var positionIndex:Number;

                        // create the five states
                        for(var i:Number=1; i<=_NUMWINS; i++)
                        {                        
                              state = new State();
                              state.name ="state"+i.toString();
                              positionIndex = 0;

                             // position every window within that state
                              for(var j:Number=1; j<=_NUMWINS; j++)
                              {
                                    setXProperty = new SetProperty();
                                    setXProperty.target = windowArray[j-1];
                                    setXProperty.name = "x";

                                    setYProperty = new SetProperty();
                                    setYProperty.target = windowArray[j-1];
                                    setYProperty.name = "y";

                                    setHeightProperty = new SetProperty();
                                    setHeightProperty.target = windowArray[j-1];
                                    setHeightProperty.name = "height";

                                    setWidthProperty = new SetProperty();
                                    setWidthProperty.target = windowArray[j-1];
                                    setWidthProperty.name = "width";
            
                                    // if i=j, then this is the BIG window position
                                    if(i == j)
                                    {
                                          setXProperty.value = 260;
                                          setYProperty.value = 50;
                                          setHeightProperty.value = 430;
                                          setWidthProperty.value = 500;

                                    }else{
                                          setXProperty.value = 50;
                                          setYProperty.value = 50 + ((positionIndex)*110);
                                          setHeightProperty.value = 100;
                                          setWidthProperty.value = 200;
                                          positionIndex++;
                                    }

                                    // push all of the overrides into the state array
                                    state.overrides.push(setXProperty);
                                    state.overrides.push(setYProperty);
                                    state.overrides.push(setHeightProperty);
                                    state.overrides.push(setWidthProperty);

                              }
                              // push the state onto the states array
                              states.push(state);
                        }
                        //addTransitions();
                  }

                  /*      
                  addTransitions is supposed to add the transition via code                  
                  THIS FUNCTION DOES NOT WORK AT ALL                  
                  WHAT AM I DOING WRONG??  ANY IDEAS??                              
                  */
                  public function addTransitions():void
                  {
                        // null                    
                  }

                  /*
                  Called when any of the title windows get the CLICK event
                  Sets the state if it is a SHIFT+CLICK                  
                  */                  
                  public function setState(event:MouseEvent):void
                  {
                   if(event.shiftKey)
                        {
                              var tmpStr:String = event.currentTarget.name;
                              currentState=tmpStr.slice(3, tmpStr.length);
                        }
                  }
]]>
      </mx:Script>
      
      <mx:Canvas id="mainCanvas" width="100%" height="100%" />

<!--
      ***
      This is the MXML that I am trying to replicate in code via
      the addTransitions function.  If using the MXML variant, it works
      perfectly (despite the fact that Flex throws warnings about my binding.
      When using the addTransitions function, it doesn't work at all.
      *** -->
      
    <mx:transitions>
        <mx:Transition id="myTransition" fromState="*" toState="*">
            <mx:Move id="myMove" duration="400" targets="{windowArray}"/>
        </mx:Transition>
    </mx:transitions>
</mx:Application>
« Last Edit: 31 Aug 2008, 23:13:01 UTC by flexy » Logged
tpaulman
Guest
« Reply #5 on: 18 Sep 2006, 16:12:46 UTC »

Hi Flexy -- thanks for the reply.  Glad to hear that I'm not the only one who is having difficulties with the AS transitions.  I don't feel so bad now!  And thanks for the reminder about the [Bindable] declaration.  Still haven't fully broken my Flex 1.5 bad habits yet!

 Cheesy
Logged
flexy
Guest
« Reply #6 on: 18 Sep 2006, 16:52:55 UTC »

Glad to help  Smiley

AS transitions seems to baffling a lot of people right now; we came up against this one a few weeks ago. Does anyone know how to directly translate an MXML transition to pure AS3?
Logged
tpaulman
Guest
« Reply #7 on: 18 Sep 2006, 17:20:34 UTC »

I just got the following response from Adobe Support on the issue ::


"I think the problem is that when you create the transition you don’t add it to the <mx:transitions> array of the application. Try this:"

Code:
public function addTransitions():void
{
      var transition:Transition = new Transition();                                  
      var move:Move = new Move();
                                                            
      move.duration=400;
      move.targets = windowArray;
                      
      transition.fromState = "*";
      transition.toState = "*";
      transition.effect = move;            

      // I was mising THIS line
      transitions.push(transition);
}


That certainly seems to do the trick -- mystery solved!  Hope this helps out anyone else who is having troubles with the issue!

Cheers,
-Tim
Logged
flexy
Guest
« Reply #8 on: 18 Sep 2006, 17:33:16 UTC »

Nice one! That's going to be a load of use to a lot of people.
Logged
Pages: [1] Print
« previous next »
Share this on: Twitter Twitter Del.icio.us del.icio.us Digg Digg
Jump to:

©2006-2010 Flexdeveloper.eu/Jodie O'Rourke. All rights reserved.
Adobe®, Adobe® Flash™, Adobe® AIR™ and Adobe® Flex™ are registered trademarks of Adobe Systems Incorporated in the United States and other countries. All rights reserved.

Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC