V
V
Valeriy19972015-08-14 11:05:07
C++ / C#
Valeriy1997, 2015-08-14 11:05:07

How to arrange objects in canvas?

I'm recently learning javascript. And even less canvas. I want to complete my dialogue quest) The map is generated there randomly for now, please tell me what to change in the code so that you can edit the map manually, in a Java script. (placement of squares with given coordinates) Here is the code that is responsible for random generation:

//генерация ландшафта
function createWorld()
{
  console.log('Creating world...');

  //создание пустоши
  for (var x=0; x < worldWidth; x++)
  {
    world[x] = [];

    for (var y=0; y < worldHeight; y++)
    {
      world[x][y] = 0;
    }
  }

// раставляем скалы случайным образом
  for (var x=1; x < worldWidth; x++)
  {
    for (var y=1; y < worldHeight; y++)
    {
      if (Math.random() > 0.75)
      world[x][y] = 0;
    }
  }

Who in the subject please help or throw off the link where to read the project

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sumor, 2019-02-07
@CiSharper

Better this way:

class Arrays<T>
    { 
        public T GetValue(int k)
        {
            if (k < 0 || k > array.Length)
                throw new IndexOutOfRanfeException();
            return (array[k]);            
        }

Worse like this:
class Arrays<T> where T : class
    { 
        public T GetValue(int k)
        {
            if (k < 0 || k > array.Length)
                return null;
            return (array[k]);            
        }

Even worse is this:
class Arrays<T> 
    { 
        public T GetValue(int k)
        {
            if (k < 0 || k > array.Length)
                return default(T);
            return (array[k]);            
        }

N
Nikita Kirakosyan, 2019-02-07
@Noomank

Why do you need to return -1 array cell? Do you know programming theory? Are you sure this is the approach you need? In my experience, I have never seen an array start with -1. To return array[-1] to you, you need exactly that. Perhaps I misunderstood the question, so we can wait for an answer from colleagues.
PS
If nevertheless to answer you without all these lectures, then, most likely, in any way. As I said, all arrays start either from 0 or from 1 (if I'm not confusing anything). Well, in general, the answer on the surface is no way. You can't bring back something that doesn't exist. If the index is outside, then this cell does not exist, therefore - no way.
My verdict is no.

V
Vitaly Inchin ☢, 2015-08-14
@Valeriy1997

javascript, render, objects

Are you learning to write even less?
All I can offer you is the JS Tutorial , and then the Canvas Tutorial , and only then the quest.

V
Valeriy1997, 2015-08-14
@Valeriy1997

Almost all the code is already there. Now the world is randomly generated. The whole canvas is a space that you can walk on, then rocks (obstacles) randomly. The problem is to set the coordinates of the rocks with the script (no complicated map editor) just set the position of the rocks (the space that the Persian cannot walk on) I thought it would be enough to edit this part code : for (var x=1; x < worldWidth; x++) { for (var y=1; y < worldHeight; y++) { if (Math.random() > 0.75) world[x][y] = 0; } } and instead of Math.random set the coordinates

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question