Game Development Reference
In-Depth Information
In other words, even though the ball itself is 18
18 pixels, the
rectangle returned by getRect is 36
36 because those are the
dimensions of the texture. Since this rectangle will be crucial in
performing hit detection in the engine, we employ a work-around
here;weoverridethe getRect method to instead return the rectan-
gle for the shine Sprite. It reflects the correct dimensions and will
provide the desired data.
override public function set x(value:Number):void
var difference:Number = value - x;
texture.x += difference;
if (texture.x > _textureOffsetX)
texture.x -= shine.width;
else if (texture.x
texture.x += shine.width;
super.x = value;
override public function set y(value:Number):void
var difference:Number = value - y;
texture.y += difference;
if (texture.y > _textureOffsetY)
texture.y -= shine.height;
else if (texture.y < -_textureOffsetY)
texture.y += shine.height;
super.y = value;
In addition to the override for getRect , we need to alter the
default behavior of the x and y accessors. Whenever the x and y
are set, we need to also update the position of the texture. This will
ensure that whenever the ball is moved, it will appear to roll in that
direction. The first step is to determine the distance the ball is
being moved and also move the texture by that amount. Then, we
test to see if the texture has moved outside of its
safe zone
reposition as necessary.
public function get vx():Number
return _vx;
public function set vx(value:Number):void
_vx = value;
Search Nedrilad ::

Custom Search