守望者--AIR技术交流

标题: alchemy中定义多个方法 [打印本页]

作者: 破晓    时间: 2015-4-7 10:21
标题: alchemy中定义多个方法
  1. //Simple String Echo example
  2. //mike chambers
  3. //mchamber@adobe.com

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

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

  9. double sum(int num)
  10. {
  11.      int i;
  12.      double total=0;
  13.      for (i=1;i<=num;i++)
  14.      {
  15.          total += i;
  16.      }
  17.      return total;
  18. }

  19. //Method exposed to ActionScript
  20. //Takes a String and echos it
  21. static AS3_Val echo(void* self, AS3_Val args)
  22. {
  23.     //9 variable
  24.     int num;
  25.     double total;
  26.     char buf[256];

  27.     AS3_ArrayValue(args, "IntType", &num);
  28.      
  29.     total = sum(num);
  30.     sprintf(buf, "%lf", total);
  31.     return AS3_String(buf);
  32. }

  33. //try new function
  34. static AS3_Val returnTheSelf(void* self,AS3_Val args){
  35.     //initialize string to null
  36.     char* val = NULL;
  37.      
  38.     //parse the arguments. Expect 1.
  39.     //pass in val to hold the first argument, which
  40.     //should be a string
  41.     AS3_ArrayValue( args, "StrType", &val );
  42.      
  43.     //if no argument is specified
  44.     if(val == NULL)
  45.     {
  46.         char* nullString = "null";
  47.         //return the string "null"
  48.         return AS3_String(nullString);
  49.     }
  50.      
  51.     //otherwise, return the string that was passed in
  52.     return AS3_String(val);

  53. }

  54. //entry point for code
  55. int main()
  56. {
  57.     //define the methods exposed to ActionScript
  58.     //typed as an ActionScript Function instance
  59.     AS3_Val echoMethod = AS3_Function( NULL, echo );
  60.     AS3_Val returnTheSelfMethod=AS3_Function(NULL,returnTheSelf);

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

  64.     // Release
  65.     AS3_Release( echoMethod );
  66.     AS3_Release(returnTheSelfMethod);

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

  69.     // should never get here!
  70.     return 0;
  71. }
复制代码
如上:

  1. AS3_Val result = AS3_Object( "echo: AS3ValType,returnTheSelf:AS3ValType", echoMethod ,returnTheSelfMethod);
复制代码
此object中承载了两个方法:echo,returnTheSelf.


本文来自:http://www.cnblogs.com/176170847/archive/2011/01/27/1946206.html





欢迎光临 守望者--AIR技术交流 (http://www.airmyth.com/)