I just wanted to make my animated plane a bit neater, but got stuck: When I transfer the type code to a function, the code simply refuses to work!
// Project: test_planeAni
// Created: 2018-05-16
// Used to display a sprite sheet animation on a 3D plane
/*****************************************************************************
INIT STUFF
*****************************************************************************/
#option_Explicit
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "test_planeAni" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 60, 0 )
SetScissor( 0,0,0,0 )
UseNewDefaultFonts( 1 )
/*****************************************************************************
TYPE
*****************************************************************************/
Type planeAniationType
numberOfFramesHori as integer // set this to the number of frames horizontally in the spritesheet
numberOfFramesVert as integer // set this to the number of frames vertically in the spritesheet
numberOfFrames as integer // you will need to manually count the number of frames in the spritesheet
activeFrame as integer // the currently active animation frame
horiPos as integer // used to store the horizontal position of the active frame
vertPos as integer // used to store the vertical position of the active frame
scaleU as float // horizontal scale 1.0=normal --- smaller values upscales graphic --- larger values downscale and repeats graphic
scaleV as float // vertical scale
PlaneID as integer // The plane ID
imgID as Integer // The planes image ID
EndType
global okay_t As planeAniationType
okay_t.numberOfFramesHori = 8 // set this to the number of frames horizontally in the spritesheet
okay_t.numberOfFramesVert = 13 // set this to the number of frames vertically in the spritesheet
okay_t.numberOfFrames = 101 // you will need to manually count the number of frames in the spritesheet
okay_t.activeFrame = 0 // (keep values)
okay_t.scaleU = 1.0 / okay_t.numberOfFramesHori // scales to the width of a single frame
okay_t.scaleV = 1.0 / okay_t.numberOfFramesVert // scales to the height of a single frame
okay_t.planeID = CreateObjectPlane (2.5, 1.0) // Set it to someting with the same aspect ratio as the frames of your animation
SetObjectPosition (okay_t.planeID, 0.0, 0.0, 5.0) // Position the plane
setObjectColor (okay_t.planeID, 255,255,255,255) // (keep values)
okay_t.imgID = LoadImage("confirmAni.png") // (Set it to your sprite sheet file)
setObjectImage (okay_t.planeID, okay_t.imgID, 0) // (keep values)
SetObjectMeshUVScale( okay_t.planeID, 1, 0, okay_t.scaleU, okay_t.scaleV ) // (keep values)
/*****************************************************************************
CAMERA MOVEMENT STUFF
*****************************************************************************/
#constant TRUE 1
#constant FALSE 0
#constant KEY_A 65
#constant KEY_S 83
#constant KEY_D 68
#constant KEY_W 87
#constant KEY_Q 81
#constant KEY_E 69
camX as float = 0.0
camY as float = 0.0
camZ as float = 0.0
camMove as float = 0.2
/*****************************************************************************
MAIN LOOP
*****************************************************************************/
do
/*
// automatically loops the animation
inc okay_t.activeFrame
if okay_t.activeFrame => okay_t.numberOfFrames then okay_t.activeFrame = 0
// Returns the remainder of the integer division activeFrame/numberOfFramesHori
// which gives us the horizontal position
okay_t.horiPos = Mod( okay_t.activeFrame, okay_t.numberOfFramesHori )
// gives us the vertical position
okay_t.vertPos = Trunc( okay_t.activeFrame/okay_t.numberOfFramesHori )
// animation!
SetObjectMeshUVOffset( okay_t.planeID, 1, 0, okay_t.horiPos, okay_t.vertPos )
*/
// This function should do the commented-out stuff above ... but it don't!
incPlaneAnimation (okay_t)
// Camera movement stuff
if GetRawKeyState( KEY_A ) = TRUE then camX = camX-camMove
if GetRawKeyState( KEY_D ) = TRUE then camX = camX+camMove
if GetRawKeyState( KEY_S ) = TRUE then camZ = camZ-camMove
if GetRawKeyState( KEY_W ) = TRUE then camZ = camZ+camMove
if GetRawKeyState( KEY_Q ) = TRUE then camY = camY-camMove
if GetRawKeyState( KEY_E ) = TRUE then camY = camY+camMove
setCameraPosition (1, camX, camY, camZ)
SetCameraLookAt (1, getObjectX(okay_t.planeID), getObjectY(okay_t.planeID), getObjectZ(okay_t.planeID), 0)
Sync()
loop
/*****************************************************************************
FUNCTIONS
*****************************************************************************/
function incPlaneAnimation (animationID as planeAniationType)
// automatically loops the animation
inc animationID.activeFrame
if animationID.activeFrame => animationID.numberOfFrames then animationID.activeFrame = 0
// Returns the remainder of the integer division activeFrame/numberOfFramesHori
// which gives us the horizontal position
animationID.horiPos = Mod( animationID.activeFrame, animationID.numberOfFramesHori )
// gives us the vertical position
animationID.vertPos = Trunc( animationID.activeFrame/animationID.numberOfFramesHori )
// animation!
SetObjectMeshUVOffset( animationID.planeID, 1, 0, animationID.horiPos, animationID.vertPos )
endfunction