如何随机掉在特定位置的形状的立场 [英] How to randomly swap shapes' positions in specific locations

查看:200
本文介绍了如何随机掉在特定位置的形状的立场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HELP!我试图创建一个记忆游戏,我也不太清楚如何在随机设置X和Y位置形状的立场。他们不能重叠,这样他们必须在每个用户启动程序时间12不同的位置是随机的。

HELP! I'm trying to create a memory game and I'm not too sure how to randomize the shapes' positions in set x and y locations. They can't overlap so they must be randomized in 12 different locations every time the user starts the program.

推荐答案

试试:

function randomSort(a:*, b:*):Number
{
    if (Math.random() < 0.5) return -1;
    else return 1;
}

// Push 12 positions as new Point() in an array.
var positions:Array = [ new Point(12, 42), new Point(43, 56), new Point(43,87) ]; // ...add 12 positions
var mcs:Array = [mc1, mc2, mc3]; // ...add 12 mcs

positions.sort(randomSort);

// link randomized position to MovieClips:
for (var i:int = 0, l:int = positions.length; i < l, i++ ) {
    var mc:MovieClip = mcs[i];
    var point:Point = positions[i];
    mc.x = point.x;
    mc.y = point.y;
}

这篇关于如何随机掉在特定位置的形状的立场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆