AS3中限制显示对象的拖动坐标
如何根据创建对象的类中的父对象或舞台引用显示对象的坐标?
本质上,当我从自定义类创建新的精灵对象并将其添加到显示列表时,我想在自定义类中包含代码,以将拖动坐标限制到舞台或舞台的一部分。
//Frame Script
import Swatch;
var test:Sprite = new Swatch();
addChild(test);
___________________
//Custom Class
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Swatch extends Sprite
{
public function Swatch()
{
init();
}
private function init():void
{
var swatchObject:Sprite = new Sprite();
swatchObject.graphics.beginFill(0x0000FF, 1);
swatchObject.graphics.drawRect(100, 100, 150, 150);
swatchObject.graphics.endFill();
swatchObject.addEventListener(MouseEvent.MOUSE_DOWN, onDrag, false, 0, true);
swatchObject.addEventListener(MouseEvent.MOUSE_UP, onDrop, false, 0, true);
this.addChild(swatchObject);
}
private function onDrag(evt:MouseEvent):void
{
evt.target.startDrag();
//how to limit it's dragability to the Stage?
}
private function onDrop(evt:MouseEvent):void
{
evt.target.stopDrag();
}
}
}
how can i reference a display object's coordinates according to it's parent object or stage from within the class that creates the object?
essentially when i create a new sprite object from a custom class and add it to the display list, i'd like to include code within the custom class that limits the drag coordinates to the stage, or a section of the stage.
//Frame Script
import Swatch;
var test:Sprite = new Swatch();
addChild(test);
___________________
//Custom Class
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Swatch extends Sprite
{
public function Swatch()
{
init();
}
private function init():void
{
var swatchObject:Sprite = new Sprite();
swatchObject.graphics.beginFill(0x0000FF, 1);
swatchObject.graphics.drawRect(100, 100, 150, 150);
swatchObject.graphics.endFill();
swatchObject.addEventListener(MouseEvent.MOUSE_DOWN, onDrag, false, 0, true);
swatchObject.addEventListener(MouseEvent.MOUSE_UP, onDrop, false, 0, true);
this.addChild(swatchObject);
}
private function onDrag(evt:MouseEvent):void
{
evt.target.startDrag();
//how to limit it's dragability to the Stage?
}
private function onDrop(evt:MouseEvent):void
{
evt.target.stopDrag();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您想做的事情有一些本机支持。
startDrag()
接受一个矩形作为参数,该参数限制可以发生拖动的区域。希望有帮助,
泰勒。
There is some native support for what you want to do.
startDrag()
accepts a rectangle as a parameter which restricts the region in which the drag can take place.Hope that helps,
Tyler.