This uses a technique of taking a point within the bullet sprite and using that to move the bullet.
Note that using the AppGameKit angle calculations, it assumes that the sprite, when at angle zero, is pointing directly up
// Project: Lookat2d
// Created: 21-11-29
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Lookat2d" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// 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( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 )
DrawLine(0, 0, 0, 5, 0xff, 0xff, 0xff)
bullet = CreateSprite(GetImage(0, 0, 1, 5))
DrawEllipse(3, 3, 3, 3, 0xffffffff, 0xffffffff, 1)
DrawLine(3, 2, 3, 20, 0xff, 0xff, 0xff)
gun = CreateSprite(GetImage(0, 0, 7, 20))
SetSpriteOffset(gun, 3, 20)
SetSpritePosition(gun, 5, 40)
target = CreateSprite(CreateImageColor(0xff, 0, 0, 0xff))
setspriteSize(target, 10, 10)
SetSpritePosition(target, 100, 100)
bullets as integer[]
x as float
y as float
time as float
time = Timer()
do
if GetPointerState() and Timer() - time > 0.1
time = Timer()
b = CloneSprite(bullet)
SetSpritePositionByOffset(b, GetSpriteXByOffset(gun), GetSpriteYByOffset(gun))
SetSpriteAngle(b, GetSpriteAngle(gun))
bullets.insert(b)
endif
for i=bullets.length to 0 step -1
b = bullets[i]
x = GetWorldXFromSprite(b, 0, -5)
y = GetWorldYFromSprite(b, 0, -5)
if y > 768 or x > 1024 or GetSpriteHitTest(target, x, y)
DeleteSprite(b)
bullets.remove(i)
else
SetSpritePositionByOffset(b, x, y)
endif
next
SetSpritePositionByOffset(target, GetPointerX(), GetPointerY())
SetSpriteLookAt(gun, target)
print(bullets.length)
Sync()
loop
function SetSpriteLookAt(s1 as integer, s2 as integer)
SetSpriteAngle(s1, ATanFull(GetSpriteXByOffset(s2)-GetSpriteXByOffset(s1), GetSpriteYByOffset(s2)-GetSpriteYByOffset(s1)))
endfunction