Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Help with array command to make multiple sprites

Author
Message
Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 9th Jul 2017 21:49
Hey guys,
Agk is my first coding program and Im kinda learning as I go. The tutorials ive found on YouTube arent very thorough. I apologize about the lengthiness of the post and appreciate any advice offered.

I wanted to made a grid of round balls 6x6. I manually created 36 sprites and spaced them evenly across the screen

A1 A2 A3
B1 B2 B3
C1 C2 C3 (etc)

A1 = createsprite("black.png")
Setspriteposition( A1, 20, 20)

A2 = createsprite("black.png")
Setspriteposition(A2, 80, 20)

I would like to create all 36 with an array, and be able to call on each for sprite collision with another command.



Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 10th Jul 2017 00:08
so I am trying to make 36 sprites with 1 command. here is the code I have used

ball = loadimage("ball.png")

for i = 1 to 6

for j = 1 to 6

ij = createsprite(ball)
setspriteposition(ij, i * 100, j * 100)
next j
next i


now what I think is happening is 1 sprite called ij is being created 36 times. What I would like to see is 36 sprites named:
11 12 13 14 15 16
21 22 23 24 25 26
31 32 33 34 35 36
41 42 43 44 45 46
51 52 53 54 55 56
61 62 63 64 65 66

Im sure this is a horrible approach for what I am trying to do, but I'm still learning. Thanks in advance for any help!
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 10th Jul 2017 09:52 Edited at: 10th Jul 2017 09:55
your almost there, you can simply use an array like so



so now your sprites have individual id's held in "balls" and you index then with "balls[0], balls[1] etc"

your first row of six sprites have index 0 to 5, second row 6 to 11 and so on


or you can use a multi-dimensional array and index by row, like so



so now row 1 is "balls[1,0] to balls[1,5]"
row 2 is "balls[2,0] to balls[2,5]"
row 3 is "balls[3,0] to balls[3,5]"

Hope that helps

Edit: sorry I should have mentioned arrays are zero based so 0 to 5 is your six sprites
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 10th Jul 2017 15:38
Arrays in AppGameKit always confuse me with the weird indexing. So if you do for loop 1 to 6 and use it to index an array defined as [5], you won't overflow?
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 10th Jul 2017 19:15
Quote: "So if you do for loop 1 to 6 and use it to index an array defined as [5], you won't overflow?"

You will overflow then. If you define it as [5], there will be 6 elements, but the first index is at 0, and the sixth element is at 5. But you can just ignore the first element, define the array as [6], then do a for loop from 1 to 6 using that as an index (the array will contain 7 elements, but you just ignore the first at 0).
13/0
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 10th Jul 2017 21:34
Quote: "Arrays in AppGameKit always confuse me with the weird indexing."


Arrays in most languages are zero based, most that I have used anyway, but any command to retrieve the array size are usually 1 based meaning you have to -1 when indexing but AppGameKit is zero based, that confused me a little at first.

Quote: "but you just ignore the first at 0"


if you do that remember to +1 on any .length calls.

when I first started programming arrays were my Achilles heel, they can be quite confusing and its worth getting to grips with them very early on as there a vital part of any project.

Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 12th Jul 2017 01:18
Thank you guys so much for the feedback! Give me a little while to digest this and I will get right back to you!
Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 12th Jul 2017 02:20
That is super cool, works perfectly. Thank you so much. Now my next question is recalling these sprites in commands.

cursor = createsprite(ball) //makes a sprite for cursor (testing purposes)

do
setspritepositionbyoffset(cursor, getpointerx(), getpointery()) //moves our cursor sprite to mouse position on screen

if getspritecollision(cursor, balls[35])
print("that one")
endif

// Print( ScreenFPS() )
Sync()
loop

Is there a way to ask if getspritecollision cursor and any of the balls?
something that works like...

if getspritecollision(cursor, balls[x])
setspriteimage(balls[x], "redball.png")
endif

Again, thanks so much for taking the time to help, it really means a lot!

Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 12th Jul 2017 03:52
Ok after doing some more homework I figured out how to use



tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 12th Jul 2017 04:48
Thanks or clarification all. I think it is the indexing of arrays that is odd for me as well.

So this code



does it not try to access the balls array defined as length 5 with index 6 in both dimensions?
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 12th Jul 2017 07:40
Quote: "does it not try to access the balls array defined as length 5 with index 6 in both dimensions?"


yes you are correct it would overflow and error out, you would need to define the array with 6 items (7 including item 0) but you ignore item 0



or, and probably a bit more confusing but I don't like to leave arrays with empty items, when accessing the array index you "-1"



Quote: "Is there a way to ask if getspritecollision cursor and any of the balls?"

Quote: "Ok after doing some more homework I figured out how to use "


yes that works generically but if you have other sprites on the screen then your going to be generating collision events for unwanted sprites, to check only your ball sprites you would loop the dimensions of the array the same way you created them and check the collision for each sprite

Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 12th Jul 2017 13:23 Edited at: 12th Jul 2017 18:58
Awesome! TMU, if it helps with the confusion, you can call for


and Part Time Coder, I had a feeling that might be the solution but didn't know if the loop would work. I'm going to try it now. here's what we have if anyone is interested =) Thanks a bunch! greenball and redball are attached



I will now try to implement the snippet you have attached, part time coder, as it seems much more practical than the jumbled up mess I currently have. After work I will let you know how it goes!

PS I know it needs to be cleaned up a little bit but this is a work in progress

Attachments

Login to view attachments
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 13th Jul 2017 05:08
Quote: "PS I know it needs to be cleaned up a little bit but this is a work in progress"


lol, I know, test bed code is never tidy, I hope you don't mind but I had a look over your code and have a few pointers that will help keep things tidy and error free, rather than list them here I commented the code, I also added the loop based collision checking and used a Select statment and moved code chunks to function calls to keep the main loop tidy, its easy to get bogged down with code.

hope this is some help

Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 14th Jul 2017 01:05
PartTimeCoder, you are my hero. Thank you so much for taking the time to put all that together. I have quite a bit to take in, but I will let keep you posted on the progress.

I often feel like I'm in some kind of dead zone, as programming tutorials for beginners just tell me what I already know, and advanced stuff is like trying to read Greek (I don't know greek), but it can be hard to find info about a specific obstacle.

Badge of Honor to PartTimeCoder!

Login to post a reply

Server time is: 2024-09-30 05:32:27
Your offset time is: 2024-09-30 05:32:27