我尝试了不同的方法来让玩家与物品碰撞:
Coin.each_bounding_circle_collision(@player) do |coin, player|
puts "coin collides with player"
end
Item.each_bounding_circle_collision(@player) do |item, player|
puts "item collides with player"
end
@player.each_bounding_circle_collision(Item) do |player, item|
puts "player collides with item"
end
@player.each_bounding_circle_collision(Coin) do |player, coin|
puts "player collides with coin"
end其中,尽管Item是Coin的父类,但只有第一个和最后一个有效(即我检查Coin的那些):
class Item < Chingu::GameObject
trait :timer
trait :velocity
trait :collision_detection
trait :bounding_circle, :scale => 0.8
attr_reader :score
def initialize(options = {})
super(options)
self.zorder = Z::Item
end
end
class Coin < Item
def setup
@animation = Chingu::Animation.new(:file => "media/coin.png", :delay => 100, :size => [14, 18])
@image = @animation.first
cache_bounding_circle
end
def update
@image = @animation.next
end
end由于我对Ruby的了解一般很低,我不能理解为什么这不能工作,也许我遗漏了一些明显的东西。任何帮助都将不胜感激。
(由于低声誉,我不能用'chingu‘来标记它,所以它在下一个最接近的东西下,'libgosu')
谢谢。
(来源:Rubyroids)
发布于 2011-11-30 00:01:10
不幸的是,Chingu只按类记录所有GameObject实例和GameObject实例,而不是按继承。Chingu在这里所做的是根据Item.all检查冲突,它是一个纯粹由Item实例组成的数组,而不是它的子类。检查所有Item实例的方法是:
@player.each_bounding_circle_collision(game_objects.of_class(Item)) do |player, item|
puts "player collides with item"
end但是,请注意,这是相当慢的,因为game_objects#of_class会遍历所有游戏对象,挑选出属于kind_of的对象?您想要的类(实际上与新版本的Array#grep相同,但可能更慢)。您可能希望每隔一段时间就记录一次物品实例列表,而不是每次都要检查碰撞,这取决于游戏中有多少对象。
https://stackoverflow.com/questions/8310542
复制相似问题