时间线上的影片剪辑与动作脚本中绘制的影片剪辑之间的碰撞检测
我正在制作一个程序,让用户用鼠标在屏幕上绘制形状。完成后,他们按下按钮即可完成。只允许用户在舞台的某个区域上绘制,为了在时间轴上控制它,我有一个影片剪辑,它占据了不允许用户绘制的所有空间。
当用户按下按钮时,我想要程序检查用户绘制的形状是否没有接触到动画剪辑。我希望它在不考虑影片剪辑边框的情况下执行此操作,因此我无法使用 hitTestObject()
这是我到目前为止所拥有的:
//------------------------------DRAW SHAPE------------------------------
private var shape:MovieClip = new MovieClip();
public function startDraw(e:MouseEvent):void {
shape.graphics.moveTo(mouseX, mouseY);
shape.graphics.lineStyle(4, 0x000000, 0.8);
stage.addEventListener(MouseEvent.MOUSE_MOVE, beginDraw);
}
public function beginDraw(e:MouseEvent):void {
shape.graphics.lineTo(mouseX, mouseY);
}
public function stopDraw(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, beginDraw);
}
//--------------------------CHECK FOR COLLISION---------------------------
public function bclick(e:MouseEvent):void {
if(cd(mcOnTimeline, shape)){
trace("HIT IT");
}else{
trace("DIDNT HIT");
}
}
public function cd(mc1:MovieClip, mc2:MovieClip):Boolean{
var mc1Bounds:Object = mc1.getBounds(mc1);
var mc2Bounds:Object = mc2.getBounds(mc1);
//return(mc1Bounds.intersects(mc2Bounds));
var mc1BmpD = new BitmapData(mc1Bounds.width, mc1Bounds.height, true, 0);
var mc2BmpD = new BitmapData(mc2Bounds.width, mc2Bounds.height, true, 0);
//mc1BmpD.draw(mc1Bounds);
//mc2BmpD.draw(mc2Bounds);
//return(mc1BmpD.intersects(mc2BmpD));
if(mc1BmpD.hitTest(new Point(mc1Bounds.x, mc1Bounds.y), 255, mc2BmpD, new Point(mc2Bounds.x, mc2Bounds.y),255)){
return true;
}
else{
return false;
}
注释掉的内容是我尝试使其工作的不同内容
I'm making a program that lets the user draw shapes on the screen with the mouse. When they're done they press a button to finish. The user is only allowed to draw on a certain area of the stage, to control this on the timeline i have a movie clip that takes up all the space the user isnt alowed to draw in.
When the user presses the button i want the programe to check that the shape the user drew isnt touching the moiveclip. I want it to do this without taking the border of the movieclip into account so i cant use hitTestObject()
heres what i have so far:
//------------------------------DRAW SHAPE------------------------------
private var shape:MovieClip = new MovieClip();
public function startDraw(e:MouseEvent):void {
shape.graphics.moveTo(mouseX, mouseY);
shape.graphics.lineStyle(4, 0x000000, 0.8);
stage.addEventListener(MouseEvent.MOUSE_MOVE, beginDraw);
}
public function beginDraw(e:MouseEvent):void {
shape.graphics.lineTo(mouseX, mouseY);
}
public function stopDraw(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, beginDraw);
}
//--------------------------CHECK FOR COLLISION---------------------------
public function bclick(e:MouseEvent):void {
if(cd(mcOnTimeline, shape)){
trace("HIT IT");
}else{
trace("DIDNT HIT");
}
}
public function cd(mc1:MovieClip, mc2:MovieClip):Boolean{
var mc1Bounds:Object = mc1.getBounds(mc1);
var mc2Bounds:Object = mc2.getBounds(mc1);
//return(mc1Bounds.intersects(mc2Bounds));
var mc1BmpD = new BitmapData(mc1Bounds.width, mc1Bounds.height, true, 0);
var mc2BmpD = new BitmapData(mc2Bounds.width, mc2Bounds.height, true, 0);
//mc1BmpD.draw(mc1Bounds);
//mc2BmpD.draw(mc2Bounds);
//return(mc1BmpD.intersects(mc2BmpD));
if(mc1BmpD.hitTest(new Point(mc1Bounds.x, mc1Bounds.y), 255, mc2BmpD, new Point(mc2Bounds.x, mc2Bounds.y),255)){
return true;
}
else{
return false;
}
The commented out stuff is different things ive tryed to get this working
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会一边画一边测试;当用户绘图时(按下鼠标按钮)测试鼠标 x,y 是否击中影片剪辑。如果是这样,请相应地设置一个标志。
I would test while drawing; when the user is drawing (mouse button down) test if the mouse x,y is hitting the movieclip. If so, set a flag accordingly.
根据 hitTest 形状的复杂性,您可能需要考虑使用 BitmapData.hitTest(),这应该更有效。查看 Mike Chamber 的博客文章主题。
Depending on the complexity of your hitTest shape you might want to consider using BitmapData.hitTest(), which should be more efficient. Check out Mike Chamber's blog post on the subject.