| 赞 | 274 |
| VIP | |
| 好人卡 | |
| 积分 | 496 |
| 经验 | |
| 最后登录 | 2024-6-21 |
| 在线时间 | 1919 小时 |
- 梦石
- 10
- 星屑
- 39550
- 在线时间
- 1919 小时
- 注册时间
- 2010-11-14
- 回帖
- 3214

|
发表于 2019-6-16 20:09:41
|
显示全部楼层
本帖最后由 KB.Driver 于 2019-6-16 20:13 编辑
瞎写的玩意,感觉有不少问题,不太追求完美就自己改改参数凑合着用吧
[pre lang="ruby"]Velocity = Struct.new(:x, :y)
class Sprite
#--------------------------------------------------------------------------
# ● 设置跳跃
# x_inc - X坐标增量
# y_inc - Y坐标增量
# duration - 总体时间
# top_inc - 跳跃高度(可选)
#--------------------------------------------------------------------------
def set_jump(x_inc, y_inc, duration, top_inc = [y_inc, 0].max + 20)
new_x = self.x + x_inc
new_y = self.y - y_inc
top_y = self.y - top_inc
set_jump_xy(new_x, new_y, top_y, duration)
end
def set_jump_xy(new_x, new_y, top_y, duration)
raise "set_jump: top_y should not be smaller than new_y" if top_y > new_y
t = duration / 2
ay = 2 * (top_y - self.y).abs / (t * t).to_f
vx = (new_x - self.x) / t.to_f
vy = (top_y - self.y).abs / t.to_f + 0.5 * ay * t
@v = Velocity.new(vx, vy)
@ay = ay
@t = 0
@new_x, @new_y, @top_y = new_x, new_y, top_y
@real_x, @real_y = self.x, self.y
@duration = duration
@jump_state = :up
end
def update_jump
return if !jumping?
@real_x += @v.x
@real_y -= @v.y
@v.y -= @ay
if @jump_state == :up
@real_y = [@real_y, @top_y].max
if @v.y <= 0 || @real_y == @top_y
#@v.y = 0
@jump_state = :down
end
else # down
@real_y = [@real_y, @new_y].min
end
self.x, self.y = @real_x, @real_y
if @t == @duration || self.y == @new_y
clear_jump
else
@t += 1
end
end
def clear_jump
@v = nil
@ay = nil
@t = nil
@new_x = @new_y = @top_y = nil
@real_x = @real_y = nil
@duration = nil
@jump_state = nil
end
def jumping?
!@jump_state.nil?
end
end[/pre]
Sprite#update_jump没有写进Sprite#update里面,需要你在对应场景的update中添加
一个使用范例(插入Main以上)
[pre lang="ruby"]sprite = Sprite.new
sprite.bitmap = Bitmap.new("Graphics/Characters/001-Fighter01")
sprite.src_rect.set(0, 0, sprite.bitmap.width / 4, sprite.bitmap.height / 4)
sprite.x = 640 / 2
sprite.y = 480 / 2
sprite.set_jump(50, 50, 30)
while sprite.jumping? do
sprite.update_jump
Graphics.update
end
sprite.set_jump(-50, -50, 30)
while sprite.jumping? do
sprite.update_jump
Graphics.update
end
[/pre]
gif:
|
|