SetWindowSize(1024,768,0)
SetClearColor(0,0,255)
// 2 colours to make a checkerboard pattern
CreateImageColor(1,255,0,0,255)
CreateImageColor(2,0,255,0,255)
// Constants
numRows = 6
numCols = 7 `odd number to get checkerboard pattern
spriteSizeX = 12
spriteSizeY = 10
pivotX = ((numCols*spriteSizeX)/2) `centre of grid in X
pivotY = ((numRows*spriteSizeY)/2)
stateStr as string[4] = ["Grid Centre","Sprite Centre","Upper left sprite corner","Lower Right Sprite Corner"]
// For all sprites
for rows = 0 to numRows-1
for cols = 1 to numCols
spriteNum = rows*numCols+cols
CreateSprite(spriteNum, 1+mod(spriteNum,2))
SetSpriteSize(spriteNum, spriteSizeX, spriteSizeY)
// set the offset so that (0,0) is in the centre of the grid
SetSpriteOffset(spriteNum, pivotX-(cols-1)*spriteSizeX, pivotY-rows*spriteSizeY)
// and set the position so the centre of the grid (sprite offset) is in the centre of the screen
// Change to SetSpritePositionByOffset from SetSpritePosition below to see how that affects the
// layout when the offset is adjusted to Sprite Centre - the positions are re-calculated
// when using SetSpritePositionByOffset and the offset is changed
`SetSpritePositionByOffset(spriteNum,50,50)
SetSpritePosition(spriteNum, 50-pivotX+((cols-1)*spriteSizeX), 50-pivotY+(rows*spriteSizeY))
next cols
next rows
rotation# = 0
state = 0
do
// notice how the sprite position doesn't change. only the angle changes but with a offset
// that's outside the sprite, that changes where the sprite is drawn if it is rotated
// if SetSpritePositionByOffset was used, then the position DOES change
print("Sprite1 X: "+str(GetSpriteX(1))+" Y: "+str(GetSpriteY(1))+" Angle: "+str(GetSpriteAngle(1)))
print(" Offset X: "+str(GetSpriteOffsetX(1))+" Offset Y:"+str(GetSpriteOffsetY(1)))
print("Click to see sprite offset in different places")
print(stateStr[state])
// upon releasing the click, redo the offsets
if GetPointerReleased()
state = state + 1
if state > 3 then state = 0
for rows = 0 to numRows-1
for cols = 1 to numCols
spriteNum = rows*numCols+cols
if state = 0
SetSpriteOffset(spriteNum, pivotX-(cols-1)*spriteSizeX, pivotY-rows*spriteSizeY)
elseif state = 1
SetSpriteOffset(spriteNum, spriteSizeX/2, spriteSizeY/2)
elseif state = 2
SetSpriteOffset(spriteNum, 0, 0)
else
SetSpriteOffset(spriteNum, spriteSizeX, spriteSizeY)
endif
next cols
next rows
endif
// rotate all the sprites
for rows = 0 to numRows-1
for cols = 1 to numCols
spriteNum = rows*numCols+cols
// when the sprites' individual rotations are set, they
// rotate around their offsets, wherever that may be
SetSpriteAngle(rows*numCols+cols, rotation#)
// Draw a grid to show where the sprite is positioned
DrawLine(GetSpriteX(spriteNum),
GetSpriteY(spriteNum),
GetSpriteX(spriteNum)+spriteSizeX,
GetSpriteY(spriteNum),
255, 255, 255)
DrawLine(GetSpriteX(spriteNum),
GetSpriteY(spriteNum),
GetSpriteX(spriteNum),
GetSpriteY(spriteNum)+spriteSizeY,
255, 255, 255)
next cols
next rows
rotation# = rotation# + 1.0
sync()
loop
Help make AGK better by submitting an example for this command! (All examples are subject to approval)