Actionscript 3 如何通过AS3在3个对象之间交换(随机)位置?
天真的方法:Actionscript 3 如何通过AS3在3个对象之间交换(随机)位置?,actionscript-3,Actionscript 3,天真的方法: //array var boxs: Array = new Array boxs[0] = [b1.x = 307.95 , b1.y = 202] boxs[1] = [b2.x = 233.95 , b2.y = 202] boxs[2] = [b3.x = 159.95 , b3.y = 202] //varable var oldg:Number = 0 //random number oldg = Number(Math.floor(Math.random
//array
var boxs: Array = new Array
boxs[0] = [b1.x = 307.95 , b1.y = 202]
boxs[1] = [b2.x = 233.95 , b2.y = 202]
boxs[2] = [b3.x = 159.95 , b3.y = 202]
//varable
var oldg:Number = 0
//random number
oldg = Number(Math.floor(Math.random()*boxs.length))
public函数Main()
{
常量数组:数组=[1,2,3,4,5,6,7];
跟踪(阵列);
// 1,2,3,4,5,6,7
swaptworandomements(数组);
跟踪(阵列);
// 1,2,3,6,5,4,7
}
私有函数swaptworAndomeElements(输入:数组):void
{
常量索引:数组=[];
对于(变量i:int=0;i
X和Y在哪里position@ActionScriptstudent那你能修改一下你的问题吗?现在还不清楚“X Y位置在哪里”这个问题下你是什么意思。根据一般问题,您需要交换数组中的两个随机位置。在代码示例中,您创建了一个数字数组,其中没有X或Y坐标。如果要对boxs数组使用交换功能,只需调用:swaptworandomeelements(boxs)
@ActionScriptstudent最简单的方法是将初始位置定义为点数组:const positions:array=[新点(20,20),…等]
。然后只需使用交换函数来洗牌这个数组。重新排列“位置”数组后,需要按适当顺序将此位置应用于剪辑。对于动画,你可以使用像TweenLite这样的软件包。@ActionScriptstudent我认为在这里要求这个是错误的。我建议去
public function Main()
{
const array:Array = [1,2,3,4,5,6,7];
trace(array);
// 1,2,3,4,5,6,7
swapTwoRandomElements(array);
trace(array);
// 1,2,3,6,5,4,7
}
private function swapTwoRandomElements(input:Array):void
{
const indices:Array = [];
for (var i:int = 0; i < input.length; i++)
{
indices.push(i);
}
const indexFirst:int = indices[int(Math.random() * indices.length)];
indices.splice(indexFirst, 1);
const indexSecond:int = indices[int(Math.random() * indices.length)];
indices.splice(indexSecond, 1);
const tmp:* = input[indexFirst];
input[indexFirst] = input[indexSecond];
input[indexSecond] = tmp;
}