A sprite has a position and a velocity
scene2d :: Sprite :: defaultinit
scene2d :: Sprite :: set_velocity
Update of vx and vy toward a given angle and magnitudecore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
scene2d :: Sprite :: defaultinit
core :: Object :: defaultinit
scene2d :: LiveObject :: defaultinit
scene2d :: LiveObject :: draw
Redefine this method to asks how to draw on a viewscene2d :: LiveObject :: exists
Controls whetherupdate' and
draw' are automatically called by `LiveGroup'
scene2d :: LiveObject :: exists=
Controls whetherupdate' and
draw' are automatically called by `LiveGroup'
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: output_class_name
Display class name on stdout (debug only).scene2d :: Sprite :: set_velocity
Update of vx and vy toward a given angle and magnitude
# The basic atomic living and moving object.
#
# A sprite has a position and a velocity
class Sprite
super LiveObject
# x coordinate of the center point
var x: Int = 0 is writable
# y coordinate of the center point
var y: Int = 0 is writable
# width of the sprite
var width: Int = 100 is writable
# height of the sprite
var height: Int = 100 is writable
# X coordinate of left side.
fun left: Int do return x - width/2
# X coordinate of right side.
fun right: Int do return x + width/2
# Y coordinate of top.
fun top: Int do return y - height/2
# Y coordinate of bottom.
fun bottom: Int do return y + height/2
# x velocity (applied by `update')
var vx: Int = 0 is writable
# y velocity (applied by `update')
var vy: Int = 0 is writable
redef fun update
do
self.x += self.vx
self.y += self.vy
end
redef fun draw(view) do view.draw_sprite(self)
# Is self overlaps (or contains) an other sprite
# `x', `y', `width', and `height' of both sprites are considered
fun overlaps(other: Sprite): Bool
do
return self.right > other.left and self.left < other.right and self.bottom > other.top and self.top < other.bottom
end
# Return the current angle of velocity
# Often used to rotate the displayed image with the correct angle
fun velocity_angle: Float
do
return atan2(self.vx.to_f, -self.vy.to_f)
end
# Return the angle to target an other sprite
fun angle_to(target: Sprite): Float
do
return atan2((target.x-self.x).to_f, (self.y-target.y).to_f)
end
# Update of vx and vy toward a given angle and magnitude
fun set_velocity(angle: Float, maginude: Int)
do
var magf = maginude.to_f
self.vx = (angle.sin * magf).to_i
self.vy = (angle.cos * -magf).to_i
end
end
lib/scene2d/scene2d.nit:33,1--105,3