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 / [SOLVED] Naming sprites/images?

Author
Message
Yasha_
4
Years of Service
User Offline
Joined: 20th May 2019
Location:
Posted: 20th May 2019 00:34
I have started working through the beginners video tutorials, and one thing that has perplexed me is how sprites and images are referenced as integers. So for example to load an image and create a sprite from it the code would be:
loadimage(1, "player_ship.png")
CreateSprite(1, 1)

And then whenever I want to manipulate that sprite I have to reference it through the integer, eg:
SetSpritePosition(1, playerx, playery)

I don't understand how I could possibly keep track of that as a project got larger. In other languages I would give the sprite or object a name so if I have lots of sprites/objects I don't get lost (as easily). For example the code would look more like:
SetSpritePosition(oPlayer, oPlayer.x, oPlayer.y)


Is there some way to give the sprites etc readable names? Or is this how you have to approach development in AppGameKit?
My apologies if this is a very simple or often asked question!


The author of this post has marked a post as an answer.

Go to answer

fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 20th May 2019 00:43 Edited at: 20th May 2019 00:50
I like to do that with an array of type like below which will load the images 1.png 2.png etc for all 10 and ssign them to image and sprite IDs
fubarpk
fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 20th May 2019 00:48 Edited at: 20th May 2019 00:55
you could add random positioning in the above with the command
SetSpritePosition(mySprites[num].sprID,random(100,900),random(100,400))
for example
and to move

which moves all the sprites to the left by one pixel by getting there current location and subtracting 1
fubarpk
fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 20th May 2019 04:51 Edited at: 20th May 2019 05:03
This post has been marked by the post author as the answer.
yasha,

it depends on your project and the # of sprites but you were already 1/2 way there when you wrote:

Quote: "SetSpritePosition(1, playerx, playery)"


what i mean is, you've given the player sprite x & y positions variable names. instead, give the player sprite itself a variable name (and the image, too, if you need it). IE:


then to manipulate:

...for example.

basically, AppGameKit gives you the option of declaring the image, sprite, object #'s as you have (in this case "1" for image and sprite, above) or letting AppGameKit decide the object #'s while you reference them with variable "names" as you're wanting to do.

so, if you have 1 player sprite, see above. and, if you have a number of enemies, you could do similar with an array:


which you can reference similarly:

...for example.

beyond that, if you want to give your enemies additional characteristics (like defining their speed, rate of fire, etc), then you would want to use Types to define, organize and reference each, as fubar suggests. HERE is a basic example of that where i've given 20 different sprites a constant direction of travel using Types.

in other words, you don't (ever) have to reference the integer/object id itself.

hope that helps

Yasha_
4
Years of Service
User Offline
Joined: 20th May 2019
Location:
Posted: 20th May 2019 05:39
Many thanks to both furbarpk and Virtual Nomad, that has cleared the issue up for me!

Login to post a reply

Server time is: 2024-04-20 12:59:15
Your offset time is: 2024-04-20 12:59:15