您的位置: 新闻频道 Ruby新闻

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

 

 

 

$n = $native
$n.import "Box2D.Dynamics.*"
$n.import "Box2D.Collision.*"
$n.import "Box2D.Collision.Shapes.*"
$n.import "Box2D.Dynamics.Joints.*"
$n.import "Box2D.Dynamics.Contacts.*"
$n.import "Box2D.Common.Math.*"
$n.import "flash.events.*"
$n.import "flash.display.*"
$n.import "flash.text.*"
$n.import "General.*"
$n.import "TestBed.*"

class Box2D
  def initialize
    @curr_id = 1
    @curr_test = nil
    
    add_fps_counter
    add_sprite
    add_instructions_text
    add_about_text
    add_input_fix_sprite
    add_listener
  end

  def add_listener
    update = Proc.new{|evt|
      # clear for rendering
      @sprite.graphics.clear
      
      # toggle between tests
      if $n.Input.isKeyPressed 39 then # Right Arrow
        @curr_id = @curr_id + 1
        @curr_test = nil
      elsif $n.Input.isKeyPressed 37 then # Left Arrow
        @curr_id = @curr_id - 1
        @curr_test = nil
      # Reset
      elsif $n.Input.isKeyPressed 82 then # R
        @curr_test = nil
      end
      
      # if nil, set new test
      if nil == @curr_test then
        case @curr_id
        # Bridge
        when 0
          @curr_test = $n.TestBridge.new
        # Example
        when 1
          @curr_test = $n.TestExample.new
        # Ragdoll
        when 2
          @curr_test = $n.TestRagdoll.new
        # Compound
        when 3
          @curr_test = $n.TestCompound.new
        # Stack
        when 4
          @curr_test = $n.TestStack.new
        # Crank
        when 5
          @curr_test = $n.TestCrank.new
        # Pulley
        when 6
          @curr_test = $n.TestPulley.new
        # Gears
        when 7
          @curr_test = $n.TestGears.new
        # Wrap around
        else
          if @curr_id < 0 then
            @curr_id = 7
            @curr_test = $n.TestGears.new
          else
            @curr_id = 0
            @curr_test = $n.TestBridge.new
          end
        end
      end
      
      # update current test
      @curr_test.Update
      
      # Update input (last)
      $n.Input.update
      
      # update counter and limit framerate
      @fps_counter.update
      $n.FRateLimiter.limitFrame 30
    }

    $n._root.addEventListener $n.Event.ENTER_FRAME, update, false, 0, true
  end

  def add_fps_counter
    @fps_counter = $n.FpsCounter.new
    @fps_counter.x = 7
    @fps_counter.y = 5
    $n.Main.m_fpsCounter = @fps_counter
    $n._root.addChildAt @fps_counter, 0
  end
  
  def add_sprite
    @sprite = $n.Sprite.new
    $n.Main.m_sprite = @sprite
    $n._root.addChild @sprite

    @input = $n.Input.new @sprite
  end

  #Instructions Text
  def add_instructions_text
    instructions_text = $n.TextField.new

    instructions_text_format = $n.TextFormat.new "Arial", 16, 0xffffff, false, false, false
    instructions_text_format.align = $n.TextFormatAlign.RIGHT

    instructions_text.defaultTextFormat = instructions_text_format
    instructions_text.x = 140
    instructions_text.y = 4.5
    instructions_text.width = 495
    instructions_text.height = 61
    instructions_text.text = "Box2DFlashAS3 examples: \n'Left'/'Right' arrows to go to previous/next example. \n'R' to reset."
    $n._root.addChild instructions_text
  end

  # textfield pointer
  def add_about_text
    aboutTextFormat = $n.TextFormat.new "Arial", 16, 0x00CCFF, true, false, false
    aboutTextFormat.align = $n.TextFormatAlign.RIGHT

    about_text = $n.TextField.new
    about_text.defaultTextFormat = aboutTextFormat
    about_text.x = 434
    about_text.y = 71
    about_text.width = 200
    about_text.height = 30
    $n.Main.m_aboutText = about_text
    $n._root.addChild about_text
  end

  # Make a big invisible box to cover the stage so that input focus doesn't change when mousing over the textfields
  # (Please let me know if there's a better way to solve this problem) (:
  def add_input_fix_sprite
    inputFixSprite = $n.Sprite.new
    inputFixSprite.graphics.lineStyle 0,0,0
    inputFixSprite.graphics.beginFill 0,0
    inputFixSprite.graphics.moveTo -10000, -10000
    inputFixSprite.graphics.lineTo 10000, -10000
    inputFixSprite.graphics.lineTo 10000, 10000
    inputFixSprite.graphics.lineTo -10000, 10000
    inputFixSprite.graphics.endFill
    $n._root.addChild inputFixSprite
  end
end

Box2D.new
 

来自:www.rubyinside.com

评论 共 21 条 发表评论

zhaonjtu 2008-04-02 17:14
很好很强大啊!
zqjshiyingxiong 2008-04-02 09:12
关注,很有前途
riafan 2008-03-29 21:14
php和java也可以动态生成swf的
fkpwolf 2008-03-28 15:15
跟ruby没啥关系吧
zq0459 2008-03-28 12:17
是厉害,可是谁能说说它厉害在那里?
老熊 2008-03-28 11:57
NB,就是有点不稳定
williamy 2008-03-28 10:30
太酷了,非常棒的技术,非常有前途,肯定会能够市场,能够让世界变得更加丰富多彩,加上应用上ruby这个东西,我太喜欢了,我会去学习的,因为它实在是很棒

上面的话的前提是假设没有Flash Flex以及ruby不是日本人写的
499947785 2008-03-28 09:29
好像有时会坏掉,全都不动了
jccg17476 2008-03-28 09:21
太帅了
ltian 2008-03-28 08:48
直接使用AS不是更好吗?
wzw00001 2008-03-27 20:47
相当滴牛!!!
onex 2008-03-27 17:38
太cool了
bookong 2008-03-27 15:10
好玩!里面的东西都可以拽来拽去,呵呵
liusu 2008-03-27 14:46
我在想那些碰撞效果和计算会不会很难。。。。
import 2008-03-27 14:20
看起来很帅,里面都可以多继承,感觉就是个语法糖

发表评论

您还没有登录,请登录后发表评论

推荐圈子

JSF
成员: 776
访问: 35417