Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1502011
  • 博文数量: 3500
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 43870
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-03 20:31
文章分类

全部博文(3500)

文章存档

2008年(3500)

我的朋友

分类:

2008-05-04 23:26:11

一起学习

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

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


拉风 酷毙的HotRuby: 在一个JavaScript和Flash的虚拟机上跑Ruby拉风 酷毙的HotRuby: 在一个JavaScript和Flash的虚拟机上跑Ruby拉风 酷毙的HotRuby: 在一个JavaScript和Flash的虚拟机上跑Ruby拉风 酷毙的HotRuby: 在一个JavaScript和Flash的虚拟机上跑Ruby拉风 酷毙的HotRuby: 在一个JavaScript和Flash的虚拟机上跑Ruby拉风 酷毙的HotRuby: 在一个JavaScript和Flash的虚拟机上跑Ruby拉风 酷毙的HotRuby: 在一个JavaScript和Flash的虚拟机上跑Ruby拉风 酷毙的HotRuby: 在一个JavaScript和Flash的虚拟机上跑Ruby拉风 酷毙的HotRuby: 在一个JavaScript和Flash的虚拟机上跑Ruby拉风 酷毙的HotRuby: 在一个JavaScript和Flash的虚拟机上跑Ruby拉风 酷毙的HotRuby: 在一个JavaScript和Flash的虚拟机上跑Ruby拉风 酷毙的HotRuby: 在一个JavaScript和Flash的虚拟机上跑Ruby
阅读(238) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~