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:
spr = CreateNewSprite("size:200,518;position:0,100;color:239,239,239,255;fixToScreen:1;")
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.
spr = CreateNewSprite("size:200,518;position:0,100;")
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:
spr = CreateNewSprite("s:200,518;p:0,100;c:239,239,239,255;fts:1;")
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):
function CreateNewSprite(parameters$)
spriteID = CreateSprite(0)
for a = 1 to CountStringTokens(parameters$, ";")
parameter$ = GetStringToken(parameters$, ";", a)
variable$ = lower(TrimString(GetStringToken(parameter$, ":=", 1), " "))
value$ = GetStringToken(parameter$, ":=", 2)
if (variable$ = "image" or variable$ = "img" or variable$ = "i")
SetSpriteImage(spriteID, val(value$))
elseif (variable$ = "size" or variable$ = "s")
SetSpriteSize(spriteID, valFloat(GetStringToken(value$, ",", 1)), valFloat(GetStringToken(value$, ",", 2)))
elseif (variable$ = "offset" or variable$ = "o")
if (value$ = "center" or value$ = "centre")
SetSpriteOffset(spriteID, GetSpriteWidth(spriteID) / 2, GetSpriteHeight(spriteID) / 2)
else
SetSpriteOffset(spriteID, valFloat(GetStringToken(value$, ",", 1)), valFloat(GetStringToken(value$, ",", 2)))
endif
elseif (variable$ = "position" or variable$ = "pos" or variable$ = "p")
SetSpritePosition(spriteID, valFloat(GetStringToken(value$, ",", 1)), valFloat(GetStringToken(value$, ",", 2)))
elseif (variable$ = "positionbyoffset" or variable$ = "pbo")
SetSpritePositionByOffset(spriteID, valFloat(GetStringToken(value$, ",", 1)), valFloat(GetStringToken(value$, ",", 2)))
elseif (variable$ = "color" or variable$ = "clr" or variable$ = "c")
if (CountStringTokens(value$, ",") = 3)
SetSpriteColor(spriteID, valFloat(GetStringToken(value$, ",", 1)), valFloat(GetStringToken(value$, ",", 2)), valFloat(GetStringToken(value$, ",", 3)), 255)
elseif (CountStringTokens(value$, ",") = 4)
SetSpriteColor(spriteID, valFloat(GetStringToken(value$, ",", 1)), valFloat(GetStringToken(value$, ",", 2)), valFloat(GetStringToken(value$, ",", 3)), valFloat(GetStringToken(value$, ",", 4)))
endif
elseif (variable$ = "angle" or variable$ = "a")
SetSpriteAngle(spriteID, valFloat(value$))
elseif (variable$ = "depth" or variable$ = "d")
SetSpriteDepth(spriteID, valFloat(value$))
elseif (variable$ = "fixtoscreen" or variable$ = "fix" or variable$ = "fts")
if (value$ = "1" or value$ = "true" or value$ = "t")
FixSpriteToScreen(spriteID, 1)
else
FixSpriteToScreen(spriteID, 0)
endif
elseif (variable$ = "group" or variable$ = "grp" or variable$ = "g")
SetSpriteGroup(spriteID, val(value$))
endif
next
endfunction spriteID
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.