Skip to main content
July 29, 2013
Question

require some help making a random map.

  • July 29, 2013
  • 4 replies
  • 3248 views

1) When you click on the "Generate" button a 15x21 board of Tile symbols is created, with top-left corner at (10,10). Initially all tiles should be blank.

i need to somehow get that done.

here is my code so far, i have a lot of graphics, movie clips and a button relating to the script, although right now they are not important.

var ghosts:Array = new Array();

var timers:Array = new Array();

//======== ghosts and ghost behaviour =================//

var ghostHoriz=1;

for (var row = 0; row < 4; row++) {

          var g = new Ghost();

          var t:Timer=new Timer(400);

          g.timer=t;

          timers[row]=t;

          g.horiz=ghostHoriz;

          ghostHoriz*=-1;

          if (row==0||row==1) {

                    g.vert=1;

          }

          else {

                    g.vert=-1;

          }

          t.addEventListener(TimerEvent.TIMER, moveGhost);

          ghosts[row]=g;

          g.visible=false;

          addChild(g);

}

var ghostIndex=0;

function moveGhost(evt:TimerEvent) {

          var t=evt.currentTarget;

          var k=timers.indexOf(t);

          var g=ghosts as Ghost;

          if (g.visible) {

                    var r=g.row;

                    var c=g.col;

                    with (g) {

                              if (Math.random()<0.5) {

                                        if (((horiz > 0 && (c+horiz)<21)||(horiz < 0 && (c+horiz)>=0)) && (map[c+horiz]==0)) {

                                                  col+=horiz;

                                                  x+=horiz*20;

                                                  map[c+horiz]=2;

                                                  map=0;

                                        }

                                        else {

                                                  horiz*=-1;

                                        }

                              }

                              else {

                                        if ( (( vert > 0 && (r+vert)<15)||( vert < 0 &&(r+vert)>= 0)) && (map[r+vert]==0)) {

                                                  row+=vert;

                                                  y+=vert*20;

                                                  map[r+vert]=2;

                                                  map=0;

                                        }

                                        else {

                                                  vert*=-1;

                                        }

                              }

                    }

          }

}

// ================= Button behaviour ========== //

generate_btn.addEventListener(MouseEvent.CLICK, generate);

function generate(m:MouseEvent = null) {

          initMap();

          ghostIndex=0;

// other stuff

          displayMap();

}

var map:Array = new Array();

var tiles:Array = new Array();

function initMap(){

 

}

function displayMap() {

          for (var row = 0; row < 15; row++) {

                    for (var col = 0; col < 21; col++) {

                              var test=map[row][col];

                              tiles[row][col].x=10+col*20;

                              tiles[row][col].y=10+row*20;

                              switch (test) {

                                        case 2 :

                                                  g=ghosts[ghostIndex];

                                                  g.x=10+col*20;

                                                  g.y=10+row*20;

                                                  g.row=row;

                                                  g.col=col;

                                                  if (row==0) {

                                                            g.vert=1;

                                                  }

                                                  else {

                                                            g.vert=-1;

                                                  }

                                                  g.visible=true;

                                                  g.timer.start();

                                                  ghostIndex++;

                                                  break;

                              }

                    }

          }

}

bit long 😕😕

anyway when I basically need it so when you press generate it comes up with a random, how do i put this....it comes up with a random pacman sort of design. And everytime generate is clicked it changes, anyone know anything?

also note, with the code up there, if I do click generate i get this error...

Fonts should be embedded for any text that may be edited at runtime, other than text with the "Use Device Fonts" setting. Use the Text > Font Embedding command to embed fonts.

TypeError: Error #1010: A term is undefined and has no properties.

          at Pacmanscripting_fla::MainTimeline/displayMap()

          at Pacmanscripting_fla::MainTimeline/generate()

thanks in advance.

This topic has been closed for replies.

4 replies

Inspiring
July 29, 2013

This code is almost the same as a previous example but it randomizes tiles in the array - watch labels.

If you populate array with your tiles - you can be in business soon as far as the grid generation goes.

import flash.display.Sprite;

import flash.text.TextField;

import flash.text.TextFormat;

var tiles:Array;

var rows:int = 15;

var cols:int = 21;

getTiles();

drawGrid();

function getTiles():void

{

          tiles = [];

          var numTiles:int = rows * cols;

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

          {

                    tiles.push(drawTile(i));

          }

          shuffle(tiles);

}

function drawGrid():void

{

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

          {

                    var t:Sprite = tiles;

                    t.x = t.width * int(i / rows);

                    t.y = t.height * (i % rows);

                    addChild(t);

          }

}

function drawTile(index:int):Sprite

{

          var tileSide:int = 40;

          var color:uint = 0xffffff * Math.random();

          var tile:Sprite = new Sprite();

          tile.graphics.beginFill(color);

          tile.graphics.drawRect(0, 0, tileSide, tileSide);

 

          var label:TextField = new TextField();

          label.defaultTextFormat = new TextFormat("Arial", 11, 0xffffff - color, "bold");

          label.autoSize = "left";

          label.text = String(index);

          label.x = label.y = 4;

          tile.addChild(label);

          return tile;

}

function shuffle(a:Array):void

{

          for (var i:int = a.length - 1; i >= 0; i--)

          {

                    var r:int = Math.floor(Math.random() * (i + 1));

                    var t:Object = a;

                    a = a;

                    a = t;

          }

}

July 29, 2013

Hey this is really great so far, colour scheme is a little bright but i dont mind ! but i was wondering that if i am able to get the swf file, would you take a look at it and maybe help me out?

please ;-;...

Inspiring
July 29, 2013

Take a screenshot of how this puppy must look like and post it here.

Inspiring
July 29, 2013

Here is how to do that from previously populated array - watch tiles:Array (still just a concept):

import flash.display.Sprite;

import flash.text.TextField;

import flash.text.TextFormat;

var tiles:Array;

var rows:int = 15;

var cols:int = 21;

getTiles();

drawGrid();

function getTiles():void

{

          tiles = [];

          var numTiles:int = rows * cols;

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

          {

                    tiles.push(drawTile(i));

          }

}

function drawGrid():void

{

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

          {

                    var t:Sprite = tiles;

                    t.x = t.width * int(i / rows);

                    t.y = t.height * (i % rows);

                    addChild(t);

          }

}

function drawTile(index:int):Sprite

{

          var tileSide:int = 40;

          var color:uint = 0xffffff * Math.random();

          var tile:Sprite = new Sprite();

          tile.graphics.beginFill(color);

          tile.graphics.drawRect(0, 0, tileSide, tileSide);

 

          var label:TextField = new TextField();

          label.defaultTextFormat = new TextFormat("Arial", 11, 0xffffff - color, "bold");

          label.autoSize = "left";

          label.text = String(index);

          label.x = label.y = 4;

          tile.addChild(label);

          return tile;

}

Inspiring
July 29, 2013

Here is a conceptual blueprint on how to generate grid. Just paste code on timeline.

import flash.display.Sprite;

drawGrid();

function drawGrid():void

{

          var rows:int = 15;

          var cols:int = 21;

          var numTiles:int = rows * cols;

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

          {

                    var t:Sprite = tile;

                    t.x = t.width * int(i / rows);

                    t.y = t.height * (i % rows);

                    addChild(t);

          }

}

function get tile():Sprite

{

          var t:Sprite = new Sprite();

          t.graphics.beginFill(0xffffff * Math.random());

          t.graphics.drawRect(0, 0, 30, 30);

          return t;

}

July 29, 2013

erm...i figured out where to past it, but once that happens the whole thing goes multicoloured and checkered....which is not really what I wanted.

mines supposed to end up like this:

https://shuspace.shu.ac.uk/courses/1/55-4885-00L-A-20123/content/_4655248_1/referralsoln_example.swf

if you have anything, please reply, but thank you so far !!!!

Inspiring
July 29, 2013

Your example requires login.

kglad
Community Expert
Community Expert
July 29, 2013

you have a 15x21 grid and 4 ghosts are to be assigned to 4 grid tiles?