you'll have to update the text positioning if the sprite size changes. something like:
// show all errors
SetErrorMode(2)
// set window properties
SetWindowSize( 1024,768, 0 )
SetWindowAllowResize( 1 )
// set display properties
SetVirtualResolution( 1024,768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 60, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
Red = MakeColor(255,0,0)
DrawBox(0,0,128,128,Red,Red,Red,Red,1)
IMG = GetImage(0,0,128,128)
BUTTON = CreateSprite(IMG)
TXT = CreateText("MyButton")
SetTextSize(TXT,48)
SetTextAlignment(TXT,1) //center text
UpdateButton(0,0,BUTTON,TXT)
do
mx = GetPointerX() : my = GetPointerY()
If GetPointerPressed() Then UpdateButton(mx,my,BUTTON,TXT)
Sync()
loop
Function UpdateButton(X,Y,Spr,Text)
SetSpritePosition(Spr,x,y) //position sprite at mouse x,y
NewX = Random(200,400) : NewY = Random(100,400) //generate new dimensions for sprite
SetSpriteSize(Spr,NewX,NewY) //resize sprite
CX# = NewX/2 + X : CY# = NewY/2 + Y - (GetTextTotalHeight(Text)/2) // find center of sprite (then add the sprite's new position and subtract 1/2 the text height)
SetTextPosition(Text,CX#,CY#) //reposition text to center
EndFunction
...click anywhere to update
edit: hopefully easier to read, now