Hi all,
I've been digging through the forums and trying to understand how I can make animated sprites work and I am having a lot of difficulty getting it right. Even copying working code and trying to adjust it to my needs fails.
What I'm trying to do is have player's character move around a room. The room is represented by a .png image that is split into a 7x7 grid of 30px long by 30px wide squares. The room is also represented by a 9x9 array called map(). Map() is defined below but essentially it is filled with 1s and 0s, a 1 indicating that that cell is filled and cannot be entered by the player while a 0 means the cell is empty.
As the code works right now, the player can move his character throughout the room but the sprite used is a single image (facing down) and not animated. It "jumps" between each 30px wide and long square. What I would like to happen is have the sprite animated so that it looks like the character is walking throughout the room using the Sprite Sheet "playerwalk.png".
What I need, though, when the character moves is to have its separate frames play over the 30px difference between squares so that the animation is finished playing when it reaches the next square. Then, the character sprite will be in a "standing still" position until another arrow key is pressed.
Any help or suggestions would be greatly appreciated and many thanks in advance for any.
Images used can be found below:
Player Character
Room PNG
Screenshot of Character in Room
**A brief explanation of the functions in the current code**
*move() - if a certain arrow key is pressed, the character's X (playerX(1)) or Y (playerY(1)) position is changed to reflect the movement to the next square by an increment of 30px. Also moves the character within map() by 1.
*checkGrid() - checks to see if the cell the player wants his character to move into is empty (0) or full (1). Returns a value 1 if the character is able to move in or 0 if it is not.
Sync On
Sync Rate 30
Hide Mouse
DIM map(9,9)
map(1,1)=1 : map(2,1)=1 : map(3,1)=1 : map(4,1)=1 : map(5,1)=1 : map(6,1)=1 : map(7,1)=1 : map(8,1)=1 : map(9,1)=1
map(1,2)=1 : map(2,2)=1 : map(3,2)=1 : map(4,2)=1 : map(5,2)=1 : map(6,2)=1 : map(7,2)=1 : map(8,2)=1 : map(9,2)=1
map(1,3)=1 : map(2,3)=1 : map(3,3)=1 : map(4,3)=1 : map(5,3)=0 : map(6,3)=0 : map(7,3)=0 : map(8,3)=1 : map(9,3)=1
map(1,4)=1 : map(2,4)=0 : map(3,4)=0 : map(4,4)=0 : map(5,4)=0 : map(6,4)=0 : map(7,4)=0 : map(8,4)=0 : map(9,4)=1
map(1,5)=1 : map(2,5)=0 : map(3,5)=0 : map(4,5)=0 : map(5,5)=0 : map(6,5)=0 : map(7,5)=0 : map(8,5)=0 : map(9,5)=1
map(1,6)=1 : map(2,6)=0 : map(3,6)=0 : map(4,6)=0 : map(5,6)=0 : map(6,6)=0 : map(7,6)=0 : map(8,6)=0 : map(9,6)=1
map(1,7)=1 : map(2,7)=1 : map(3,7)=0 : map(4,7)=0 : map(5,7)=0 : map(6,7)=0 : map(7,7)=1 : map(8,7)=0 : map(9,7)=1
map(1,8)=1 : map(2,8)=1 : map(3,8)=0 : map(4,8)=0 : map(5,8)=0 : map(6,8)=0 : map(7,8)=1 : map(8,8)=0 : map(9,8)=1
map(1,9)=1 : map(2,9)=1 : map(3,9)=1 : map(4,9)=1 : map(5,9)=1 : map(6,9)=1 : map(7,9)=1 : map(8,9)=1 : map(9,9)=1
DIM playerX(1) : DIM playerY(1)
DIM playerPosX(1) : DIM playerPosY(1)
playerPosX(1)=4 : playerPosY(1)=4
playerX(1)=210 : playerY(1)=60
Load Image "images/bedroom1.png",100
Load Image "images/player3.png",1
Do
Paste Image 100,150,0
Sprite 1,playerX(1),playerY(1),1
move()
Wait 100
Sync
Loop
Function move()
if (upkey()=1 AND checkGrid(playerPosX(1),playerPosY(1)-1)=1)
playerY(1)=playerY(1)-30 : playerPosY(1)=playerPosY(1)-1
EndIf
if (rightkey()=1 AND checkGrid(playerPosX(1)+1,playerPosY(1))=1)
playerX(1)=playerX(1)+30 : playerPosX(1)=playerPosX(1)+1
EndIf
if (downkey()=1 AND checkGrid(playerPosX(1),playerPosY(1)+1)=1)
playerY(1)=playerY(1)+30 : playerPosY(1)=playerPosY(1)+1
EndIf
if (leftkey()=1 AND checkGrid(playerPosX(1)-1,playerPosY(1))=1)
playerX(1)=playerX(1)-30 : playerPosX(1)=playerPosX(1)-1
EndIf
EndFunction
Function checkGrid(x,y)
if (map(x,y)=1) : value=0 : EndIf
if (map(x,y)=0) : value=1 : EndIf
EndFunction value
Sprite Sheet (3x4 - one row for each direction)