Personally, I've always adored the hittest() function in actionscript 2.0
and I decided to make a simple implementation of it in ruby shoes. This is a
simple rough draft of my hittest function in action. It's the most basic of
collision detection. This should be able to get many of the people that like
programming games headed in the right direction.
Shoes.app(:width => 400, :height => 400, :resizable => false, :scroll =>
false){
background "#000000"

def hittest(obj1, obj2)
# We're pretending that obj2 is static
lowlft = obj2.left - obj1.width
higlft = obj2.left + obj2.width
lowtop = obj2.top - obj1.height
higtop = obj2.top + obj2.height
if obj1.left > lowlft and obj1.left < higlft and obj1.top > lowtop and
obj1.top < higtop then
return true
else
return false
end
end


para strong("The greyish circle will follow your mouse and when the greyish
circle hits the black circle, the text will change to Hitting!"), :stroke =>
"#FFFFFF", :size => 10, :top => 0, :left => 10
hitting = para "Not Hitting!", :stroke => "#FFFFFF", :size => 8, :top =>
400-30, :left => 10
m_chords = para "Mouse Coordinates!", :stroke => "#FFFFFF", :size => 8, :top
=> 400-20, :left => 10

circle1 = oval(:left => 100, :top => 100, :radius => 20, :center => true,
:stroke => "#FFFFFF", :fill => "#000000")
circle_mouse = oval(:radius => 20, :center => true, :stroke => "#FFFFFF",
:fill => "#9E9E9E")

animate(50){
if hittest(circle_mouse, circle1) == true then
hitting.text = "Hitting!"
else
hitting.text = "Not hitting!"
end

circle_mouse.left = mouse[1]
circle_mouse.top = mouse[2]
m_chords.text = "Left:" + mouse[1].to_s + " - Top: " + mouse[2].to_s
}
}

Reply via email to