Skip to main content
Participant
November 5, 2013
Answered

button that randomizes location of objects,when clcked again del the old stuff & reputs stuff.help

  • November 5, 2013
  • 1 reply
  • 842 views

Hello world I'm trying to make a program that when I click the randomize button it randomly puts objects in a given area and if you hit the button again it should delete the objects and put on a new randomized pattern of objects.

need help!!!!

as of the moment I only have a "else is unexpected" error but I'm afraid my idea night not work help please!!!!

import flash.geom.Point;

import flash.events.MouseEvent;

var no:Number = 1

var scale:Number = new Number();

var treeList:Array = new Array();

var grass1List:Array = new Array();

var grass2List:Array = new Array();

var grass3List:Array = new Array();

var grass4List:Array = new Array();

var grass5List:Array = new Array();

rdm_btn.addEventListener(MouseEvent.MOUSE_UP,Random);

function Random (e:MouseEvent):void

{

          if (no < 3)

 

                    for (var p:Number = 0; p<500; p++)

                    {

                    var grass5:Grass5=new Grass5();

                    var tempPointGrass5:Point=new Point();

                    tempPointGrass5.x = Math.floor(Math.random() * 1000);

                    tempPointGrass5.y = Math.floor(Math.random() * 1000);

                    if (checkBoundary(tempPointGrass5.x,tempPointGrass5.y))

                    {

                              grass5.x = tempPointGrass5.x;

                              grass5.y = tempPointGrass5.y;

                              grass5.scaleX = grass5.scaleY =(tempPointGrass5.y/650);

                              grass5List.push(tempPointGrass5);

                              addChild(grass5);

                    }

                    else

                    {

                              p--;

                    }

          }

                    for (var q:Number = 0; q<500; q++)

                    {

                    var grass4:Grass4=new Grass4();

                    var tempPointGrass4:Point=new Point();

                    tempPointGrass4.x = Math.floor(Math.random() * 1000);

                    tempPointGrass4.y = Math.floor(Math.random() * 1000);

                    if (checkBoundary(tempPointGrass4.x,tempPointGrass4.y))

                    {

                              grass4.x = tempPointGrass4.x;

                              grass4.y = tempPointGrass4.y;

                              grass4.scaleX = grass4.scaleY =(tempPointGrass4.y/650);

                              grass4List.push(tempPointGrass4);

                              addChild(grass4);

                    }

                    else

                    {

                              q--;

                    }

          }

 

                    for (var v:Number = 0; v<500; v++)

                    {

                    var grass3:Grass3=new Grass3();

                    var tempPointGrass3:Point=new Point();

                    tempPointGrass3.x = Math.floor(Math.random() * 1000);

                    tempPointGrass3.y = Math.floor(Math.random() * 1000);

                    if (checkBoundary(tempPointGrass3.x,tempPointGrass3.y))

                    {

                              grass3.x = tempPointGrass3.x;

                              grass3.y = tempPointGrass3.y;

                              grass3.scaleX = grass3.scaleY =(tempPointGrass3.y/650);

                              grass1List.push(tempPointGrass3);

                              addChild(grass3);

                    }

                    else

                    {

                              v--;

                    }

          }

                    for (var t:Number = 0; t<500; t++);

                //ingore the enter code here

                    var grass2:Grass2=new Grass2();

                    var tempPointGrass2:Point=new Point();

                    tempPointGrass2.x = Math.floor(Math.random() * 1000);

                    tempPointGrass2.y = Math.floor(Math.random() * 1000);

                    if (checkBoundary(tempPointGrass2.x,tempPointGrass2.y))

                    {

                              grass2.x = tempPointGrass2.x;

                              grass2.y = tempPointGrass2.y;

                              grass2.scaleX = grass2.scaleY =(tempPointGrass2.y/650);

                              grass2List.push(tempPointGrass1);

                              addChild(grass2);

                    }

                    else

                    {

                              t--;

                    }

          }

                    for (var j:Number = 0; j<500; j++)

                    {

                    var grass1:Grass1=new Grass1();

                    var tempPointGrass1:Point=new Point();

                    tempPointGrass1.x = Math.floor(Math.random() * 1000);

                    tempPointGrass1.y = Math.floor(Math.random() * 1000);

                    if (checkBoundary(tempPointGrass1.x,tempPointGrass1.y))

                    {

                              grass1.x = tempPointGrass1.x;

                              grass1.y = tempPointGrass1.y;

                              grass1.scaleX = grass1.scaleY =(tempPointGrass1.y/650);

                              grass1List.push(tempPointGrass1);

                              addChild(grass1);

                    }

                    else

                    {

                              j--;

                    }

          }

                    for (var i:Number = 0; i<20; i++)

          {

                    var tree:Tree=new Tree();

                    var tempPointTree:Point=new Point();

                    tempPointTree.x = Math.floor(Math.random() * 1000);

                    tempPointTree.y = Math.floor(Math.random() * 1000);

                    if (checkBoundary(tempPointTree.x,tempPointTree.y))

                    {

                              tree.x = tempPointTree.x;

                              tree.y = tempPointTree.y;

                              tree.scaleX = tree.scaleY =(tempPointTree.y/650);

                              treeList.push(tempPointTree);

                              addChild(tree);

                    }

                    else

                    {

                              i--;

 

                    }

}

          no++

          no++

          no++

          else

{

          no--

          no--

          no--

          treeList.length = 0

          grass1List.length = 0

          grass2List.length = 0

          grass3List.length = 0

          grass4List.length = 0

          grass5List.length = 0

}

}

function checkBoundary(X:Number, Y:Number):Boolean

{

          if (X > 100 && X < 900 && Y > 220 && Y < 500)

          {

                    return true;

          }

          return false;

}

This topic has been closed for replies.
Correct answer kglad

Yes


// define your rectangle and call positionItemsF whenever you want to randomize/re-randomize the items' positions.

var r:Rectangle = new Rectangle(100,200,300,300);

var itemA:Array = [tree,grass1,grass2,grass3,grass4,grass5];

positionItemsF();

function positionItemsF():void{

    for(var i:int=0;i<itemA.length;i++){

         // if your items have top left reg point

        itemA.x = randomF(r.left,r.left+r.width-itemA.width);

        itemA.y = randomF(r.top,r.top+r.height-itemA.height);

    }

}

function randomF(n1:int,n2:int):int{

    if(n1<n2){

        return n1+Math.floor((n2-n1+1)*Math.random());

    } else {

        return n2+Math.floor((n1-n2)*Math.random());

    }

}

1 reply

kglad
Community Expert
Community Expert
November 5, 2013

that error means you have a mismatched curly bracket somewhere.

what shape is your "given area" and what objects do you want to position there?

J_DAWG1Author
Participant
November 5, 2013

My shape is a square and to make sure it's in the square I have the check boundry function and I'm trying to put objects (tree.grass1,grass2,grass3,grass4,grass5)

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
November 5, 2013

Yes


// define your rectangle and call positionItemsF whenever you want to randomize/re-randomize the items' positions.

var r:Rectangle = new Rectangle(100,200,300,300);

var itemA:Array = [tree,grass1,grass2,grass3,grass4,grass5];

positionItemsF();

function positionItemsF():void{

    for(var i:int=0;i<itemA.length;i++){

         // if your items have top left reg point

        itemA.x = randomF(r.left,r.left+r.width-itemA.width);

        itemA.y = randomF(r.top,r.top+r.height-itemA.height);

    }

}

function randomF(n1:int,n2:int):int{

    if(n1<n2){

        return n1+Math.floor((n2-n1+1)*Math.random());

    } else {

        return n2+Math.floor((n1-n2)*Math.random());

    }

}