none
animating the player RRS feed

  • Question

  • in small basic i have the player ruining down the screen i have 7 sprites for this player and i use this code to try to make it look like he is runing 

    While level = "greenhillzone"
              sonic = Shapes.AddImage(S1)
              Shapes.Remove(sonic)
              sonic = Shapes.AddImage(S2)
              Shapes.Remove(sonic)
              sonic = Shapes.AddImage(S3)
              Shapes.Remove(sonic)
              sonic = Shapes.AddImage(S4)
              Shapes.Remove(sonic)
              sonic = Shapes.AddImage(S5)
              Shapes.Remove(sonic)
              sonic = Shapes.AddImage(S6)
              Shapes.Remove(sonic)
              sonic = Shapes.AddImage(S7)
              Shapes.Remove(sonic)
              EndWhile

    but when i do this he flutters in and out of existence!

    can someone please help? 

    Friday, September 5, 2014 3:24 PM

Answers

  • Hello TheCliffStudios, 

    QPG365

    Type in this code above to get a sprite animation prototype by litDiv. One part of this program that you'll need to change to accommodate your sprites is included below. 

    dir = Program.Directory+"\sidestroller\"
    For i = 1 To 3
      player1WalkLeft[i] = Shapes.AddImage(ImageList.LoadImage(dir+"walkL"+i+".png"))
      Shapes.HideShape(player1WalkLeft[i])
    
      player1WalkRight[i] = Shapes.AddImage(ImageList.LoadImage(dir+"walkR"+i+".png"))
      Shapes.HideShape(player1WalkRight[i])
    EndFor

    Another part that you'll need to change is in this sample is this line. You'll need to change the 3 in Math.Remainder() to the number of sprites your are cycling in the "right" direction, which in your case is 7. 

    player1Frame = 1+Math.Remainder(player1Frame,3) 'Cycle player image in group (these for walking left)
    If everything works accordingly, you should move your sprite (and initiate the sprite animation) with the right key. You may want to comment out the code for the left key until you everything works out for your right sprite animation. You'll notice it won't move very fast. To change the overall frames per second and pixel movement of your sprite then you'll need to edit this part of the program, which I posted below. 

    'Move players
    'player1 is the current active shown image (there is only ever one not hidden at a time)
    Sub movePlayers
      If (keyDown["Left"]) Then
        player1X = player1X-2 'Move player position left
        If (Math.Remainder(iFrame,8) = 0) Then 'Update player every 8 frames of movement
          Shapes.HideShape(player1) 'Hide current player image (will be last player image)
          player1Frame = 1+Math.Remainder(player1Frame,3) 'Cycle player image in group (these for walking left)
          player1 = player1WalkLeft[player1Frame] 'The next player image in group is now the current player image
          Shapes.Move(player1,player1X,player1Y) 'Move the image before we show it
          Shapes.ShowShape(player1) 'Show current player image
        Else
          Shapes.Move(player1,player1X,player1Y) 'Just move player
        EndIf
        iFrame = iFrame+1
      ElseIf (keyDown["Right"]) Then
        player1X = player1X+2
        If (Math.Remainder(iFrame,8) = 0) Then
          Shapes.HideShape(player1)
          player1Frame = 1+Math.Remainder(player1Frame,3)
          player1 = player1WalkRight[player1Frame]
          Shapes.Move(player1,player1X,player1Y) 'Move the image before we show it
          Shapes.ShowShape(player1) 'Show current player image
        Else
          Shapes.Move(player1,player1X,player1Y)
        EndIf
        iFrame = iFrame+1
      Else
        iFrame = 0
      EndIf
    EndSub





    • Edited by Ezra94 Saturday, September 6, 2014 6:03 AM Reword
    • Proposed as answer by litdevModerator Sunday, September 7, 2014 2:27 PM
    • Marked as answer by WhTurner33Editor Friday, September 12, 2014 5:34 PM
    Saturday, September 6, 2014 1:24 AM

All replies

  • I think that the whileloop runs too fast.

    Try what happens if you insert a Program.Delay(50) after each AddImage, before removing the shape.. 


    Jan [ WhTurner ] The Netherlands


    Friday, September 5, 2014 5:38 PM
    Answerer
  • I think that the whileloop runs too fast.

    Try what happens if you insert a Program.Delay(50) after each AddImage, before removing the shape.. 


    Jan [ WhTurner ] The Netherlands


    now we dose the same but skipping frames
    Friday, September 5, 2014 7:00 PM
  • Don't create and remove the shapes for each frame, create them all and use Shapes.HideShape and Shapes.ShowShape to show them or not in order - much faster to just show/hide than to create and remove completely - you will then need the delay as WhTurner33 suggests.

    Perhaps also us an array to hold the images.

    Untested since I don't have the images, but maybe like this:

    'Assuming S[i] is array of ImaeList loaded images
    For i = 1 To 7
      sonic[i] = Shapes.AddImage(S[i])
      Shapes.HideShape(sonic[i]) 'Hide them all to start with
    EndFor
    img = 1
    
    While level = "greenhillzone"
      imgNext = img+1
      If (imgNext > 7) Then
        imgNext = 1
      EndIf
      Shapes.HideShape(sonic[img])
      Shapes.ShowShape(sonic[imgNext])
      img = imgNext
      
      Program.Delay(50)
    EndWhile
    This isn't the final solution, just a starting idea, you will need to move them as well etc.
    Friday, September 5, 2014 7:17 PM
    Moderator
  • thanks can i ask how to set up the array for variable S?
    Friday, September 5, 2014 7:22 PM
  • Perhaps like this if the images are named image1.png image2.png etc.

    For i = 1 To 7
      S[i] = ImageList.LoadImage(Program.Directory+"\image"+i+".png")
      sonic[i] = Shapes.AddImage(S[i])
      Shapes.HideShape(sonic[i]) 'Hide them all to start with
    EndFor

    Or just

    S[1] = ImageList.LoadImage(...)

    S[2] = ...

    Friday, September 5, 2014 7:35 PM
    Moderator
  • Hello TheCliffStudios, 

    QPG365

    Type in this code above to get a sprite animation prototype by litDiv. One part of this program that you'll need to change to accommodate your sprites is included below. 

    dir = Program.Directory+"\sidestroller\"
    For i = 1 To 3
      player1WalkLeft[i] = Shapes.AddImage(ImageList.LoadImage(dir+"walkL"+i+".png"))
      Shapes.HideShape(player1WalkLeft[i])
    
      player1WalkRight[i] = Shapes.AddImage(ImageList.LoadImage(dir+"walkR"+i+".png"))
      Shapes.HideShape(player1WalkRight[i])
    EndFor

    Another part that you'll need to change is in this sample is this line. You'll need to change the 3 in Math.Remainder() to the number of sprites your are cycling in the "right" direction, which in your case is 7. 

    player1Frame = 1+Math.Remainder(player1Frame,3) 'Cycle player image in group (these for walking left)
    If everything works accordingly, you should move your sprite (and initiate the sprite animation) with the right key. You may want to comment out the code for the left key until you everything works out for your right sprite animation. You'll notice it won't move very fast. To change the overall frames per second and pixel movement of your sprite then you'll need to edit this part of the program, which I posted below. 

    'Move players
    'player1 is the current active shown image (there is only ever one not hidden at a time)
    Sub movePlayers
      If (keyDown["Left"]) Then
        player1X = player1X-2 'Move player position left
        If (Math.Remainder(iFrame,8) = 0) Then 'Update player every 8 frames of movement
          Shapes.HideShape(player1) 'Hide current player image (will be last player image)
          player1Frame = 1+Math.Remainder(player1Frame,3) 'Cycle player image in group (these for walking left)
          player1 = player1WalkLeft[player1Frame] 'The next player image in group is now the current player image
          Shapes.Move(player1,player1X,player1Y) 'Move the image before we show it
          Shapes.ShowShape(player1) 'Show current player image
        Else
          Shapes.Move(player1,player1X,player1Y) 'Just move player
        EndIf
        iFrame = iFrame+1
      ElseIf (keyDown["Right"]) Then
        player1X = player1X+2
        If (Math.Remainder(iFrame,8) = 0) Then
          Shapes.HideShape(player1)
          player1Frame = 1+Math.Remainder(player1Frame,3)
          player1 = player1WalkRight[player1Frame]
          Shapes.Move(player1,player1X,player1Y) 'Move the image before we show it
          Shapes.ShowShape(player1) 'Show current player image
        Else
          Shapes.Move(player1,player1X,player1Y)
        EndIf
        iFrame = iFrame+1
      Else
        iFrame = 0
      EndIf
    EndSub





    • Edited by Ezra94 Saturday, September 6, 2014 6:03 AM Reword
    • Proposed as answer by litdevModerator Sunday, September 7, 2014 2:27 PM
    • Marked as answer by WhTurner33Editor Friday, September 12, 2014 5:34 PM
    Saturday, September 6, 2014 1:24 AM