Skip to main content
April 21, 2012
Answered

core function for game not fully working

  • April 21, 2012
  • 1 reply
  • 769 views

I am using code that generates words onto a map from an array. A function stops the words from being deployed in an area of a rectangle called block. However it does not work, the functions stops the player from entering but placing the words from the array seem to still deploy at random but not using the function to take consideration to the blocks...

Any help much appreciated!!

The code i am using

public function placeWords()

                    {

                    objects = new Array();

                    var goodObjects:Array = ["VerbObject1","VerbObject2","VerbObject3","VerbObject4",

                                                                                                                         "VerbObject5","VerbObject6","VerbObject7","VerbObject8",

                                                                                                                         "VerbObject9","VerbObject10",];

                    var badObjects:Array = ["NounObject1","NounObject2","NounObject3","NounObject4",

                                                                                "NounObject5","NounObject6","NounObject7","NounObject8",

                                                                                "NounObject9","NounObject10"];

                              for (var i:int=0; i<numWordObjects; i++)

                              {

                              while (true)

                              {

                                        // random location

                                        var x:Number = Math.floor(Math.random() * mapRect.width) + mapRect.x;

                                        var y:Number = Math.floor(Math.random() * mapRect.height) + mapRect.y;

                                        // check all blocks to see if it is over any

                                        var isOnBlock:Boolean = false;

                                        for (var j:int=0; j<blocks.length; j++)

                                        {

                                                  if (blocks.hitTestPoint(x + gamesprite.x,y + gamesprite.y))

                                                  {

                                                            isOnBlock = true;

                                                            break;

                                                            trace(isOnBlock);

                                                  }

                                        }

 

                                                  // not over any, so use location

                                                  if (!isOnBlock)

                                                  {

 

                                                            if (Math.random() < .5)

                                                            {

                                                                      var r:int = Math.floor(Math.random() * goodObjects.length);

                                                                      var classRef:Class = getDefinitionByName(goodObjects) as Class;

                                                                      var newObject:MovieClip = new classRef();

                                                                      newObject.typestr = "good";

                                                            }

                                                            else

                                                            {

                                                                      r = Math.floor(Math.random() * badObjects.length);

                                                                      classRef = getDefinitionByName(badObjects) as Class;

                                                                      newObject = new classRef();

                                                                      newObject.typestr = "bad";

                                                            }

 

                                                            newObject.x = Math.floor(Math.random() * mapRect.width) + mapRect.x;

                                                            newObject.y = Math.floor(Math.random() * mapRect.height) + mapRect.y;

                                                            gamesprite.addChild(newObject);

                                                            objects.push(newObject);

                                                            break;

                                                            trace(gamesprite.stage);

                                                            trace(objects);

 

                                                  }

                                        }

                              }

                    }

This topic has been closed for replies.
Correct answer kglad

if newobject is not supposed to be placed on a block, you probably want to assign its x,y to be the same x,y generated just below your //random location comment.

p.s.  remove that while(true) loop.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
April 21, 2012

if newobject is not supposed to be placed on a block, you probably want to assign its x,y to be the same x,y generated just below your //random location comment.

p.s.  remove that while(true) loop.

April 21, 2012

the while true though is needed to keep looping the random location loop until its found a location without a block on it??

April 21, 2012

fixed, it was like you said assigning the x and y values which i changed to be xW and yW then modifying the hitTestPoint to include them. while true ended up staying as if not the location loop only runs once therefore only displaying 1 word.

thanks for the advice.