Posted: 29th Jul 2021 21:53
Thought I would give the brain-ache stuff a little break and just tinker with some game stuff and straight away while trying to attach a turret to a tank I found a missing function ... I Know, I Know, I cant find something that does not exist its an oxymoron!!

Anyhou, This seems to work, pretty happy with how easy it was to implement.

+ Code Snippet
parent_id = CreateSprite(0)
SetSpriteSize(parent_id, 100, 20)
SetSpriteOffset(parent_id, 50, 10)
SetSpritePositionByOffset(parent_id, 200, 200)
SetSpriteColor(parent_id, 255,0,0,255)

child_id1 = CreateSprite(0)
SetSpriteSize(child_id1, 50, 50)
SetSpriteOffset(child_id1, 25, 25)
SetSpritePositionByOffset(child_id1, 300, 200)
SetSpriteColor(child_id1, 0,0,255,255)

child_id2 = CreateSprite(0)
SetSpriteSize(child_id2, 30, 30)
SetSpriteOffset(child_id2, 15, 15)
SetSpritePositionByOffset(child_id2, 250, 230)
SetSpriteColor(child_id2, 0,255,0,255)

child_id3 = CreateSprite(0)
SetSpriteSize(child_id3, 30, 30)
SetSpriteOffset(child_id3, 15, 15)
SetSpritePositionByOffset(child_id3, 250, 170)
SetSpriteColor(child_id3, 0,255,0,255)

child_id4 = CreateSprite(0)
SetSpriteSize(child_id4, 30, 30)
SetSpriteOffset(child_id4, 15, 15)
SetSpritePositionByOffset(child_id4, 180, 230)
SetSpriteColor(child_id4, 0,255,0,255)

child_id5 = CreateSprite(0)
SetSpriteSize(child_id5, 30, 30)
SetSpriteOffset(child_id5, 15, 15)
SetSpritePositionByOffset(child_id5, 180, 170)
SetSpriteColor(child_id5, 0,255,0,255)

// fix sprites to parent
FixSpriteToSprite(parent_id, child_id1)
FixSpriteToSprite(parent_id, child_id2)
FixSpriteToSprite(parent_id, child_id3)
FixSpriteToSprite(parent_id, child_id4)
FixSpriteToSprite(parent_id, child_id5)

do
	if GetRawMouseLeftState()
		MoveSprite(parent_id, 5)
		TurnSpriteTo(parent_id, GetRawMouseX(), GetRawMouseY(), 0.1)
		UpdateFixSpriteToSprite()
	endif

	Print("Press Mouse Left To Move Sprite")
    Sync()
loop


// FixSpriteToSprite
Type tSprite
	ParentID
	ChildID
	ChildOffsetX
	ChildOffsetY
EndType
Global gSprite as tSprite[-1]

Function FixSpriteToSprite(parent_id, child_id)
	spr as tSprite
	spr.ParentID=parent_id
	spr.ChildID=child_id
	spr.ChildOffsetX=GetSpriteXByOffset(child_id)-GetSpriteXByOffset(parent_id)
	spr.ChildOffsetY=GetSpriteYByOffset(child_id)-GetSpriteYByOffset(parent_id)
	gSprite.insert(spr)
EndFunction

Function UpdateFixSpriteToSprite()
	for index=0 to gSprite.length
		px# = GetWorldXFromSprite( gSprite[index].ParentID, gSprite[index].ChildOffsetX, gSprite[index].ChildOffsetY )
		py# = GetWorldYFromSprite( gSprite[index].ParentID, gSprite[index].ChildOffsetX, gSprite[index].ChildOffsetY )
		SetSpritePositionByOffset(gSprite[index].ChildID, px#, py#)
		SetSpriteAngle(gSprite[index].ChildID, GetSpriteAngle(gSprite[index].ParentID))
	next
EndFunction
// ####################


// Forum Functions
function MoveSprite(spriteID, amount#)
    rem get display data
    dw# = getVirtualWidth()
    dh# = getVirtualHeight()
    aspect# = (dw# / dh#)
    if aspect# = 1.0
        aspect# = getDisplayAspect()
    else
        aspect# = 1.0
    endif
    a# = getSpriteAngle(spriteID)
    x# = getSpriteXbyOffset(spriteID)
    y# = getSpriteYbyOffset(spriteID)
    setSpritePositionByOffset(spriteID,x#+cos(a#)*amount#,y#+sin(a#)*amount#*aspect#)
endfunction

function TurnSpriteTo(spriteID,x#,y#,value#)
    rem get display data
    dw# = getVirtualWidth()
    dh# = getVirtualHeight()
    aspect# = (dw# / dh#)
    if aspect# = 1.0
        aspect# = getDisplayAspect()
    else
        aspect# = 1.0
    endif
    dx# = x#-getSpriteX(spriteID)
    dy# = y#-getSpriteY(spriteID)
    ang# = getSpriteAngle(spriteID)
    targ# = atanfull(dx#,dy#/aspect#)-90
    diff# = targ#-ang#
    while diff#>=180
        diff# = diff#-360
    endwhile
    while diff#<=-180
        diff# = diff#+360
    endwhile
    new# = ang# + diff# * value#
    setSpriteAngle(spriteID,new#)
endfunction



Not sure where the 2 "Forum Functions" came from but I have them in an include, thanks to whoever made them.

Now, to have fun with tanks!
Posted: 1st Aug 2021 18:09
I thought there was a command for this already, I must've written my own back in the day when I did a Spriter importer (which is no longer necessary)
Posted: 2nd Aug 2021 20:34
Very nice matey.
FixTextToSprite() and FixEditBoxToSprite() would be nice as well. For the UI warriors
Posted: 2nd Aug 2021 23:04
That's a good idea, group a bunch of items to a sprite and tween it ... I like that.
Posted: 2nd Aug 2021 23:59
What would also be super brilliant is if you added draw commands relative to the sprite.
So you could attach a draw command and it would draw a box,line.elipse when it synced. Again for the UI warriors
Posted: 3rd Aug 2021 16:39
Hmm, yea draw functions, I wonder how best to go about that

FixBoxToSprite
FixElipseToSprite
FixLineToSprite

FixParticalToSprite, FixParticalToObject could be useful to.

I saw Lee's GGMax video about "Smart Objects" and was thinking that although AppGameKit already has the functionally to achieve the same thing you would have to go about it in a bit of a mix n match fashion, FixObjectToObject/FixObjectToBone, fine its a start but the rest we are updating somewhere else.

FixObjectToBone // sword
FixObjectToObject // sword attachment
FixParticalToObject // sword emitter
FixObjectToBone // shield
FixParticalToObject // shield emitter
FixSpriteToObject // attach healthbar

^^ I just setup a character with a sword with attachment and particle, a shield with particle and a 2D health bar with 6 function calls
maybe even LoadObjectWithAttachments, LoadSpriteWithAttachments

A complete set of Fix* functions is certainly something I could use so I'll be adding them my local lib.