How to make arrays of sprites

Get help using Construct 2

Post » Fri Dec 16, 2011 11:44 am

Hey guys,

I read the arrays and loops tutorial, and also looked at the FAQ sticky regarding arrays. But I can't figure out something.

I'm trying to create a chessboard, and so I thought an array of 8x8 sprites would be perfect. But I can't figure out how to implement it and make it work.

I have an arraySquares object, width 8, height 8. I tried to populate it in a loop but either I must be doing something wrong or perhaps an array is not the right thing to use for this.

I added an event with 2 conditions, OnStartOfLayout and For curLine from 0 to 7, and then System->Create Object->square on layer 1 at (400 + curLine * 35) and expected to see 8 squares there when the game runs, but there's only one.

So please let me know what I'm doing wrong, or better still, what approach would you take if you wanted to draw a chessboard. Next steps I want to make other sprites drag-and-drop-able and they should "snap" to the right square.

Thanks in advance.
B
14
S
5
G
7
Posts: 235
Reputation: 5,175

Post » Fri Dec 16, 2011 11:50 am

loopindex("curLine")

Also, are you providing both X and Y?
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

Post » Fri Dec 16, 2011 11:59 am

Hi:
   Check out the Path Finder plugin. It creates a grid in it's demo game. Sprites make their own collection, so an array may not be necessary. Also, Objects can carry their own variables, so you can add data fields to the object itself.
B
27
S
5
G
5
Posts: 24
Reputation: 4,553

Post » Fri Dec 16, 2011 1:18 pm

not sure if this would be 100% correct for bordlayout and loopreset but maybe it can help you

you will need 2 framed squaresprite, 0 white 1 black and set origin at topleft

/////vars
offset=150
yloop=0
xloop=0

for "create" 0 to 64
create square at x: offset + square.width*xloop
                 y: offset + yloop*loopindex
                    add 1 to xloop


if loopindex+1%2 = 0 set frame to 1 (division remainder = 0 / if even set frame black)
if loopindex+1%8 = 0 add square.height to yloop and set xloop to 0 (if first row created set y position 1 square down
ImageImage
B
61
S
19
G
6
Posts: 809
Reputation: 9,028

Post » Fri Dec 16, 2011 2:13 pm

Thanks for the prompt replies guys.

@vtrix: how can I add the "IF"s? When I click Add Action I can only add actions, not conditions. I can't figure out in what order to click the things to get it to work.

Here's what I have:

http://imageshack.us/photo/my-images/37/loopa.png/

But obviously the yLoop is incremented at the same time with the xLoop.

How can I add a condition only for this action, what to click and in which order? It's not very intuitive.

Thanks!
B
14
S
5
G
7
Posts: 235
Reputation: 5,175

Post » Fri Dec 16, 2011 2:14 pm

By the way I don't care about white / black squares just yet, I want to get it to work with the same color first.
B
14
S
5
G
7
Posts: 235
Reputation: 5,175

Post » Fri Dec 16, 2011 2:24 pm

About the checkboard just create a checkboard image 2x2 that is white black on top black white on bottom (or vice versa :D) and import it on construct as tiled background object. Kiyoshi2011-12-16 14:25:56
B
58
S
13
G
10
Posts: 632
Reputation: 12,505

Post » Fri Dec 16, 2011 2:39 pm

@Kiyoshi thanks, but I don't want the board to cover the entire layout.

@vtrix: I got it to work after all, I added a sub-event (hadn't noticed the menu). Then I had to stop animation for the square, as it was going all black as soon as the game started.

For reference here's what I ended up with:

http://img51.imageshack.us/img51/4012/loop2j.png

Thanks again, and I'll be back :D with more questions.
B
14
S
5
G
7
Posts: 235
Reputation: 5,175

Post » Fri Dec 16, 2011 2:39 pm

about the colors, it can be usefull later on when you want to check this... take the solution you think is best..

about the ifs, it should be subevents of the loop, system compare
ImageImage
B
61
S
19
G
6
Posts: 809
Reputation: 9,028

Post » Fri Dec 16, 2011 2:43 pm

@Geo Just resize it as wanted. Or am i misinterpreting what you want ?
B
58
S
13
G
10
Posts: 632
Reputation: 12,505

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 13 guests