**
how to make random position like this using AS3
**
**
**
Copy paste this actionscript in your Flash IDE
import fl.transitions.Tween
import fl.transitions.easing.*
import fl.transitions.TweenEvent
stop()
var centerX:Number = stage.stageWidth/2
var centerY:Number = stage.stageHeight/2
var gridContainer:MovieClip = new MovieClip()
var format:TextFormat = new TextFormat()
format.size = 30;
format.align = TextFormatAlign.CENTER
var arrMc:Array = new Array()
var arrPos:Array = new Array()
var arrNumber:Array = new Array()
makeGrid(3,3)
function makeGrid(nrCol:int, nrRow:int)
{
gridContainer= new MovieClip()
addChild(gridContainer)
var i:int = 0
var nrItem:int = nrCol * nrRow
var col:int = 0
var row:int = 0
var space:int = 6
var margin:int = 0
for (i; i < nrItem; i++)
{
var mc:MovieClip = new MovieClip()
var rect:Shape = makeShape(50,50,0x4099FF)
mc.addChild(rect)
mc.x = margin + col * (mc.width+space)
mc.y = margin + row * (mc.height+space)
var txt:TextField = makeText(rect.width,10,0xeeeeee,String(i))
txt.width = rect.width
txt.y = rect.y + 5
mc.addChild(txt)
gridContainer.addChild(mc)
arrMc.push(mc)
arrPos.push({"x":mc.x, "y":mc.y})
arrNumber.push(i)
col++;
if (col >= nrCol)
{
col = 0
row++
}
}
gridContainer.x = centerX - gridContainer.width/2
gridContainer.y = centerY - gridContainer.height/2
}
var btnRandom:MovieClip = new MovieClip()
addChild(btnRandom)
var rect:Shape = makeShape(250,50,0x888888)
btnRandom.addChild(rect)
var txt:TextField = makeText(250,4,0xFFFFFF,"RANDOM")
btnRandom.addChild(txt)
btnRandom.x = centerX - (btnRandom.width/2)
btnRandom.y = stage.stageHeight - 100
btnRandom.buttonMode = true;
btnRandom.mouseChildren = false;
btnRandom.addEventListener(MouseEvent.CLICK, randomize)
function randomize(e:MouseEvent):void
{
shuffleArray(arrNumber)
var i:int = 0
for(i; i<arrPos.length;i++)
{
goSwitch(arrMc[i],arrPos[arrNumber[i]]["x"],arrPos[arrNumber[i]]["y"])
}
}
function makeShape(w,h:int,color:uint):Shape
{
var rectangle:Shape = new Shape;
rectangle.graphics.beginFill(color)
rectangle.graphics.drawRect(0,0,w,h)
rectangle.graphics.endFill()
return rectangle;
}
function makeText(w,top:uint,color:uint,str:String):TextField
{
var txt:TextField = new TextField()
txt.defaultTextFormat = format
txt.text = str
txt.width = w
txt.y = top
txt.textColor = color
return txt
}
function shuffleArray(ar:Array)
{
for (var i:int = ar.length-1; i >=0; i--)
{
var randomIndex:int = Math.floor(Math.random() * i)
var tempValue = ar[randomIndex]
ar[randomIndex] = ar[i]
ar[i] = tempValue
}
}
function goSwitch(mc:MovieClip,endx,endy:int)
{
var myTweenx = new Tween(mc, "x", Strong.easeInOut, mc.x,endx, 1, true);
var myTweeny = new Tween(mc, "y", Strong.easeInOut, mc.y,endy, 1, true);
}
quiz.swf (4.5 KB)
:: Copyright KIRUPA 2024 //--