Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Android / Assistance with sprite movement/animation requested

Author
Message
Nationalcrafter67
User Offline
Joined: 6th Oct 2018
Location:
Posted: 8th Oct 2018 20:23
Currently developing a game, and have hit a speed bump. I'm sure that it's just a simple fix too, nonetheless, I'm stumped. I have four sprite animations loaded, and the animations function properly when GetRawKeyPressed is True. However, I need the previous animation that was playing, to be deleted and replaced @ the same location with the next sprite that is triggered by GetRawKeyPressed. So lets say that the current sprite that is using the down walking animation, "In this case it is CreateSprite ( 1, 0 )", needs to be replaced by the left walking animation when GetRawKeyPressed(65) is True. How would one preform this function? Thanks in advance for any help that is offered.
Nationalcrafter67
User Offline
Joined: 6th Oct 2018
Location:
Posted: 18th Oct 2018 21:50
This has been solved, after a solid 15 hour sit down. As I thought, the solution was quite simple. I'll post the code below for anyone else who has this issue in the future.



//Sets the down walking animation stopstart
if GetRawKeyPressed(83)
CreateSprite(1,1)
AddSpriteAnimationFrame ( 1, LoadImage ( "Down(1).png" ) )
AddSpriteAnimationFrame ( 1, LoadImage ( "Down(2).png" ) )
AddSpriteAnimationFrame ( 1, LoadImage ( "Down(3).png" ) )
AddSpriteAnimationFrame ( 1, LoadImage ( "Down(4).png" ) )
PlaySprite ( 1, 10, 1, 1, 4 )
DeleteSprite(7)
DeleteSprite(11)
DeleteSprite(15)


elseif GetRawKeyReleased(83)
StopSprite(1)
endif

//walkleft

if GetRawKeyPressed(65)
CreateSprite(7,7)
AddSpriteAnimationFrame ( 7, LoadImage ( "Left(1).png" ) )
AddSpriteAnimationFrame ( 7, LoadImage ( "Left(2).png" ) )
AddSpriteAnimationFrame ( 7, LoadImage ( "Left(3).png" ) )
AddSpriteAnimationFrame ( 7, LoadImage ( "Left(4).png" ) )
PlaySprite(7, 10, 1, 1, 4)
DeleteSprite(1)
DeleteSprite(11)
DeleteSprite(15)


elseif GetRawKeyReleased(65)
StopSprite(7)

endif
//walkright
if GetRawKeyPressed(68)
CreateSprite(11,11)
AddSpriteAnimationFrame (11, LoadImage("Right(0).png"))
AddSpriteAnimationFrame (11, LoadImage("Right(1).png"))
AddSpriteAnimationFrame (11, LoadImage("Right(2).png"))
AddSpriteAnimationFrame (11, LoadImage("Right(3).png"))
PlaySprite(11,10,1,1,4)
DeleteSprite(1)
DeleteSprite(7)
DeleteSprite(15)


elseif GetRawKeyReleased(68)
StopSprite(11)

endif

//walkup
if GetRawKeyPressed(87)
CreateSprite(15,15)
AddSpriteAnimationFrame (15, LoadImage("Up(0).png"))
AddSpriteAnimationFrame (15, LoadImage("Up(1).png"))
AddSpriteAnimationFrame (15, LoadImage("Up(2).png"))
AddSpriteAnimationFrame (15, LoadImage("Up(3).png"))
PlaySprite(15,10,1,1,4)
DeleteSprite(1)
DeleteSprite(7)
DeleteSprite(11)


elseif GetRawKeyReleased(87)
StopSprite(15)

endif
smallg
Valued Member
13
Years of Service
User Offline
Joined: 8th Dec 2005
Location: steam
Posted: 19th Oct 2018 22:45 Edited at: 19th Oct 2018 22:54
Creating/deleting sprites and loading images at runtime like this is not good practice, you'll get memory leaks and potential crashes if you're not careful (not to mention it's way slower to process).
For example I don't see anywhere in there where you are deleting or checking those images aren't already loaded.

You should create your sprite and assign all the animations before the main loop, then call the desired frames depending on the direction he is walking.
I think you have done it your way because you wanted the animation frames to keep the same slots/frame numbers? Why? You could simply add an offset based on the direction.
I.e. right is 0, left is 4, up is 8 and down is 12....
Then depending on your keypress you use the same code but change the offset and add the offset to the frames.
So
PlaySprite(id,10,1,1+offset,4+offset)
life's one big game
spec= 4ghz, 16gb ram, AMD R9 2700 gpu

Login to post a reply

Server time is: 2018-12-17 13:32:39
Your offset time is: 2018-12-17 13:32:39