Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 04-25-2007, 02:12 PM   #1
ShoulderOfOrion
Registered User
 
Join Date: Apr 2007
Posts: 9
Default 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
ShoulderOfOrion is offline   Reply With Quote
Old 04-25-2007, 02:27 PM   #2
xxneon
Super Moderator
 
xxneon's Avatar
 
Join Date: Dec 2006
Location: Amish Country, PA
Posts: 8,570
Send a message via ICQ to xxneon Send a message via AIM to xxneon Send a message via MSN to xxneon Send a message via Skype™ to xxneon
Default

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..
xxneon is offline   Reply With Quote
Old 04-25-2007, 02:35 PM   #3
ShoulderOfOrion
Registered User
 
Join Date: Apr 2007
Posts: 9
Default

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
ShoulderOfOrion is offline   Reply With Quote
Old 04-25-2007, 02:48 PM   #4
xxneon
Super Moderator
 
xxneon's Avatar
 
Join Date: Dec 2006
Location: Amish Country, PA
Posts: 8,570
Send a message via ICQ to xxneon Send a message via AIM to xxneon Send a message via MSN to xxneon Send a message via Skype™ to xxneon
Default

how are the tiles named as far as their instance names?
xxneon is offline   Reply With Quote
Old 04-25-2007, 02:52 PM   #5
infernosnow
Senior Member
 
Join Date: Jan 2007
Posts: 254
Default

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
infernosnow is offline   Reply With Quote
Old 04-25-2007, 02:54 PM   #6
ShoulderOfOrion
Registered User
 
Join Date: Apr 2007
Posts: 9
Default

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);
}
}
ShoulderOfOrion is offline   Reply With Quote
Old 04-25-2007, 02:59 PM   #7
ShoulderOfOrion
Registered User
 
Join Date: Apr 2007
Posts: 9
Default

Quote:
Originally Posted by infernosnow View Post
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!
ShoulderOfOrion is offline   Reply With Quote
Old 04-25-2007, 03:39 PM   #8
infernosnow
Senior Member
 
Join Date: Jan 2007
Posts: 254
Default

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.
infernosnow is offline   Reply With Quote
Old 04-25-2007, 03:58 PM   #9
ShoulderOfOrion
Registered User
 
Join Date: Apr 2007
Posts: 9
Default

Quote:
Originally Posted by infernosnow View Post
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
ShoulderOfOrion is offline   Reply With Quote
Old 04-25-2007, 04:02 PM   #10
infernosnow
Senior Member
 
Join Date: Jan 2007
Posts: 254
Default

post your fla
infernosnow is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with a Unique Array Sorting/Randomization Problem TheMediaBoy ActionScript 2.0 4 11-27-2008 09:37 AM
Problem with a for...in loop in a for...in loop VictoriaWelby ActionScript 2.0 2 04-09-2007 11:20 PM
help with multidimensional array roamingBobert ActionScript 2.0 9 10-03-2006 12:27 AM
problem creating mc instance names inside a function jasonyen ActionScript 2.0 10 12-08-2004 12:01 AM
trouble with variable getting lost whoo Simple Stuff (Newbies) 2 08-07-2004 05:52 PM


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

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.