problem updating Animation

Advanced game related topics
dan yalte
User
User
Posts: 50
Joined: Thu Aug 13, 2015 8:56 am

problem updating Animation

Post by dan yalte »

trying to update some routines to move sprites. As what i had before was too slow. However do to the number of sprites in this case cards for a card game every time I finish moving one card into place as soon as i try to place another card to be moved the screen does strange things due to a small change in any valuables. I even tried to separate each section of code for each card movement. That does not work because when i flip the screen buffer when it goes back into the card movement routine if i skip the first part of code. Everything that code did is gone.

Do to the size of all the code i will not post that here i just need an example of moving sprites where i can move them around without previous work disappearing.THANKS
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: problem updating Animation

Post by Caronte3D »

To me, sounds like you doesn't wait for the windows events.
Try to put a: "While WindowEvent():Wend" at beginning of the main loop
User avatar
SPH
Enthusiast
Enthusiast
Posts: 561
Joined: Tue Jan 04, 2011 6:21 pm

Re: problem updating Animation

Post by SPH »

Maybe looking at the sample sprite codes provided in PB...

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
miso
Enthusiast
Enthusiast
Posts: 407
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: problem updating Animation

Post by miso »

Sounds like the animation is just an additive offset that does not update the base coordinates of your sprite. The change may disappear when you select a new card for that animation. Hard to tell without code.
dan yalte
User
User
Posts: 50
Joined: Thu Aug 13, 2015 8:56 am

Re: problem updating Animation

Post by dan yalte »

miso wrote: Wed Feb 05, 2025 4:30 pm Sounds like the animation is just an additive offset that does not update the base coordinates of your sprite. The change may disappear when you select a new card for that animation. Hard to tell without code.
No it animates just fine it gets messed up because the loop wants to rerun the code i used for the previous card. but the valuables have different values. I need an example that moves several sprites. if i try to make the loop skip the code after the first go around. All the stuff from the first run either disappears or gets messed up.

An example moving one sprite will do nothing to help. and unless i load up all the sprites with the code posting several 1000's of lines of code that i will not do. Even something that will show me how to move 3 to 5 sprites.

1 thing out of place is all it takes to mess up animation
User avatar
SPH
Enthusiast
Enthusiast
Posts: 561
Joined: Tue Jan 04, 2011 6:21 pm

Re: problem updating Animation

Post by SPH »

It's very difficult to answer you without code!!
It can be very simple or very complex. :|

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
miso
Enthusiast
Enthusiast
Posts: 407
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: problem updating Animation

Post by miso »

Example 1

Code: Select all

;-CONSTANTS
#SPRITECOUNT  = 255 ; +1 because I will use id 0 too
#SPRITESIZE   = 64
#SPRITEID     = 1
#BGCOLOR      = 8454016
#MAINWINDOW_H = 0
#WINDOWWIDTH  = 1366
#WINDOWHEIGHT = 768
#SPEEDOFCARDS = 0.01


UsePNGImageDecoder()
;-STRUCTURES
Structure simplespritestructure
  id.f
  x.f
  y.f
  targetx.f
  targety.f
EndStructure
;-GLOBALS
Global Dim spritelist.simplespritestructure(#SPRITECOUNT)

;-PROCEDURES
Procedure setupspritelist()
  For x=0 To #SPRITECOUNT
    With spritelist(x)
      \id=#SPRITEID
      \x=Random(#WINDOWWIDTH-#SPRITESIZE)
      \y=Random(#WINDOWHEIGHT-#SPRITESIZE)
      \targetx=Random(#WINDOWWIDTH-#SPRITESIZE)
      \targety=Random(#WINDOWHEIGHT-#SPRITESIZE)
    EndWith
  Next x
EndProcedure

Procedure newtarget(x)
    With spritelist(x)
      \targetx=Random(#WINDOWWIDTH-#SPRITESIZE)
      \targety=Random(#WINDOWHEIGHT-#SPRITESIZE)
    EndWith
EndProcedure
  
Procedure drawspritelist()
  For x=0 To #SPRITECOUNT
    With spritelist(x)
      DisplayTransparentSprite(\id,Round(\x,#PB_Round_Nearest),Round(\y,#PB_Round_Nearest))
    EndWith
  Next x
EndProcedure

Procedure updatespritelist()
  For x=0 To #SPRITECOUNT
    With spritelist(x)
      \x.f=\x.f*(1-#SPEEDOFCARDS)+\targetx.f*#SPEEDOFCARDS
      \y.f=\y.f*(1-#SPEEDOFCARDS)+\targety.f*#SPEEDOFCARDS
      If Round(\x,#PB_Round_Nearest)=Round(\targetx,#PB_Round_Nearest) And Round(\y,#PB_Round_Nearest)=Round(\targety,#PB_Round_Nearest)
        newtarget(x)
      EndIf
    EndWith
  Next x
EndProcedure

;-MAINPROGRAM START
InitSprite()
InitKeyboard()

OpenWindow(#MAINWINDOW_H,0,0,#WINDOWWIDTH,#WINDOWHEIGHT,"moving sprites",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#MAINWINDOW_H),0,0,#WINDOWWIDTH,#WINDOWHEIGHT,1,0,0,#PB_Screen_WaitSynchronization)


CatchSprite(#SPRITEID,?cardpng,#PB_Sprite_AlphaBlending)
ZoomSprite(#SPRITEID,#SPRITESIZE,#SPRITESIZE)
setupspritelist()

;-MAINLOOP
Repeat
  Repeat:Until Not WindowEvent()
  ExamineKeyboard()  
  ClearScreen(#BGCOLOR)
  updatespritelist()
  drawspritelist()
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)

;-DATASECTIONS
DataSection
cardpng:
Data.a $89,$50,$4E,$47,$0D,$0A,$1A,$0A,$00,$00,$00,$0D,$49,$48,$44,$52,$00,$00,$00,$10,$00,$00,$00,$10,$08,$06,$00
Data.a $00,$00,$1F,$F3,$FF,$61,$00,$00,$00,$3C,$49,$44,$41,$54,$38,$CB,$63,$60,$40,$02,$8C,$8C,$8C,$FF,$89,$C1,$0C
Data.a $D8,$00,$48,$82,$58,$80,$61,$08,$29,$9A,$B1,$1A,$32,$6A,$00,$0D,$0C,$20,$C6,$40,$9C,$06,$C0,$D8,$84,$0C,$C1
Data.a $6B,$00,$0C,$0F,$8C,$0B,$06,$26,$16,$28,$CE,$4C,$94,$64,$67,$00,$DE,$8F,$A6,$1F,$B1,$D0,$29,$1D,$00,$00,$00
Data.a $00,$49,$45,$4E,$44,$AE,$42,$60,$82
EndDataSection

Example 2

Code: Select all

;-CONSTANTS
#SPRITECOUNT   = 25 ; +1 because I will use id 0 too
#SPRITESIZE    = 64
#SPRITEID      = 1
#MOUSESPRITEID = 2
#BGCOLOR       = 8454016
#SELECTCOLOR   = 8421631
#MAINWINDOW_H  = 0
#WINDOWWIDTH   = 1366
#WINDOWHEIGHT  = 768
#SPEEDOFCARDS  = 0.1


UsePNGImageDecoder()
;-STRUCTURES
Structure simplespritestructure
  id.f
  x.f
  y.f
  targetx.f
  targety.f
EndStructure
;-GLOBALS
Global Dim spritelist.simplespritestructure(#SPRITECOUNT)
Global selectedcard = -1


;-PROCEDURES
Procedure setupspritelist()
  For x=0 To #SPRITECOUNT
    With spritelist(x)
      \id=#SPRITEID
      \x=Random(#WINDOWWIDTH-#SPRITESIZE)
      \y=Random(#WINDOWHEIGHT-#SPRITESIZE)
      \targetx=\x
      \targety=\y
    EndWith
  Next x
EndProcedure

Procedure newtarget(x)
    With spritelist(x)
      \targetx=Random(#WINDOWWIDTH-#SPRITESIZE)
      \targety=Random(#WINDOWHEIGHT-#SPRITESIZE)
    EndWith
EndProcedure
  
Procedure drawspritelist()
  For x=0 To #SPRITECOUNT
    With spritelist(x)
      DisplayTransparentSprite(\id,Round(\x,#PB_Round_Nearest),Round(\y,#PB_Round_Nearest))
      If x = selectedcard
        DisplayTransparentSprite(\id,Round(\x,#PB_Round_Nearest),Round(\y,#PB_Round_Nearest),128,#SELECTCOLOR)
      EndIf
    EndWith
  Next x
EndProcedure

Procedure updatespritelist()
  For x=0 To #SPRITECOUNT
    With spritelist(x)
      \x.f=\x.f*(1-#SPEEDOFCARDS)+\targetx.f*#SPEEDOFCARDS
      \y.f=\y.f*(1-#SPEEDOFCARDS)+\targety.f*#SPEEDOFCARDS
      If Round(\x,#PB_Round_Nearest)=Round(\targetx,#PB_Round_Nearest) And Round(\y,#PB_Round_Nearest)=Round(\targety,#PB_Round_Nearest)
        ;newtarget(x)
      EndIf
    EndWith
  Next x
EndProcedure

Procedure updatemouse()
  ExamineMouse()
  If MouseButton(#PB_MouseButton_Left)
    selectedcard=-1
    For x = 0 To #SPRITECOUNT
      With spritelist(x)
        
        If  MouseX()>\x And MouseX()<\x+#SPRITESIZE 
          If MouseY()>\y And MouseY()<\y+#SPRITESIZE
            selectedcard=x
          EndIf
        EndIf
      EndWith
    Next x
  EndIf
  
  If MouseButton(#PB_MouseButton_Right)
    If selectedcard<>-1
      spritelist(selectedcard)\targetx = MouseX()-(#SPRITESIZE/2)
      spritelist(selectedcard)\targety = MouseY()-(#SPRITESIZE/2)
      selectedcard = -1
    EndIf
  EndIf
  
  
  DisplayTransparentSprite(#MOUSESPRITEID,MouseX(),MouseY())
EndProcedure


;-MAINPROGRAM START
InitSprite()
InitKeyboard()
InitMouse()

OpenWindow(#MAINWINDOW_H,0,0,#WINDOWWIDTH,#WINDOWHEIGHT,"use mouse left and rightclick",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#MAINWINDOW_H),0,0,#WINDOWWIDTH,#WINDOWHEIGHT,1,0,0,#PB_Screen_WaitSynchronization)



CatchSprite(#MOUSESPRITEID,?mousepng,#PB_Sprite_AlphaBlending)
ZoomSprite(#MOUSESPRITEID,#SPRITESIZE,#SPRITESIZE)


CatchSprite(#SPRITEID,?cardpng,#PB_Sprite_AlphaBlending)
ZoomSprite(#SPRITEID,#SPRITESIZE,#SPRITESIZE)
setupspritelist()

;-MAINLOOP
Repeat
  Repeat:Until Not WindowEvent()
  ExamineKeyboard()  
  ClearScreen(#BGCOLOR)
  updatespritelist()
  drawspritelist()
  updatemouse()
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)

;-DATASECTIONS
DataSection
cardpng:
Data.a $89,$50,$4E,$47,$0D,$0A,$1A,$0A,$00,$00,$00,$0D,$49,$48,$44,$52,$00,$00,$00,$10,$00,$00,$00,$10,$08,$06,$00
Data.a $00,$00,$1F,$F3,$FF,$61,$00,$00,$00,$3C,$49,$44,$41,$54,$38,$CB,$63,$60,$40,$02,$8C,$8C,$8C,$FF,$89,$C1,$0C
Data.a $D8,$00,$48,$82,$58,$80,$61,$08,$29,$9A,$B1,$1A,$32,$6A,$00,$0D,$0C,$20,$C6,$40,$9C,$06,$C0,$D8,$84,$0C,$C1
Data.a $6B,$00,$0C,$0F,$8C,$0B,$06,$26,$16,$28,$CE,$4C,$94,$64,$67,$00,$DE,$8F,$A6,$1F,$B1,$D0,$29,$1D,$00,$00,$00
Data.a $00,$49,$45,$4E,$44,$AE,$42,$60,$82
EndDataSection

DataSection
mousepng:
Data.a $89,$50,$4E,$47,$0D,$0A,$1A,$0A,$00,$00,$00,$0D,$49,$48,$44,$52,$00,$00,$00,$10,$00,$00,$00,$10,$08,$06,$00
Data.a $00,$00,$1F,$F3,$FF,$61,$00,$00,$00,$3E,$49,$44,$41,$54,$38,$CB,$63,$60,$64,$64,$FC,$0F,$C2,$0C,$E4,$02,$90
Data.a $66,$10,$20,$DB,$10,$AA,$19,$40,$B6,$21,$C8,$06,$90,$65,$08,$D5,$0D,$20,$D9,$10,$9A,$18,$40,$92,$21,$B0,$84
Data.a $84,$1C,$9D,$64,$25,$2E,$AA,$A4,$87,$C1,$91,$2A,$29,$CE,$5C,$74,$07,$00,$61,$2F,$8D,$D9,$D0,$48,$39,$69,$00
Data.a $00,$00,$00,$49,$45,$4E,$44,$AE,$42,$60,$82
EndDataSection
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: problem updating Animation

Post by Caronte3D »

Nice examples! :D
miso
Enthusiast
Enthusiast
Posts: 407
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: problem updating Animation

Post by miso »

Thanks Caronte3D!

I think though OP has a point. Available 2d examples are either too simple, or extremely advanced/hard to comprehend. There is a missing gap in between.
dan yalte
User
User
Posts: 50
Joined: Thu Aug 13, 2015 8:56 am

Re: problem updating Animation

Post by dan yalte »

Well I have fixed most of it. At least i got a lot more along then i thought I would. Answer nested loops, not what i was hopping for but i am a lot closer to getting ahead. Also needed arrays. The only hard part is making sure the loops are set up right. You cant just move the sprite without letting your main code update the screen. The big issue seems to be make sure you use different valuables for anything you don't want to move when done.

Also you need to setup a loop where you have 1 Displaysprite command for each object you move as more then one will mess it up unless for a visual thing. if you use more then one Displaysprite command for 1 object of the same sprite,you get that number of copy's not what you want in a card game most likely,

I have done mostly Communications type stuff in the past. Not much Graphic stuff until now.

I also worked in Casino's for a long time. What i am working on is a blackjack simulator. Bin off it for a bit due to work.

I have it down to seven lines of code to move any given card now, not to bad for something that has to change direction a lot the screen placement is the easy part.

Thanks to all for dropping ideas
User avatar
minimy
Enthusiast
Enthusiast
Posts: 551
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: problem updating Animation

Post by minimy »

Hey miso, very good examples. Thanks for share!!
If translation=Error: reply="Sorry, Im Spanish": Endif
Post Reply