<%@ page contentType="text/html; charset=gb2312"%> 拉风+酷毙的HotRuby: 在一个JavaScript和Flash的虚拟机上跑Ruby
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
拉风+酷毙的HotRuby: 在一个JavaScript和Flash的虚拟机上跑Ruby
     发布者: 发布时间:2008-03-29

HotRuby是一个在Javascript和flash上的虚拟机上跑ruby代码并编译成opcode的项目。
你可以通过在网页上嵌入<script type=”text/ruby”>…</script>并在之间写ruby脚本。HotRuby会识别并编译为远程脚本然后由javascript和flash的虚拟机来执行,显示在页面上。这里演示一个demo,包括一个物理学应用,一组碰撞球一个速度测试。(在我机器上,使用HotRuby要比Ruby1.9快了78%)

 

quality="high" menu="false"
pluginspage="http://www.macromedia.com/go/getflashplayer"
type="application/x-shockwave-flash" width="600"
height="400">

 

 

Ruby代码复制代码
  1. $n = $native  
  2. $n.import "Box2D.Dynamics.*"  
  3. $n.import "Box2D.Collision.*"  
  4. $n.import "Box2D.Collision.Shapes.*"  
  5. $n.import "Box2D.Dynamics.Joints.*"  
  6. $n.import "Box2D.Dynamics.Contacts.*"  
  7. $n.import "Box2D.Common.Math.*"  
  8. $n.import "flash.events.*"  
  9. $n.import "flash.display.*"  
  10. $n.import "flash.text.*"  
  11. $n.import "General.*"  
  12. $n.import "TestBed.*"  
  13.   
  14. class Box2D   
  15.   def initialize   
  16.     @curr_id = 1   
  17.     @curr_test = nil  
  18.        
  19.     add_fps_counter   
  20.     add_sprite   
  21.     add_instructions_text   
  22.     add_about_text   
  23.     add_input_fix_sprite   
  24.     add_listener   
  25.   end  
  26.   
  27.   def add_listener   
  28.     update = Proc.new{|evt|   
  29.       # clear for rendering  
  30.       @sprite.graphics.clear   
  31.          
  32.       # toggle between tests  
  33.       if $n.Input.isKeyPressed 39 then # Right Arrow  
  34.         @curr_id = @curr_id + 1   
  35.         @curr_test = nil  
  36.       elsif $n.Input.isKeyPressed 37 then # Left Arrow  
  37.         @curr_id = @curr_id - 1   
  38.         @curr_test = nil  
  39.       # Reset  
  40.       elsif $n.Input.isKeyPressed 82 then # R  
  41.         @curr_test = nil  
  42.       end  
  43.          
  44.       # if nil, set new test  
  45.       if nil == @curr_test then  
  46.         case @curr_id  
  47.         # Bridge  
  48.         when 0   
  49.           @curr_test = $n.TestBridge.new  
  50.         # Example  
  51.         when 1   
  52.           @curr_test = $n.TestExample.new  
  53.         # Ragdoll  
  54.         when 2   
  55.           @curr_test = $n.TestRagdoll.new  
  56.         # Compound  
  57.         when 3   
  58.           @curr_test = $n.TestCompound.new  
  59.         # Stack  
  60.         when 4   
  61.           @curr_test = $n.TestStack.new  
  62.         # Crank  
  63.         when 5   
  64.           @curr_test = $n.TestCrank.new  
  65.         # Pulley  
  66.         when 6   
  67.           @curr_test = $n.TestPulley.new  
  68.         # Gears  
  69.         when 7   
  70.           @curr_test = $n.TestGears.new  
  71.         # Wrap around  
  72.         else  
  73.           if @curr_id < 0 then  
  74.             @curr_id = 7   
  75.             @curr_test = $n.TestGears.new  
  76.           else  
  77.             @curr_id = 0   
  78.             @curr_test = $n.TestBridge.new  
  79.           end  
  80.         end  
  81.       end  
  82.          
  83.       # update current test  
  84.       @curr_test.Update   
  85.          
  86.       # Update input (last)  
  87.       $n.Input.update   
  88.          
  89.       # update counter and limit framerate  
  90.       @fps_counter.update   
  91.       $n.FRateLimiter.limitFrame 30   
  92.     }   
  93.   
  94.     $n._root.addEventListener $n.Event.ENTER_FRAME, update, false, 0, true  
  95.   end  
  96.   
  97.   def add_fps_counter   
  98.     @fps_counter = $n.FpsCounter.new  
  99.     @fps_counter.x = 7   
  100.     @fps_counter.y = 5   
  101.     $n.Main.m_fpsCounter = @fps_counter  
  102.     $n._root.addChildAt @fps_counter, 0   
  103.   end  
  104.      
  105.   def add_sprite   
  106.     @sprite = $n.Sprite.new  
  107.     $n.Main.m_sprite = @sprite  
  108.     $n._root.addChild @sprite  
  109.   
  110.     @input = $n.Input.new @sprite  
  111.   end  
  112.   
  113.   #Instructions Text  
  114.   def add_instructions_text   
  115.     instructions_text = $n.TextField.new  
  116.   
  117.     instructions_text_format = $n.TextFormat.new "Arial", 16, 0xffffff, falsefalsefalse  
  118.     instructions_text_format.align = $n.TextFormatAlign.RIGHT   
  119.   
  120.     instructions_text.defaultTextFormat = instructions_text_format   
  121.     instructions_text.x = 140   
  122.     instructions_text.y = 4.5   
  123.     instructions_text.width = 495   
  124.     instructions_text.height = 61   
  125.     instructions_text.text = "Box2DFlashAS3 examples: \n'Left'/'Right' arrows to go to previous/next example. \n'R' to reset."  
  126.     $n._root.addChild instructions_text   
  127.   end  
  128.   
  129.   # textfield pointer  
  130.   def add_about_text   
  131.     aboutTextFormat = $n.TextFormat.new "Arial", 16, 0x00CCFF, truefalsefalse  
  132.     aboutTextFormat.align = $n.TextFormatAlign.RIGHT   
  133.   
  134.     about_text = $n.TextField.new  
  135.     about_text.defaultTextFormat = aboutTextFormat   
  136.     about_text.x = 434   
  137.     about_text.y = 71   
  138.     about_text.width = 200   
  139.     about_text.height = 30   
  140.     $n.Main.m_aboutText = about_text   
  141.     $n._root.addChild about_text   
  142.   end  
  143.   
  144.   # Make a big invisible box to cover the stage so that input focus doesn't change when mousing over the textfields  
  145.   # (Please let me know if there's a better way to solve this problem) (:  
  146.   def add_input_fix_sprite   
  147.     inputFixSprite = $n.Sprite.new  
  148.     inputFixSprite.graphics.lineStyle 0,0,0   
  149.     inputFixSprite.graphics.beginFill 0,0   
  150.     inputFixSprite.graphics.moveTo -10000, -10000   
  151.     inputFixSprite.graphics.lineTo 10000, -10000   
  152.     inputFixSprite.graphics.lineTo 10000, 10000   
  153.     inputFixSprite.graphics.lineTo -10000, 10000   
  154.     inputFixSprite.graphics.endFill   
  155.     $n._root.addChild inputFixSprite   
  156.   end  
  157. end  
  158.   
  159. Box2D.new  


(转载文章请保留出处:北天JAVA技术网(www.java114.com))
 
更多精彩文章:
满江红开放技术研究组织发布Seam 2.0中文文档正式版
满江红开放技术研究组织发布Seam 2.0中文文档正式版
Spring Java Configuration——用java代码来装配Spring
Spring Java Configuration——用java代码来装配Spring
RAP再次放入令人振奋的Demo
RAP再次放入令人振奋的Demo
 
最近评论:
        
鍥炲
        
鍥炲
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号