作者Friess (Fries)
看板Flash
标题[问题] 关於准确吸附目标
时间Mon Nov 21 18:40:20 2011
不好意思,把整个程式码PO上来
因为我真的试了好久好久好久好久好久
从晚上试到天亮,怎样弄就是不行
到底是发生什麽问题了
真的麻烦有心的大大帮小弟解决这个长久的烦恼
小弟在做的是资料结构的双向链结
就是一台车,有两个箭头(前&後),可以去"指向"其他的车
大概长这样 <-(XXX)->
然後当拖曳箭头去"指向"其他车的时候会吸附上去
简单说,就是当车子上的箭头去碰撞到其他的车子,箭头就会吸附到上面
可是小弟一直发生的问题
不管怎样碰撞,明明碰撞成功,却吸附到自己的车上去,而不是对方
var container:Sprite=new Sprite();//存车子
addChild(container);//容器放到舞台
new_btn.addEventListener(MouseEvent.CLICK,newbox);//产生车子的按钮
function newbox(e:MouseEvent):void {
var tty:MovieClip=new TTY();//元件库的车子
tty.x=125;//车子舞台座标
tty.y=50;
container.addChild(tty);//车子放到容器
tty.txt1.addEventListener(MouseEvent.MOUSE_DOWN,star);
tty.txt2.addEventListener(MouseEvent.MOUSE_DOWN,star);
tty.carbody0.addEventListener(MouseEvent.MOUSE_DOWN,star);
tty.carbody1.addEventListener(MouseEvent.MOUSE_DOWN,star);
tty.carbody2.addEventListener(MouseEvent.MOUSE_DOWN,star);
tty.txt1.addEventListener(MouseEvent.MOUSE_UP,onReleaseMouseUp);
tty.txt2.addEventListener(MouseEvent.MOUSE_UP,onReleaseMouseUp);
tty.carbody0.addEventListener(MouseEvent.MOUSE_UP,onReleaseMouseUp);
tty.carbody1.addEventListener(MouseEvent.MOUSE_UP,onReleaseMouseUp);
tty.carbody2.addEventListener(MouseEvent.MOUSE_UP,onReleaseMouseUp);
}
function onReleaseMouseUp(e:MouseEvent):void {
e.target.parent.stopDrag();
}
function star(e:MouseEvent):void {
e.target.parent.startDrag();
}
tty.dot1.addEventListener(MouseEvent.MOUSE_DOWN, onClcik1);//dot1车子前面的箭头
tty.dot1.addEventListener(MouseEvent.MOUSE_UP, upClcik1);
function onClcik1(e:MouseEvent) {
e.target.startDrag();
}
//以下是重点问题程式码(车子的箭头可以自动拉长.缩短,AS在箭头自己的影格内
应该不是这个的问题所以没PO出来)
function upClcik1(e:MouseEvent) {
e.target.stopDrag();
var child:MovieClip;
for (var i = 0; i < container.numChildren; i++) {
child=MovieClip(container.getChildAt(i));
if (e.target!=child) {
if (e.target.hitTestObject(child)) {
//车子前面的箭头碰到其他车子
e.target.x=child.x;
e.target.y=child.y;
}
}
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 111.240.129.149
1F:→ Friess:在问个问题,为何我var a:MovieClip=e.target; 系统都会错误 11/21 18:41
2F:→ Friess:1118: 静态类型 Object 的值以隐含方式强制转型成可能不相 11/21 18:42
3F:→ Friess:关的类型 flash.display:MovieClip。 11/21 18:42
4F:→ Friess:不好意思问题很多,小弟只是个新手,敬请见谅 11/21 18:42
※ 编辑: Friess 来自: 111.240.129.149 (11/21 19:01)
5F:→ sextitanic:因为 e.target 是 Object,所以要做转型的动作 11/22 03:16
6F:→ sextitanic:像用 MovieClip() 或 as MovieClip; 11/22 03:18