UnderwareDESIGN

PlayBASIC => Beginners => Topic started by: Fash on February 06, 2006, 04:35:27 PM

Title: Newbie animation problem solved
Post by: Fash on February 06, 2006, 04:35:27 PM
Hi,
After my post yesterday ref animations I've managed to sort it using the following code (commented)...Hope it may help someone in the future :)
It uses Kevs animstrip function, and uses cursor keys to move left/right-animation image attached.

[pbcode]
; PROJECT : spritetest
; AUTHOR  : Steve "Fash" Harrison
; CREATED : 05/02/2006
; EDITED  : 06/02/2006
OpenScreen 640,480,32,1                 ;open a screen
Dim CurrentAnim(1)                   ;define animation
Load_anim(CurrentAnim(),"gfx\walkingduck.png",8,48,48,RGB(255,0,128));load gfxstip into anim
SetFPS 60                    ;set maximum frames per second
time=Timer()                     ;create a timer
CurrentFrame=1:dir$="right"              ;set initial startframe and direction
x=100                      ;set initial x position of sprite
Do                         ;start of main loop
Cls 0                       ;clear screen
   Text 0,50,"dir="+dir$                 ;text to show which direction we're facing
; ==================================================
; Drawing Image animation frames manually
; ==================================================
DrawImage CurrentAnim(CurrentFrame),x,100,1           ;use kevs animation function
If dir$="right"                  ;if we are facing right
   If RightKey()=0                   ;and right key is not pressed
 currentframe=1                   ;set frame to standing, facing right
   EndIf
EndIf
If RightKey()=1:dir$="right"              ;if right key pressed set direction
   If Timer()-time>=60                ;wait until timer at 60 millisecs
   Inc currentFrame:x=x+2              ;increase animation frame and x position
      If x>640                  ;if x position over right edge of screen
     x=0                  ;wrap around other side
      EndIf
   time=Timer()
    EndIf
    If currentFrame>4                   ;if final anim for this direction reached
   CurrentFrame=1                   ;reset to start
    EndIf
EndIf
;===================================================  
If dir$="left"                     ;if we are facing left
   If LeftKey()=0                   ;and left key is not pressed
   currentframe=8                  ;set frame to standing facing left
   EndIf                 
EndIf
If LeftKey()=1:dir$="left"              ;if leftkey pressed set direction
    If Timer()-time>=60                ;wait until timer at 60 millisecs
   Dec currentFrame:x=x-2              ;decrease animation frame and x position
      If x< 0                  ;if x position over left edge of screen
     x=640                  ;wrap around other side
      EndIf
   time=Timer()
    EndIf
 If currentFrame<5                   ;if final anim for this direction reached
   CurrentFrame=8                   ;reset to start
    EndIf
EndIf
Sync                      ;switch buffers
Loop                      ;repeat loop until "escape" pressed

; =======================================================================
; This function loads an image strip/grid into memory then cuts it into
; set of small images. The Image indexes are stored in a passed
; array.
; =======================================================================

Function Load_Anim(AnimFrames(),File$,Frames,FrameWidth,FrameHeight,TransparentColour)
Index=GetFreeImage()
LoadImage file$,Index  

width=GetImageWidth(index)
height=GetImageHeight(index)

Dim AnimFrames(Frames)
AnimFrames(0)=Frames

For lp=1 To frames
 RenderToImage Index
 FreeImage=GetFreeImage()
 AnimFrames(lp)=FreeImage
 GetImage FreeImage,Xpos,Ypos,Xpos+Framewidth,Ypos+FrameHeight
 ImageMaskColour FreeImage,TransparentColour
 Xpos=Xpos+Framewidth
 If Xpos=>width Then Xpos=0: Ypos=Ypos+FRameHeight
Next
RenderToScreen
DeleteImage index
EndFunction
[/pbcode]