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 / Loading SubImages using a String[] of their filenames doesn't work.

Author
Message
Dead Pixel
9
Years of Service
User Offline
Joined: 27th Nov 2014
Location:
Posted: 27th Feb 2015 21:57
Hello folks,

I have a sprite sheet from which I wish to load 12 sub-images. For the sake of efficiency I want to loop through a String[] of their names and to this end I have the following array set up.

sprite_filenames as String[12] = ["WhitePawn.png","BlackPawn.png", "WhiteKnight.png","BlackKnight.png", "WhiteBishop.png","BlackBishop.png", "WhiteRook.png","BlackRook.png", "WhiteQueen.png", "BlackQueen.png","WhiteKing.png","BlackKIng.png"]

But the following code won't create a sprite for me.



The code above is in a function that is called before the do.....loop is entered. For clarity here I have ommitted the for.....next loop which works its way through the entire array.

What am I doing wrong?

//******************************
// Coding In BASIC using AppGameKit V2
//******************************
Polaraul
9
Years of Service
User Offline
Joined: 13th Dec 2014
Location:
Posted: 28th Feb 2015 00:16 Edited at: 28th Feb 2015 00:16
Hi there. I managed to get this to work in my code by using:




www.polaraul.com
Conjured Entertainment
AGK Developer
18
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 2nd Mar 2015 05:35 Edited at: 2nd Mar 2015 05:56
Quote: "What am I doing wrong?
"

sprite_filenames[x]

try ...

sprite_filenames$[x]

... so it is a string

Quote: "dim sprite_filenames[1] as String = ["WhiteKnight.png"] "

Right, AS STRING

Quote: "sprite_filenames as String[12] = ["WhitePawn.png","BlackPawn.png", "WhiteKnight.png","BlackKnight.png", "WhiteBishop.png","BlackBishop.png", "WhiteRook.png","BlackRook.png", "WhiteQueen.png", "BlackQueen.png","WhiteKing.png","BlackKIng.png"]"

Should be ...
sprite_filenames[12] as String =

not...
sprite_filenames as String[12] =

but I would use...
sprite_filenames$[12] =

The reason I like doing that is so I know at a glance what type of variable I am dealing with , without scrolling up to see its declaration.

sprite_filenames[x] //integer
sprite_filenames#[x] //float
sprite_filenames$[x] //string

Bottom line is though, you made a typo...
sprite_filenames as String[12] =
sprite_filenames[12] as String =


Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
Dead Pixel
9
Years of Service
User Offline
Joined: 27th Nov 2014
Location:
Posted: 3rd Mar 2015 00:30 Edited at: 3rd Mar 2015 00:31
Thanks guys, with your help I have it working now, nice one !!!.

@Conjured Entertainment - according to the online help declaring arrays in AppGameKit V2 can be done using sprite_filenames as Integer[12] = etc.....
See here -
http://www.appgamekit.com/documentation/guides/12_array_changes.htm - The example given is for an integer array so I assumed the same syntax should apply to string arrays.

This is what I have at the moment, it compiles and runs as expected.



Array indexes start at 0, but the indexes of sub images cannot hence my use of sub_image_id to take care of that problem.

I plan on replacing sprites[x] = CreateSprite(sub_image_id) with an array of types to take care of the sprite management.

Now, a question for you..... How important is the naming of the subimages - "WhitePawn.png","BlackPawn.png", etc? The reason I ask is that if I were to rename the subimages to "1.png", "2.png" and so on I could generate the string names on the fly as I go through the loop with Str(x) + ".png" and do away with my use of the array entirely. It seems to be a neater solution, but is there any major drawbacks to that approach?

//******************************
// Coding In BASIC using AppGameKit V2
//******************************
Conjured Entertainment
AGK Developer
18
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 3rd Mar 2015 03:14 Edited at: 3rd Mar 2015 03:17
Quote: "@Conjured Entertainment - according to the online help declaring arrays in AppGameKit V2 can be done using sprite_filenames as Integer[12] = etc.....
See here - "

lol WOW
That's a new one on me.
I thought it was backwards and was a typo.
Thanks for pointing that out!

Quote: "
Array Declaration and Resizing

To maintain backwards compatibility..."

I guess it is backwards after all.

Quote: "Now, a question for you..... How important is the naming of the subimages - "WhitePawn.png","BlackPawn.png", etc? The reason I ask is that if I were to rename the subimages to "1.png", "2.png" and so on I could generate the string names on the fly as I go through the loop with Str(x) + ".png" and do away with my use of the array entirely. It seems to be a neater solution, but is there any major drawbacks to that approach?
"

I never liked the caps in the file names, but I have done it on occasion.
I never liked having a file as just a number either, as I usually use the number as a suffix so I retain the name and know what the image is.
I do like your idea though of using that number, and you could put it in anywhere.
For instance, if you used "chess_piece_" + str(x) + ".png" , then you would still have to know that 1 is a pawn, 2 is a rook, 3 is a knight, and so on, but you would know at a glance that the image pertains to a chess piece.

I would probably use an array for the board, to keep track of what piece is where, but yeah, you probably don't need one for the pieces.
I am a master at chess (have won tournaments), but I have never tried to program the game.
So, I have never really given the logic much thought as far as code goes.
It sounds as if you were going to duplicate the pieces though, since your arrays is only 12 and not 32.
So, I think eliminating that array would not affect your original coding plans.
I probably would have started off with a multi-dimensional array for the pieces and over complicated things.
I think this is really your call and depends on how you plan to code the logic as there is more than one way to do this.

I see it as the board as an array holding the value of the piece.
Each move is targeting a square to move from, and selecting one of the valid options to move to.
So, depending on the value of the piece of the selected square, we could determine the valid moves available.
The tricky part is detecting invalid moves like that move opening up a file where the piece behind it would be checking the King.
When other pieces are affected by the move, you really have to be checking a lot of stuff all the time.
It is probably simpler than it sounds, but the complexities of this in general is why I never fooled with it.
I never wanted to open up that can of worms.

Since I have not thought this out, I would probably make a lot of mistakes along the way and would have to revise the code over and over.
So, instead of me even offering any advice on this, I will have to let you run with it and experience those logistical challenges on your own.
I am sure there is someone here in the forums who has made a chess game before though, so hopefully one of those guys can chime in.


Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
Dead Pixel
9
Years of Service
User Offline
Joined: 27th Nov 2014
Location:
Posted: 3rd Mar 2015 22:30 Edited at: 3rd Mar 2015 23:45
Sorry for misleading you, I'm not coding a chess game, I have an idea for a variation on a puzzle game I wrote in C# a few years ago. The reason I'm using chess pieces for the sprites this time around is that the hierarchy of the pieces lend themselves to the nature of the puzzle. I'm still getting to grips with AppGameKit and some things are tripping me up. I tend to do a lot of coding experiments as I go.

I also like your idea of the "chess_piece_" + Str(x) + ".png" for building the string and will be implementing it. From what I've read elsewhere on the forums it seems to be good programming practice to unload the spritesheet from memory once the subimages have been set up as sprites.

I had an account on http://www.redhotchess.com/ for a while and played against some friends of mine there. I loved the fact that it was all running in the browser with nothing to install.

//******************************
// Coding In BASIC using AppGameKit V2
//******************************
Mike Archer
9
Years of Service
User Offline
Joined: 19th Feb 2015
Location: Wales
Posted: 4th Mar 2015 01:39
I'm not sure if I'm misunderstanding what you say when you say 'unload the spritesheet from memory', but if you mean the larger image you loaded that the subimages are contained in, then as I understand it, this must be kept in memory, as this is what is used each time a sprite is displayed, the advantage of using a large image like this rather that individual images for each sprite is one of speed, because all of the sprites can be drawn from the same image. By using SubImage you are not creating new images, but defining which part of the larger image should be used for your sprite.
Conjured Entertainment
AGK Developer
18
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 4th Mar 2015 18:10 Edited at: 4th Mar 2015 18:52
Quote: "I'm not sure if I'm misunderstanding what you say when you say 'unload the spritesheet from memory', but if you mean the larger image you loaded that the subimages are contained in, then as I understand it, this must be kept in memory, as this is what is used each time a sprite is displayed. The advantage of using a large image like this rather that individual images for each sprite is one of speed, because all of the sprites can be drawn from the same image. By using SubImage you are not creating new images, but defining which part of the larger image should be used for your sprite. "

Yeah, there is a big advantage to using these sprite sheets as long as the images are tight and you don't have a lot of wasted space.

I have been using separate images for each frame when creating animations for Angry Piglets. (and all my sprite animations in the past)
Made my first sprite sheet the other day of the Barbarian in FPSC MP42, and it was very cool how much work that saves me.
The time saved was due to using Model-2-Sprite, which I picked up the other day in the Sprite Tools Bundle here at TGC.
They are both great products, and I strongly advise anyone who is going to be working with 2d sprite sheets in AppGameKit to take advantage of that bundle's savings. (25%)
Just thought I would mention that to any of the new comers seeing this who have not yet made a sprite sheet.

Quote: "Sorry for misleading you, I'm not coding a chess... game"

Awe, and I wanted to show you how Black can win on their second move.


Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1

Attachments

Login to view attachments
Dead Pixel
9
Years of Service
User Offline
Joined: 27th Nov 2014
Location:
Posted: 4th Mar 2015 23:23 Edited at: 4th Mar 2015 23:30
Quote: ".....as I understand it, this must be kept in memory"


Mike, you are correct, I tested this a few minutes ago where I deleted the spritesheet below the end of my loop and all I saw on screen were 12 white squares. My knowledge of AppGameKit expands yet again

One issue that I found with the Image Joiner utility was that it wouldn't pack my images in the order I wanted which was from the lowest ranking pieces to the highest. I used numbers and when that didn't work I used letters of the alphabet to name the files - 1.png, 2.png...etc, and then A.png, B.png...etc. No luck, so in the end I used the text file generated with Image Joiner along with a spritesheet generated in a third party tool which packed the images in the right order for me.
The images are all the same size so Image Joiner would have no reason to pack them in any order other than the way they were named.

//******************************
// Coding In BASIC using AppGameKit V2
//******************************

Login to post a reply

Server time is: 2024-04-19 02:06:41
Your offset time is: 2024-04-19 02:06:41