Game Development Reference
In-Depth Information
The first example, which you should be able to grasp easily by now, will create a
simple sprite and add it to the stage. Also, we register the mouse down and mouse
up events, and in the callbacks, we make calls to start and stop dragging the sprite.
Note the compiler options: -default-size 300 300 -default-background-color
0xFFFFFF
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
public class MouseDrag extends Sprite
{
// The sprite that will be moved around
private var m_s:Sprite = new Sprite();
public function MouseDrag()
{
m_s.graphics.beginFill(0x555555);
m_s.graphics.drawCircle(0, 0, 25);
m_s.graphics.endFill();
addChild(m_s);
m_s.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
m_s.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onMouseDown(e:MouseEvent):void {
m_s.startDrag();
}
private function onMouseUp(e:MouseEvent):void {
m_s.stopDrag();
}
}
}
Here is the listing for constraining the drag to the bounds.
In this example, we first create a rectangle called m_bounds , which we pass into the
startDrag() method. We also put another sprite b to simply highlight the bound area.
Note that something different happens when you move the mouse within the circle.
The cursor icon is changed and becomes a hand. There are two lines of code that
does that. Can you figure out which ones?
 
Search WWH ::




Custom Search