set position in Array

0 favourites
  • 3 posts
From the Asset Store
Easy to use shop and inventroy populated by an array!
  • Hi,

    well today i have another little question.

    i will do a kind of a "Battelship" game. So i ha a Grid. I can also drag around the stuff i wont, but now is my problem, how i can save the "position" in a array?

    Its more less like this:

    1 2 3 4 5

    2 x x x x

    3 x x x x

    4 x x x x

    5 x x x x

    so now i wont to now, object xyz is in quarter x/y (like 2 / 4)

    then i will have to now, what is in this quarter. so do i have to do 3d array to save also "what" is in there? or is there a more simply way!?

    thanks for all answers! :)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • If I've understood the question correctly, I think you can do this with a 2 dimensional array.

    Let's say you have a 6 by 4 grid

    Create an array object and set the size x=6, y=4, z=1. Rename the array to be called GRID.

    The data in your array will look like this (remember C2 arrays are zero based so the first row or column is 0 and the highest is (width-1):

    x

    012345

    y

    0 000000

    1 000000

    2 000000

    3 000000

    So now you want to put in a destroyer, which is 3 squares long, (and ship type 1) starting in the top left going horizontally. And a frigate (ship type =2) which is 2 squares long, starting in the bottom right and going vertically.

    You can do this by creating actions:

    GRID Set value at (0,0) to 1

    GRID Set value at (1,0) to 1

    GRID Set value at (2,0) to 1

    GRID Set value at (5,2) to 2

    GRID Set value at (5,3) to 2

    Now the data in your array will look like this:

    x

    012345

    y

    0 111000

    1 000000

    2 000002

    3 000002

    And you can see what ship is where. You can also do error checking to make sure you are not placing a new ship where one already is, and update the values in the array when a ship gets hit.

    Hope this helps.

  • Ah ok thanks... i think this will help me a lot :) i have tried a lot of things but nothing worked...

    Well no i try this. I Think this should work now!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)