本帖最后由 TKCB 于 2019-12-2 09:04 编辑
[Actionscript3] 纯文本查看 复制代码 import flash.events.MouseEvent;
//数学题
var speedA:Number = pool.width / 300;
trace("speedA:"+speedA);
var speedB:Number = - pool.width / 320;
trace("speedB:"+speedB);
B.x = pool.width + pool.x - B.width;
var n:int = 1;//记录相遇
var j:int = 2;
var h:int=2;
var meet:Boolean = false;
ta.text = "我是乙\n";
ta2.text = "我是甲\n";
btStart.addEventListener(MouseEvent.CLICK,onStart);
btRestart.addEventListener(MouseEvent.CLICK,onRestart);
btStart.visible = true;
btStop.visible = false;
function onStart(evt:MouseEvent):void
{
A.addEventListener(Event.ENTER_FRAME, fl_AnimateHorizontally);
B.addEventListener(Event.ENTER_FRAME, fl_AnimateHorizontallyB);
btStart.visible = false;
btStop.visible = true;
}
function onRestart(evt:MouseEvent):void
{
A.x = 0;
B.x = pool.width + pool.x - B.width;
ta.text = "我是乙\n";
ta2.text = "我是甲\n";
n = 1;
j = 2;
meet = false;
btStart.visible = true;
}
btStop.addEventListener(MouseEvent.CLICK,onStop);
function onStop(evt:MouseEvent):void
{
A.removeEventListener(Event.ENTER_FRAME, fl_AnimateHorizontally);
B.removeEventListener(Event.ENTER_FRAME, fl_AnimateHorizontallyB);
btStop.visible = false;
btStart.visible = true;
}
function fl_AnimateHorizontally(event:Event)
{
if (A.x<(pool.width+pool.x-A.width)&&(A.x>pool.x||A.x==0))
{
A.x += speedA;
}
else
{
speedA = - speedA;
A.x += speedA;
ta2.appendText("甲:我游第"+h+"次。\n");
h++;
}
}
function fl_AnimateHorizontallyB(event:Event)
{
var k:Number = pool.width + pool.x - B.width;
if ((B.x<k||B.x==k)&&(B.x>pool.x||B.x==0))
{
B.x += speedB;
}
else
{
speedB = - speedB;
B.x += speedB;
ta.appendText("乙:我游第"+j+"次。\n");
j++;
}
if (B.hitTestObject(A) && meet == false)
{
meet = true;
ta.appendText("我和甲第"+n+"次相遇\n");
ta2.appendText("我和乙第"+n+"次相遇\n");
n++;
}
else
{
if (! B.hitTestObject(A))
{
meet = false;
}
}
} |