Home Tutorials Forums Articles Blogs Movies Library Employment Press

 ActionScript.org Forums 2D Array help
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

 04-25-2007, 02:12 PM #1 ShoulderOfOrion Registered User   Join Date: Apr 2007 Posts: 9 2D Array help Hi, I'm new here, quite new to using Actionscript as well, but I need to create a tile based map using a 2d array. Its for a Pacman style game I have tried to follow the tutorial on tonypa's site and so am using the same set up as that. I currently have the basic background along with the walls, however I am struggling to work out the code to randomly place pills onto the map. I know I need a FOR loop with a nested IF statement (to make sure pills arent placed over walls). To make things slightly more complicated, all of the tiles are stored in one movie clip, frame 1 is walls, 2 is walkable, 3 is pills. I just have no idea of the code i need to use. I could do it if the placing didn't need to be random, i.e add to the array I have written. So far I have the following (its a 30 x 30 array) for (n = 0; n < maxPills; n++) randx = Math.random()*30; randy = Math.random()*30 ; if (map[randx][randy] != tile.frame2) map[randx][randy] = tile.frame3; else n--; It doesn't work so I hope someone can help. Thanks
 04-25-2007, 02:27 PM #2 xxneon Super Moderator     Join Date: Dec 2006 Location: Amish Country, PA Posts: 8,570 map[randx][randy] what is the value that is stored at that location.. a movieclip instance?? true/false??? a number??? till i know what is in the array i dont know what to compare in the for loop.. to determine whether a pill can go there or not..
 04-25-2007, 02:35 PM #3 ShoulderOfOrion Registered User   Join Date: Apr 2007 Posts: 9 map[randx][randy] is a number which refers to a frame of a movieclip. The number 2 would refer to the 2nd frame of the tile movieclip. So I should probably change the code to: if (map[randx][randy] != 2) map[randx][randy] =3; This should mean that if the map is not displaying a wall tile then it will display an item in that spot
 04-25-2007, 02:48 PM #4 xxneon Super Moderator     Join Date: Dec 2006 Location: Amish Country, PA Posts: 8,570 how are the tiles named as far as their instance names?
 04-25-2007, 02:52 PM #5 infernosnow Senior Member   Join Date: Jan 2007 Posts: 254 your Math.random()*30 code is going to return a float number. Frames are whole numbers. You need to round it with Math.round(). Also, you said that your wall tile was in frame 1. That means your if statement should look like this: ActionScript Code: `if (map[randx][randy] != 1)` instead of != 2
 04-25-2007, 02:54 PM #6 ShoulderOfOrion Registered User   Join Date: Apr 2007 Posts: 9 Here are a couple of examples of how I have declared the tiles. I have no idea how it works Im afraid, just was on the tonypa website and having no idea how to make tiles (and getting no help from my tutor) i just decided to use the code game={tileW:30, tileH:30}; game.TileClass = function () {}; game.TileClass.prototype.walkable=false; game.TileClass.prototype.frame=20; game.Tile0= function () {}; game.Tile0.prototype.__proto__ = game.TileClass.prototype; game.Tile0.prototype.walkable=true; game.Tile0.prototype.frame=1; game.Tile1= function () {}; game.Tile1.prototype.__proto__ = game.TileClass.prototype; game.Tile1.prototype.walkable=false; game.Tile1.prototype.frame=2; they all refer to a movieclip named "tile" and are attached to the scene using the following code: for (var i = 0; i < mapHeight; ++i) { for (var j = 0; j < mapWidth; ++j) { var name = "t_"+i+"_"+j; game[name]= new game["Tile"+map[i][j]]; game.clip.attachMovie("tile", name, i*100+j*2); game.clip[name]._x = (j*game.tileW); game.clip[name]._y = (i*game.tileH); game.clip[name].gotoAndStop(game[name].frame); } }
04-25-2007, 02:59 PM   #7
ShoulderOfOrion
Registered User

Join Date: Apr 2007
Posts: 9

Quote:
 Originally Posted by infernosnow your Math.random()*30 code is going to return a float number. Frames are whole numbers. You need to round it with Math.round(). Also, you said that your wall tile was in frame 1. That means your if statement should look like this: ActionScript Code: `if (map[randx][randy] != 1)` instead of != 2

I changed it to Math.round() and now my character won't move at all. Now im confused!

 04-25-2007, 03:39 PM #8 infernosnow Senior Member   Join Date: Jan 2007 Posts: 254 Think about what this is saying: ActionScript Code: ```if (map[randx][randy] != 2) map[randx][randy] =3;``` "if the number in my map array is a wall or a pill, make it a pill." That's what the above code does. That would make it possible for a pill to be placed where a wall is supposed to be. But if you have it != 1, you're saying, "if the number in my map array is walkable or is a pill, make it a pill. This is because 1 is your wall.
04-25-2007, 03:58 PM   #9
ShoulderOfOrion
Registered User

Join Date: Apr 2007
Posts: 9

Quote:
 Originally Posted by infernosnow Think about what this is saying: ActionScript Code: ```if (map[randx][randy] != 2) map[randx][randy] =3;``` "if the number in my map array is a wall or a pill, make it a pill." That's what the above code does. That would make it possible for a pill to be placed where a wall is supposed to be. But if you have it != 1, you're saying, "if the number in my map array is walkable or is a pill, make it a pill. This is because 1 is your wall.
I've tried this as well, but it still isn't working. I really don't understand what is wrong

 04-25-2007, 04:02 PM #10 infernosnow Senior Member   Join Date: Jan 2007 Posts: 254 post your fla

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home ActionScript Forums Group     ActionScript 3.0     ActionScript 2.0     ActionScript 1.0 (and below)     Simple Stuff (Newbies) Flash General Questions     Flash 10 General Questions     Flash 9 General Questions     Flash 8 General Questions     Other Flash General Questions Flex     Flex 2, 3 & 4     Flex 1 Extensions and Plugins     Components     JSFL - Extending Flash Desktop, Mobile and non-browser Environments     AIR (Apollo)     FlashLite / Portable Devices Development     Projectors and CDs Supporting Technologies     HTML and JavaScript     haXe     Server-Side Scripting     Flash Remoting     Flash Media Server General     Best Practices     Gaming and Game Development     Animation and Effects     Flashants Support Forum Community Boards     General Chat     Just for Kicks Challenges     Detention Flash In Action     Site Check     Cool Sites     Widgets Decommissioned     Projects and Positions CMS Forums     Announcements Board     Content Postings / Updates     Product Review Requests     CMS Technical Questions     Process Questions     Collaboration & Suggested Articles

 Similar Threads Thread Thread Starter Forum Replies Last Post TheMediaBoy ActionScript 2.0 4 11-27-2008 09:37 AM VictoriaWelby ActionScript 2.0 2 04-09-2007 11:20 PM roamingBobert ActionScript 2.0 9 10-03-2006 12:27 AM jasonyen ActionScript 2.0 10 12-08-2004 12:01 AM whoo Simple Stuff (Newbies) 2 08-07-2004 05:52 PM

All times are GMT. The time now is 09:07 AM.

///