/    Sign up×
Community /Pin to ProfileBookmark

Custom 2D Array

Hello!

How do I create a custom 2D array that looks like the following code?
Checks the whole array for a random integer, according to that integer it places gem.

[CODE]my2DArray = [ R(), R(), R(), R(), R(), R() ],
[ R(), R(), R(), R(), R(), R() ],
[ R(), R(), R(), R(), R(), R() ],
[ R(), R(), R(), R(), R(), R() ],
[ R(), R(), R(), R(), R(), R() ],
[ R(), R(), R(), R(), R(), R() ]

for (var y = 0; y < 6; y++){
for (var x = 0; x < 6; x++) {
if (my2DArray[y][x] == 1) {
// place a gem here
}
}
}

function R() {
return Math.floor(Math.random() * (1 + 5 – 1)) + 1;
}[/CODE]

I have no idea how a class works, if someone could explain me how to make a custom class like “Gem” it would be awesome.

Thanks in Advance, AlmarM

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JonaAug 30.2010 — [font=arial]Hi AlmarM,

JavaScript does not have "classes." Everything in JavaScript is an object, including functions. Therefore, you would be working with objects in JavaScript rather than classes.

I'm not sure exactly what your question is, but I would love to help if you can elaborate a little bit on what you're looking for. I'm guessing that the "gems" you're referring to are coming from a Ruby background?[/font]
Copy linkTweet thisAlerts:
@AlmarMauthorAug 31.2010 — I already found what I need :]

Thanks though ?

This what I needed about the "2D Array":
var myLevel = new Array(6);
for (q = 0; q &lt; 6; q++) {
myLevel[q] = new Array(6);
}

for (var a = 0; a &lt; 6; a++) {
for (var s = 0; s &lt; 6; s++) {
myLevel[a][s] = R();
//alert("myLevel[" + a + "][" + s + "] = " + myLevel[a][s]);
}
}


If you can optimize it better, go ahead.

And this is what I wanted to know about the "classes":
function Gem(x, y, id, context) {
var _handle = new Image();
var _x = x;
var _y = y;
var _id = id;

<i> </i>this.GetX = function() {
<i> </i> return _x;
<i> </i>}

<i> </i>this.GetY = function() {
<i> </i> return _y;
<i> </i>}

<i> </i>this.GetID = function () {
<i> </i> return _id;
<i> </i>}

<i> </i>this.loadImage = function() {
<i> </i> _handle.onload = function() {
<i> </i> context.drawImage(_handle, _x, _y);
<i> </i> }

<i> </i> _handle.src = "gems/" + _id + ".png";
<i> </i>}
}
Copy linkTweet thisAlerts:
@JonaAug 31.2010 — [font=arial]I don't have much to add. Your 2D Array function seems all right; any optimization would probably be negligible in the context of today's JavaScript engines.

Your Gem function doesn't allow for private variables or methods, but I'm assuming that's a design choice rather than a flaw.

Glad you figured it out. Best of luck![/font]
×

Success!

Help @AlmarM spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...