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 / Creating Global Sprites within functions?

Author
Message
Gibbon28
11
Years of Service
User Offline
Joined: 22nd Oct 2012
Location: Derbyshire, UK
Posted: 24th Oct 2012 21:48
Hi Im really confused about how to get access to sprites that have been created within a function?

If i have 1 sprite, i can declare a var global OUTSIDE of any functions, then use this variable in a function to create a sprite. Thats fine, i can then access it using the variable where ever.

But im creating the sprites in a loop, say 10 for instance and i want to be able to give each sprite the id 100+loopindex. This is done in a function.



So i have 10 sprites with ID's 100 TO 110. But i cannot access them using the ID's 100 - 110? How can i get access to them?

Thanks
Andy
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 24th Oct 2012 22:55
Is the variable 'image' declared as a global in the main file?

Plus, you are only creating IDs 101 through 110, not 100. Your loop goes from 1 to 10, so the first one created is id 101.

Cheers,
Ancient Lady
AGK Community Tester
Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 24th Oct 2012 23:21
This is just a quick example, not tested, but should give you some idea.



Gibbon28
11
Years of Service
User Offline
Joined: 22nd Oct 2012
Location: Derbyshire, UK
Posted: 25th Oct 2012 00:43
Thank for the help, i had realised minutes after posting what i was doing wrong, and how to do it correctl (as shown in your code example) but as im still in the "untrusted2 group (you never know) i could come and edit/remove my post.

Thanks again though
Andy
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 25th Oct 2012 01:11
If you're storing the references in an array, you want to be using the simpler sprArray[I] = createSprite(image) format.

You could also pass the size into the function and dim the array in there.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 25th Oct 2012 01:43 Edited at: 25th Oct 2012 01:45
Matty H, from your sample, spr5ID should be 105.

EDIT:
And your sample, as it stands, will fail. This is because there is no evidence that the value in image has been set to anything. Presumably, there should be a LoadImage call that sets the image variable. Otherwise, all your sprites will just be white blobs.

Cheers,
Ancient Lady
AGK Community Tester

Login to post a reply

Server time is: 2024-04-28 05:59:26
Your offset time is: 2024-04-28 05:59:26