News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Simple Sprite Animation

Started by kevin, March 13, 2021, 09:01:52 PM

Previous topic - Next topic

kevin

   Simple Sprite Animation

      The following example takes sequential approach to animation of objects within a scene.   Meaning each character is given a list of instructions such as POSITION/ MOVE/ WAIT/ SINUSWAIT that it will follow one by one until complete.    

      This is more a starting position to build more robust animations than a destination, but it's here for those interesting in how you might do sprite animation, or just animate objects in general..  


 
PlayBASIC Code: [Select]
; PROJECT : 2021-03-14 - Simple Sprite Animation
; AUTHOR : Kevin Picone - http://playbasic.com
; CREATED : 14/03/2021
; EDITED : 14/03/2021
; ---------------------------------------------------------------------


// --------------------------------------------------
// Load MEDIA for demo
// --------------------------------------------------

path$="gfx\"


BACKDROP_IMAGE = loadnewimage(path$+"White-Shaded-Backdrop.png",2)
Care_IMAGE = loadnewimage(path$+"Care-Logo2.png",2)
Mecwa_IMAGE = loadnewimage(path$+"Mecwa-Logo2.png",2)
Mecwa_WIDTH = GetImageWidth(Mecwa_IMAGE)


// Create our FX screen HD in size, that we'll scale to the current
// PB screen size..
Screen=newimage(1920,1080,2)


// Create Animated Objects
framerate = 30
waittime = Framerate*1
logoYpos = 190
logoXpos = 340



// -------------------------------------------------------------------
// Create animated objects
// -------------------------------------------------------------------


// Delcare MECWQ as TOBJECT and Allocated it on the next line
Dim Mecwa as tObject
Mecwa = new tObject

// Set what image this object should use
Mecwa.image = mecwa_image

initANIM(mecwa)
AddAnim_Position(LogoXpos,LogoYpos)
AddAnim_AddWait(WaitTime)
AddAnim_SinusMove(FrameRate*1.5,-200,0)


// Create animation OBJECT and attach an Animation to it
Dim Care as tObject
Care = new tObject
Care.image =Care_image
initANIM(care)
AddAnim_Position(LogoXpos+Mecwa_WIDTH+16,LogoYpos)
AddAnim_AddWait(WaitTime)
AddAnim_SinusMove(FrameRate*1.5,200,0)







// ----------------------------------------------------------
// ---- [ MAIN Loop ] ---------------------------------------
// ----------------------------------------------------------

setfps 30
do

// start drawing scene
rendertoimage screen
drawimage backdrop_image,0,0,false

// Animate our objects
Animate(Mecwa)
Animate(Care)

// Draw the objects asa their current positions
drawimage Mecwa.image ,Mecwa.pos.x ,Mecwa.pos.y,0
drawimage care.image ,care.pos.x ,care.pos.y,0


// Show the user the display
rendertoscreen
DrawDisplay(Screen)


frame++
// loop until we reach 250 frames
loop frame> 250

end



// -------------------------------------------------------------------
// -------------------------------------------------------------------
// -------------------------------------------------------------------
// -------------------------------------------------------------------
// -------------------------------------------------------------------


// Compute relative Mouse position from our Scaled SCREEN image.

function MX#(Screen)
iw=getimagewidth(Screen)
ih=getimageheight(Screen)
sx#=float(iw)/GetScreenWidth()
x#=mousex()*sx#
EndFunction x#

function MY#(Screen)
iw=getimagewidth(Screen)
ih=getimageheight(Screen)
sx#=float(iw)/GetScreenWidth()
sy#=float(ih)/GetScreenHeight()
y#=mousey()*sy#
EndFunction y#


// -------------------------------------------------------------------
// -------------------------------------------------------------------
// -------------------------------------------------------------------
// -------------------------------------------------------------------
// -------------------------------------------------------------------


Function DrawDisplay(Screen)

// COmpute the Amount of scaling need to display FX screen
// on PB sreeb
iw=getimagewidth(Screen)
ih=getimageheight(Screen)

sx#=GetScreenWidth()/float(iw)
sy#=GetScreenHeight()/float(ih)

// draw entire screen as rotated sprite :)
drawrotatedimage Screen,0,0,angle#,sx#,sy#,0,0,false

// Read the Mouse from our FX screen..
x#=mx#(Screen)
y#=my#(Screen)
ink 0
Login required to view complete source code




Video:

   





  Download

   Attached bellow.