alchemy中定义多个方法

//Simple String Echo example
//mike chambers
//mchamber@adobe.com

#include <stdlib.h>
#include <stdio.h>

//Header file for AS3 interop APIs
//this is linked in by the compiler (when using flaccon)
#include "AS3.h"

double sum(int num)
{
     int i;
     double total=0;
     for (i=1;i<=num;i++)
     {
         total += i;
     }
     return total;
}

//Method exposed to ActionScript
//Takes a String and echos it
static AS3_Val echo(void* self, AS3_Val args)
{
	//9 variable
	int num;
    double total;
	char buf[256];

	AS3_ArrayValue(args, "IntType", &num);
	
	total = sum(num);
	sprintf(buf, "%lf", total);
	return AS3_String(buf);
}

//try new function
static AS3_Val returnTheSelf(void* self,AS3_Val args){
	//initialize string to null
	char* val = NULL;
	
	//parse the arguments. Expect 1.
	//pass in val to hold the first argument, which
	//should be a string
	AS3_ArrayValue( args, "StrType", &val );
	
	//if no argument is specified
	if(val == NULL)
	{
		char* nullString = "null";
		//return the string "null"
		return AS3_String(nullString);
	}
	
	//otherwise, return the string that was passed in
	return AS3_String(val);

}

//entry point for code
int main()
{
	//define the methods exposed to ActionScript
	//typed as an ActionScript Function instance
	AS3_Val echoMethod = AS3_Function( NULL, echo );
	AS3_Val returnTheSelfMethod=AS3_Function(NULL,returnTheSelf);

	// construct an object that holds references to the functions
	AS3_Val result = AS3_Object( "echo: AS3ValType,returnTheSelf:AS3ValType", echoMethod ,returnTheSelfMethod);
	//result=AS3_Object("returnTheSelf:AS3ValType",returnTheSelfMethod);

	// Release
	AS3_Release( echoMethod );
	AS3_Release(returnTheSelfMethod);

	// notify that we initialized -- THIS DOES NOT RETURN!
	AS3_LibInit( result );

	// should never get here!
	return 0;
}

如上,

AS3_Val result = AS3_Object( "echo: AS3ValType,returnTheSelf:AS3ValType", echoMethod ,returnTheSelfMethod);

此object中承载了两个方法:echo,returnTheSelf.

posted on 2011-01-27 14:35  低调的沧桑  阅读(742)  评论(0编辑  收藏  举报

导航