我在制作这段代码时遇到了麻烦,我每天都很擅长编码和学习,如果这个问题看起来很愚蠢,那就很抱歉。
这是我到目前为止的地方:
a1从a3区域开始,当我将a1拖到a2时,它显示5,如果我拖回到a3,它显示0,所以它按预期工作
现在我挣扎着:
这是我的代码:
var myTextBox:TextField = new TextField();
var myTextBox2:TextField = new TextField();
addChild(myTextBox2);
myTextBox2.width = 200;
myTextBox2.height = 100;
myTextBox2.x = 100;
myTextBox2.y = 20;
a1.addEventListener(MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(e:MouseEvent):void
{
e.target.startDrag();
}
function drop(e:MouseEvent):void
{
stopDrag();
if (a1.hitTestObject(a2))
{
myTextBox.text = "5";
var r:String=String(uint(myTextBox.text)+uint(myTextBox2.text));
myTextBox2.text=r;
}
if (a1.hitTestObject(a3))
{
myTextBox.text = "5";
var r2:String=String(uint(myTextBox2.text)-uint(myTextBox.text));
myTextBox2.text=r2;
}
else
{
trace("No collision.");
}
}
我不认为它过于复杂,但我还没有知识可以使它发挥作用,任何帮助都将不胜感激!谢谢!
答案 0 :(得分:0)
好的,找到了一种方法,所以发布代码以防有人需要它,或者如果有人有更好的代码,总是想学习!谢谢!
添加了一个名为a4的可移动对象,以测试多个拖放对象
var myTextBox:TextField = new TextField();
var myTextBox2:TextField = new TextField();
var myTextBox3:TextField = new TextField();
addChild(myTextBox2);
myTextBox2.width = 200;
myTextBox2.height = 100;
myTextBox2.x = 100;
myTextBox2.y = 20;
a1.addEventListener(MouseEvent.MOUSE_DOWN, drag);
a1.addEventListener(MouseEvent.MOUSE_UP, drop);
a4.addEventListener(MouseEvent.MOUSE_DOWN, drag);
a4.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(e:MouseEvent):void
{
if (e.target.hitTestObject(a2))
{
e.target.startDrag();
myTextBox.text = "0";
myTextBox3.text = "5";
}
if (e.target.hitTestObject(a3))
{
e.target.startDrag();
myTextBox.text = "5";
myTextBox3.text = "0";
}
}
function drop(e:MouseEvent):void
{
stopDrag();
if (e.target.hitTestObject(a2))
{
var r:String=String(uint(myTextBox.text)+uint(myTextBox2.text));
myTextBox2.text=r;
}
if (e.target.hitTestObject(a3))
{
var r2:String=String(uint(myTextBox2.text)-uint(myTextBox3.text));
myTextBox2.text=r2;
}
}