Archive for the ‘ActionScript’ Category

Flash Builder/Flex 4 and ZendAMF alternative connection example

Tuesday, May 18th, 2010

I was attempting to try out a simple test of ZendAMF and Flash Builder. I had begun by building the test using WAMP on my local machine. I had began by using the built in Connect to Data/Service wizard built into Flash Builder.  The example I was following was from and article over at the Zend Developer Zone called: Data-centric Adobe Flash Builder development with the Zend Framework. Everything worked slick as can be runing locally on WAMP but when I tried using the same data connection with my site on an unamed hosting company I was ending up with errors due to the age of mySQL and PHP that my hosting company is currently using. I am stilll working on resolving those problems. But in the meantime I was needing to get it to work.  I decided to incorporate the simple example Lee Brimelow used with his Flash and Zend AMF tutorial.

The example here is a simple voting screen to pick from a set of actors for best actor and worst actor. It holds a simple SharedObject variable to determine if you have already voted and keeps you from voting twice.

Here is the MXML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="500" minHeight="600"  creationComplete="application_creationCompleteHandler(event)" width="500" height="600" backgroundColor="#000000">
    <fx:Script>
        <![CDATA[
            import flash.events.Event;
            import flash.events.NetStatusEvent;
            import flash.events.SecurityErrorEvent;
            import flash.net.NetConnection;
            import flash.net.NetStream;
            import flash.net.Responder;
            import flash.net.SharedObject;
            import flash.system.Security;
           
            import mx.controls.Alert;
            import mx.events.CloseEvent;
            import mx.events.FlexEvent;

           
            private var so:SharedObject = SharedObject.getLocal("awardVoting");
            private var radioEnabled:Boolean = false;
            private var nc:NetConnection;
           
            protected function voteBtn_clickHandler(event:MouseEvent):void
            {
               
                var record:Object = new Object;
                record.bestActor = radiogroup1.selectedValue;
                record.worstActor = radiogroup2.selectedValue;
               
                /* had to use the old Zend connection scripts from Flash because of the age of PHP and mySQL on my server */
                nc = new NetConnection();
                nc.connect("/*path to my file*/bootstrapper.php");
               
                var responder:Responder = new Responder(onResResult, onResFault);
               
                nc.call("awards.add", responder, record.bestActor, record.worstActor);
               
                Alert.show("Thank you for voting!", "Acting Award voting", 0 , this, doSomethingElse);
               
            }
           
            private function onResResult(result:Object):void {
                //Alert.show("it works!!!");
               
            }
            public function onResFault(fault:Object):void {
                Alert.show(String(fault.description));
            }

           
            protected function doSomethingElse(event:CloseEvent):void {
                radioEnabled = false;
                this.doEnable(radioEnabled);
                so.data.voted = "true";
                so.flush();
            }
                       
           
            protected function application_creationCompleteHandler(event:FlexEvent):void
            {
                Security.allowDomain("/*my domain*/");
                Security.loadPolicyFile("/*path to my file*/crossdomain.xml");

                /*so.clear();*/
                if (so.data.voted) {
                    Alert.show("You have already voted!", "Acting Award voting");
                    radioEnabled = false;
                    this.doEnable(radioEnabled);
                } else {
                    Alert.show("Welcome to the Acting Awards poll.\n\nPlease select an actor from each category", "Acting Award voting");
                    radioEnabled = true;
                    this.doEnable(radioEnabled);
                }
               
            }
           
            protected function doEnable(n:Boolean):void {
                radiogroup1.enabled = n;
                radiogroup2.enabled = n;
                voteBtn.enabled = n;
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <s:RadioButtonGroup id="radiogroup1" />
        <s:RadioButtonGroup id="radiogroup2" />
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <mx:Image x="0" y="0" source="images/awardsMockup.jpg"/>
    <s:BorderContainer x="30" y="209" width="203" height="319" backgroundColor="#000000" backgroundAlpha="0.5"> </s:BorderContainer>
    <s:BorderContainer x="267" y="209" width="203" height="319" backgroundColor="#000000" backgroundAlpha="0.5">    </s:BorderContainer>
    <s:VGroup x="30" y="209" width="203" height="319" id="vGroup" color="#FFFFFF" contentBackgroundColor="#000000" contentBackgroundAlpha="0.5" gap="6" paddingLeft="8" paddingRight="8" paddingTop="8" paddingBottom="8">
        <s:Label text="Best Actor:" fontWeight="bold" fontSize="13"/>
        <s:RadioButton label="Viggo Mortensen - The Road" groupName="radiogroup1" value="Mortensen"/>
        <s:RadioButton label="Sharlto Copley - District 9" groupName="radiogroup1" value="Copley"/>
        <s:RadioButton label="George Clooney - Up in the Air" groupName="radiogroup1" value="Clooney"/>
        <s:RadioButton label="Matt Damon - The Informant!" groupName="radiogroup1" value="Damon"/>
        <s:RadioButton label="Ben Foster - The Messenger" groupName="radiogroup1" value="Foster"/>
        <s:RadioButton label="Jeremy Renner - The Hurt Locker" groupName="radiogroup1" value="Renner "/>
        <s:RadioButton label="Brad Pitt - Inglourious Basterds" groupName="radiogroup1" value="Pitt"/>
        <s:RadioButton label="Colin Firth - A Single Man" groupName="radiogroup1" value="Firth"/>
        <s:RadioButton label="Tobey Maguire - Brothers" groupName="radiogroup1" value="Maguire"/>
        <s:RadioButton label="Jeff Bridges - Crazy Heart" groupName="radiogroup1" value="Bridges"/>
    </s:VGroup>
    <s:VGroup x="267" y="209" width="202" height="319" color="#FFFFFF" contentBackgroundColor="#000000" contentBackgroundAlpha="1.0" paddingLeft="8" paddingRight="8" paddingTop="8" paddingBottom="8">
        <s:Label text="Don't Quit Your Day Job:" fontWeight="bold" fontSize="13"/>
        <s:RadioButton label="Jack Black  - Year One" groupName="radiogroup2" value="Black"/>
        <s:RadioButton label="Jim Carrey - A Christmas Carol" groupName="radiogroup2" value="Carrey"/>
        <s:RadioButton label="Nicholas Cage - Knowing" groupName="radiogroup2" value="Cage"/>
        <s:RadioButton label="Robert Pattinson - New Moon" groupName="radiogroup2" value="Pattinson"/>
        <s:RadioButton label="Vince Vaughn - Couples Retreat" groupName="radiogroup2" value="Vaughn"/>
        <s:RadioButton label="John Cusack - 2012" groupName="radiogroup2" value="Cusack"/>
        <s:RadioButton label="Kevin James - Paul Blart Mall Cop" groupName="radiogroup2" value="James"/>
        <s:RadioButton label="Johnny Depp - Public Enemies" groupName="radiogroup2" value="Depp"/>
        <s:RadioButton label="Taylor Lautner - New Moon" groupName="radiogroup2" value="Lautner"/>
        <s:RadioButton label="Ashton Kutcher - Personal Effects" groupName="radiogroup2" value="Kutcher"/>
    </s:VGroup>
    <s:Button x="215" y="551" label="Vote" id="voteBtn" click="voteBtn_clickHandler(event)" />
    <s:TextArea x="66" y="152" width="361" height="31" text="Acting Award Voting" fontFamily="Arial" fontWeight="bold" fontSize="20" borderVisible="false" textAlign="center" contentBackgroundAlpha="0.0" color="#FFFFFF"/>
</s:Application>

Here is the awards.php file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
class awards {
    public function __construct()
    {
        require("/*path to file outside of public web directory*/configConn.inc.php");
        mysql_connect($server,$username,$password);
        mysql_select_db("/*my database*/" );
    }
    public function getPeople()
    {
        $result = mysql_query("SELECT * FROM awards");
        $t = array();

        while($row = mysql_fetch_assoc($result))
        {
            array_push($t, $row);
        }

        return $t;

    }

      public function add( $bestActor, $worstActor ) {

        $insert = sprintf( "INSERT INTO awards VALUES (NULL, '%s', '%s')",
        mysql_real_escape_string($bestActor),
        mysql_real_escape_string($worstActor));

        mysql_query($insert);

        return 'You addded: ' . $bestActor . $worstActor . '. The Query string is: ' . $insert;
    }
}

Here is the configConn.inc.php:

1
2
3
4
5
<?php
$server = "/*my mySQL server*/";
$username= "/*my username*/";
$password = "/*my password*/";
?>

And finally here is the bootstrapper.php file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
?php

error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", "on");

ini_set("include_path", ini_get("include_path") . ":'/*my path to Zend*/");

require_once Zend/Amf/Server.php';
require_once '
awards.php';

$server = new Zend_Amf_Server();

$server->setClass("awards");
// You can keep adding all the classes you need here

echo($server->handle());

?>

P.S. I aplologize in advance for any typos due to attempting to make the code more generic.

GreenSock Tweening Platform

Wednesday, November 4th, 2009

My friend Chuck Stein just sent me a link to the GreenSock tweening libraries:

They have downloads for AS3 and AS2. They are looking pretty cool at first glance.