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 / A single function that combines multiple sprite functions to reduce repetitive coding and lines of code

Author
Message
Kevin Cross
20
Years of Service
User Offline
Joined: 15th Nov 2003
Location: London, UK
Posted: 24th Feb 2017 09:54 Edited at: 24th Feb 2017 10:03
I've asked a few questions on this forum and not really given anything back so thought I'd share a function I wrote that combines multiple sprite functions into one function so that you have less lines of code. I love AppGameKit but I hate the repetitiveness and the number of lines required creating sprites, positioning them and colouring them etc.

The function I wrote allows you to create a sprite, size it, colour it, position it, set an image, change the depth and a couple of other things all on one line. The function has been set up to allow for other options to be added that you might use a lot like collisions etc.

The single line would look like this:



Because you can't have optional parameters in AppGameKit I went with one parameter that's a delimited string so that you don't have to have every single setting/keyword present. This line below would also work.



I also set it up so that the lines can also be shorter by allowing for shorter variable names/keywords, for example instead of size you could use s, and instead of positionByOffset you could use pbs. Here's an example:



You can easily amend the function to use your own keywords and add more sprite functions that you use a lot.

I've added a keyword called offset which allows for x and y position or the value "center". If you use the value "center" it will find the centre of the sprite by dividing the height and width by 2 and then PositionByOffset from that point.

Here's the function (sorry I'm really bad when it comes to comments):



A similar function could be written to amend/change an already created sprite to change the position or size when needed etc. i.e.: UpdateSprite("id:1;pos:100,100;") or ChangeSprite("id:1;size:50,50;position:45,100;") etc.
easter bunny
11
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 25th Feb 2017 02:43
Hey that's actually pretty cool! I never thought of doing that. It's basically an operator overload hack
I reckon I'll be using this technique in my future projects

My Games - Latest WIP - My Website: Immortal.Digital - FB - Twitter
130,000 installs with AppGameKit and counting
Richard_6
7
Years of Service
User Offline
Joined: 3rd Feb 2017
Location:
Posted: 25th Feb 2017 04:05
Good job. Never thought on this perspective. Reminds me a html tag where we can usually put all parameters in a single line.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 25th Feb 2017 10:11
It's a nice way to do it, but it has some limitations. The cost (in terms of coding) of constructing the parameter is quite high. You have to concatenate a lot of strings together to make it dynamically.
If anyone has a neat way of doing string concatenation, it would be good to see. It would make this solution even better.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt

Login to post a reply

Server time is: 2024-09-29 23:23:44
Your offset time is: 2024-09-29 23:23:44