[SOLVED] array of arrays for positions

Get help using Construct 2

Post » Fri Nov 24, 2017 10:55 pm

I am cautious about posting, because i feel it should be fairly easy to do this, but i just can't figure it out.

I am spawning a number of sprites (between 1 and 10).
I want them to spawn at specific locations so i am making an array (or a dictionary) to make a list of possible locations for them.
The problem i have is how to populate that array.
The data structure will be ;-

(60,150),
(60,265),
(60,380),

(180,120),
(180,235),
(180,350),
(180,465),

(280,150),
(280,265),
(280,380)

I will then pick random items from the main array and delete the index after used.
It is a fairground shooting gallery type game and i need to have the sprites (ducks) spawn on any of the three rows.

Can anyone advise on how to do this... the manual goes into multidimensional arrays, but i can't see how to apply that here because the 2 numbers are always values, not keys.

Any suggestions ? Or a better way to make a number of sprites appear but in pre-defined "allowable" places.
Last edited by jezjones24 on Thu Dec 07, 2017 11:43 am, edited 1 time in total.
B
6
S
1
Posts: 28
Reputation: 406

Post » Sat Nov 25, 2017 3:42 am

Sorry, I still don't understand your question.
You need to populate this array? Should these locations be predefined on each level? Or are they generated on random?

If predefined, you can read them from a file, say, using CSV plugin.
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :)
B
17
S
9
G
69
Posts: 1,048
Reputation: 37,659

Post » Sat Nov 25, 2017 7:33 am

Yes, they are predefined.
I thought about loading them via JSON but it doesn't give me the structure i need.
(the debugger shows 10 values in the array but they are all 0)

The output should allow me to do the following;-

index = random number from 1-10
array[index][0 for x position]
array[index][1 for y position]

then i can delete that array index so its not used again.

[EDIT]
I have solved my own question with the above reply.
Set Value at (0,0) to 60
Set Value at (0,1) to 150
Set Value at (1,0) to 60
Set Value at (1,1) to 265
etc

I just need to do that for all ten positions.
Last edited by jezjones24 on Sat Nov 25, 2017 8:49 am, edited 1 time in total.
B
6
S
1
Posts: 28
Reputation: 406

Post » Sat Nov 25, 2017 7:52 am

Try this:
Code: Select all
Array Load from JSON   
Copy/paste this string:
"{""c2array"":true,""size"":[10,2,1],""data"":[[[60],[150]],[[60],[265]],[[60],[380]],[[0],[0]],[[0],[0]],[[0],[0]],[[0],[0]],[[0],[0]],[[0],[0]],[[0],[0]]]}"

Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :)
B
17
S
9
G
69
Posts: 1,048
Reputation: 37,659

Post » Sat Nov 25, 2017 9:03 am

Thanks for that, i was trying to do something similar but i couldn't get the syntax right... that removes the need for all the set value statements.
B
6
S
1
Posts: 28
Reputation: 406

Post » Sat Nov 25, 2017 9:10 am

You can use Browser.Log -> Array.AsJSON to get the correctly formatted JSON string. All you need to change is to double all quotation marks.
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :)
B
17
S
9
G
69
Posts: 1,048
Reputation: 37,659

Post » Sat Nov 25, 2017 9:22 am

This works when i call it from On Start layout, but if i put it in a function eg: populatePositions and then call that from On Start Layout, it doesn't.
How can i find out why that is?

It doesn't work in it's own dedicated function, but as part of the 'nextLevel' function it works.
B
6
S
1
Posts: 28
Reputation: 406

Post » Sat Nov 25, 2017 9:41 am

Is the function on the same event sheet? If it's on a different event sheet, you need to Include this event sheet.
If this doesn't help, could you share your capx?
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :)
B
17
S
9
G
69
Posts: 1,048
Reputation: 37,659

Post » Sat Nov 25, 2017 10:31 am

Yes, it is in the same event sheet, although i seem to have it working now. I think there must be some issue with saving / caching my browser.
B
6
S
1
Posts: 28
Reputation: 406


Return to How do I....?

Who is online

Users browsing this forum: warmty and 12 guests