声明:JavaEye新闻文章的版权属于JavaEye网站所有,严禁任何网站转载本文,否则必将追究法律责任!
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
fkpwolf 2008-03-28 15:15
zq0459 2008-03-28 12:17
老熊 2008-03-28 11:57
williamy 2008-03-28 10:30
上面的话的前提是假设没有Flash Flex以及ruby不是日本人写的
499947785 2008-03-28 09:29
jccg17476 2008-03-28 09:21
ltian 2008-03-28 08:48
wzw00001 2008-03-27 20:47
onex 2008-03-27 17:38
bookong 2008-03-27 15:10
liusu 2008-03-27 14:46
import 2008-03-27 14:20
wangxinpier 2008-03-27 14:05
passyt 2008-03-27 14:00
jnduan 2008-03-27 13:50
Quake Wang 2008-03-27 13:49
http://hotruby.accelart.jp/test-web/diy.html
庄表伟 2008-03-27 13:40