Showing posts with label ActionScript. Show all posts
Showing posts with label ActionScript. Show all posts

Wednesday, February 10, 2010

Calling Flash Remote Service in Flash with Zend amf

Steps:

  1. Download Zend amf library and put it into your htdocs (www root) folder.
  2. Create a Gateway.php for connection
  3. Create your service using php
  4. Connect to service using ActionScript in TestService.fla
  5. Run the TestService.fla

Step 1:

  1. Download Zend amf library: http://framework.zend.com/download/amf
  2. Unzip and put the Zend folder into your htdocs folder

Step 2:

  1. Create a folder named “service” under htdocs.
  2. Create a Gateway.php under htdocs.
  3. Gateway.php:

<?php
	require('./Zend/Amf/Server.php');
	//require('./services/HowAreYou.php');
	
	$server = new Zend_Amf_Server();
	$server->addDirectory(dirname(__FILE__) .'/services/');
	//$server->setClass('HowAreYou');
	$response = $server->handle();
	echo $response;
?>




Step 3:




  1. Create your remote service “HowAreYou.php” under “service” folder


  2. HowAreYou.php



<?php
class HowAreYou{
    /*
     * @param  string $receiveMsg
     * @return string $replyMsg
     */
	function sendStr($yourname){
		return "$yourname, 你好!";
	} 
}
?>


Step 4:




  1. Write ActionScript in your TestService.fla file


  2. In TestService.fla



stop();
var connection:NetConnection; // Initialize NetConnection Object
var responder:Responder;  // Initialize Responder Object
 
//Construct Instance
responder = new Responder(onResult, onFault);  // Show Receive Data
connection = new NetConnection; 
// Setting
var gateway:String = "http://localhost/Gateway.php"; 
connection.connect(gateway);  // Connect to Zend amf
// Data Receieve
function onResult(Result:String):void { 
   trace(Result);
}
// Fail
function onFault():void {  
    trace("Connection Fail");
}
// Call Remoting Service's Method: (Class.Method, reaonder, parameter)
connection.call("HowAreYou.sendStr",responder,"your_message");


Step 5:




  1. Test(Run) your TestService.fla


  2. you will receive “your_message,你好!”  on the output window

Saturday, August 29, 2009

ActionScript 3.0 Tutorial Slide. Hope it helps

Coding Flash : ActionScript(3.0) Tutorial

I use this slide to give an ActionScript 3.0 Tutorial in the freshman training course of Intelligent Agent Laboratory in Department of Computer Science and Information Engineering of National Taiwan University.

FYI.

Saturday, July 18, 2009

Produce Random Number with ActionScript

var randomNumber:Number;


randomNumber = Math.random(); // 0 <= randomNumber < 1

Catch Keyboard Event (Key Down) with ActionScript

// catch Key Down Event

stage.addEventListener( KeyboardEvent.KEY_DOWN, KeyDownEventHandler);


function KeyDownEventHandler(e:KeyboardEvent ){
    trace(e.charCode);
    trace(e.keyCode);


    if( e.keyCode == Keyboard.UP){
        // use keyCode to detect special key

    }
    else if( e.keyCode == Keyboard.DOWN){
    }
    else if( e.keyCode == Keyboard.LEFT){
    }
    else if( e.keyCode == Keyboard.RIGHT){
    }
    else if( e.keyCode == Keyboard.SPACE){
    }
    else if( e.keyCode == Keyboard.SHIFT){
    }
    else if( e.charCode == 98){
        // b, ascii code = 98

        // use charCode to detect character
    }
    else if( e.charCode == 108){
        // l
    }
    else if( e.charCode == 97){
        // a
    }
    else if( e.charCode == 65){
        // A
    }
    else if( e.charCode == 118){
        // v
    }
    else if( e.charCode == 114){
        // r
    }
    else{
        //
    }
}

Monday, June 29, 2009

Using Global Variable in Flex

 

1. Declare the global variable in the main .mxml file.

 

In the main .mxml file :

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="TryAS()">
    <mx:Script>
        <![CDATA[
            public var bookSize:Rectangle = new Rectangle(0,0,800,600);

            public function TryAS():void{
                trace("Hello World");
            }
       ]]>
    </mx:Script>
</mx:WindowedApplication>

2.Use Application.application.yourGlobalVariableName to access the variable.

 

In the .as class you write :

import mx.core.Application;

// Application.application.yourGlobalVariableName

Application.application.bookSize.width

Thursday, June 25, 2009

Writing Action Script in Flex

Writing Action Script in Flex

Example of a .mxml file :

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="TryAS()">
    <mx:Script>
        <![CDATA[
            import Puzzles.*;
            public function TryAS():void{
                trace("InitPuzzles");
            }
       ]]>
    </mx:Script>

</mx:Application>