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.

Newcomers AppGameKit Corner / Landscape repeating via an Array

Author
Message
Khadin
4
Years of Service
User Offline
Joined: 13th Feb 2015
Location: Nottingham
Posted: 11th Apr 2015 21:27 Edited at: 11th Apr 2015 21:28
Ok, so i'm very new when it comes to Arrays. I've tried to understand them, but i'm still stuck.

To create and lay landscape, i've been doing it like so:



This puts my planes in a nice little row. I use the same code when laying landscape.

Now i'm pretty certain an Array would make my life so much easier, but i'm unsure how to go about doing it.

Khadin
IronManhood
4
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 11th Apr 2015 23:11 Edited at: 12th Apr 2015 02:05
There are a bunch of different ways of doing this same thing but here is one way.



Here is something that may help you understand arrays.

Khadin
4
Years of Service
User Offline
Joined: 13th Feb 2015
Location: Nottingham
Posted: 12th Apr 2015 02:04
Thanks IronManhood.

I'll give this a go tomorrow, and hopefully i'll understand it I find Arrays really hard to get, but i know they're something i must learn.

Thanks again.

Khadin
IronManhood
4
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 12th Apr 2015 17:55
You can think of an array as boxes placed in a line. Each box is called an element and the number of elements is call the array size or length. Each element is basically its own variable that will hold whatever data you put in it. You can access any element of the array by calling it specifically or you can use a loop to increment a variable and iterate over the array accessing each element sequentially.

Of course, you can have an array of arrays. Those are called multi-dimensional, matrix or table. In agk you can have arrays of up to 6 dimensions, which does boggle the mind a bit. My advice would be to stick with one dimension until you are comfortable and then move on to other dimensions.

Read about arrays, then play with them.
Khadin
4
Years of Service
User Offline
Joined: 13th Feb 2015
Location: Nottingham
Posted: 12th Apr 2015 18:41
Works perfectly dude, thanks. I'm trying to break it down, but it just will not sink in lol.

As i say, it works though, but now it's just trying to understand them. They're a little bit like JavaScript Arrays which i did understand to a point, but that was a while back.

Thanks again.

Khadin
BatVink
Moderator
16
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 12th Apr 2015 19:37
Try another analogy - boys names. We'll just stick to one piece of data - a string for each name.

You have 4 boys, and in your current method, you'll need 4 variables:



to print the names, you firstly need to know that there are exactly 4 boys. Then, you need to add a line of code for each one:



Now the same using arrays. Firstly, create an array of 4 elements, as described in the examples above.
Then add the names



So far, it's not looking very different, and the advantages aren't apparent...yet. Let's print them in exactly the same way




Now let's print them using a loop.



Here we are executing one line of code 4 times. Each time it prints the next element (boy's name) in the array. We can make this more generic. Lats pretend we don't know how many boys there are:



Now it will loop until we reach the end of the array, which could be any length.

It's still not looking much more efficient. So let's pretend we have a school class of 30 boys



or as an array:



Spot the difference between this and the last array example...there is none! It's exactly the same code. But it can handle any of these:



Imagine applying this to an army of soldiers with a health value. After each round of fighting, you can iterate through an unknown number of soldiers and quickly determine which ones died (health = 0) instead of writing 100 lines of code or more to check each one individually. And with the non-array method, you have to set an arbitrary limit on how many soldiers you're prepared to code for.

hope this helps!

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Khadin
4
Years of Service
User Offline
Joined: 13th Feb 2015
Location: Nottingham
Posted: 12th Apr 2015 21:25
BatVink... that's fantastic. Thank you so much for explaining it the way you have. I'm pretty certain after writing a few examples, i'll have it down!

Thanks again

Khadin
Khadin
4
Years of Service
User Offline
Joined: 13th Feb 2015
Location: Nottingham
Posted: 12th Apr 2015 22:19 Edited at: 12th Apr 2015 22:54
Out of interest, what does the message mean: Array Index out of bounds. It's not a main worry, just sometimes get it.



Oki, so it's repeating the land, but once i get to the bit where the land is no longer being placed, the app crashes. Any reason for that?

EDIT: The frame rate seriously drops, then it'll freeze.

Khadin
IronManhood
4
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 12th Apr 2015 22:23 Edited at: 12th Apr 2015 22:23
That means you tried to call an element that didn't exist. For example, you try to call element 3 in an array that is only 2 elements in size. If you are using dynamic arrays, chances are you will encounter this a lot and have to debug your code. Post your code if you need help.
BatVink
Moderator
16
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 12th Apr 2015 22:40
Which reminds me...both Ironmanhood and myself used array elements 1 onwards.

Arrays actually start at 0.

So this...

global boy as string[4]

has 5 elements - 0 to 4. And...

boy.length

will return 4. It gives you the last element number, not the number of elements

SO...to keep things simple, many people ignore element 0. Using it makes counting and managing elements a little bit more confusing than they really need to be, and introduces bugs at a greater rate! Element 0 can be safely ignored. Just bear in mind that if you accidentally try to read element 0, you won't get an error. You'll probably just get empty strings or zero values.

An array with no elements has a length of -1.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
IronManhood
4
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 12th Apr 2015 23:35
Khadin
4
Years of Service
User Offline
Joined: 13th Feb 2015
Location: Nottingham
Posted: 13th Apr 2015 00:09 Edited at: 13th Apr 2015 00:10
Thanks Iron, i'll read that now. Whilst i am, could you just have a look over the code. It's laying the landscape, just it locks up after i reach the end of it.

Thanks



Khadin
IronManhood
4
Years of Service
User Offline
Joined: 6th Feb 2015
Location: US
Posted: 13th Apr 2015 00:54 Edited at: 13th Apr 2015 00:57
The only issues that I see with your code are with LoadImage. You really only need to load an image once. The way you have it now, you load the same image four different times. Also, using a loop to load a bunch of images is an efficient way of doing it, so you're on the right track. You'll have to figure out a way to iterate through the .png filenames to take advantage of it.
BatVink
Moderator
16
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 13th Apr 2015 08:02 Edited at: 13th Apr 2015 08:04
Sat in an airport so can't test, but this should also work. Remember your ultimate goal is to create code that can manage any number of array indexes, so that your game is expandable. In this case, you have to lay sprite 1 "manually", then all others can be done with simple logic based on array index.




Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Khadin
4
Years of Service
User Offline
Joined: 13th Feb 2015
Location: Nottingham
Posted: 13th Apr 2015 16:59
Hey guys.

Can't code till friday sadly, but i'll give what you've suggested a try and report back. Hopefully it'll work, then i can move onto my next question ;P

Thanks.

Khadin

Login to post a reply

Server time is: 2019-08-23 15:03:52
Your offset time is: 2019-08-23 15:03:52