LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Basic top down RPG movement?

Root / Programming Questions / [.]

NoxusCreated:
I've struggled with this in the past, can someone help with a basic 4-way directional movement program? (Think Pokémon). I can do all the animations myself, but making the walking feel smooth is something I can't seem to do unless I abused vsync which just ends up with a shitty program. Edit: I don't need collision, and if I wasn't clear I want it to have "grid-like" movement, but the transitions between moving tiles to be smooth. In Pokémon its all grid based, but when you move you don't "teleport" to the next tile; theres a smooth walking transition.

I used SPANIM and my own command BGSCRALL to accomplish it. BGSCRALL is basically just this: DEF BGSCRALL TIME FOR I=0 TO 3 BGANIM I,"XY",TIME (OR -TIME, CAN'T REMEMBER),X*16,Y*16 NEXT END and so I did that when you were moving inside of the map and not on the boundaries. For when you're on the boundaries, I just detected it (example X==0 or X==39; if BUTTON()==0 then go up) Then I subtracted from your X or Y (the ones for the player, not the map) then of course used SPANIM XY to have smooth transitions. I used 2 variables for each X and Y: Detecting the location of the map and the location of the player. Of course the movement in my program isn't the best and I'm trying to have it be less ice-like.

Have you tried using SPANIM? It's the perfect way to create transitions for sprites, including position and display. Also what's perfect is detecting if the sprite are animating. That is where SPCHK can be used. First, we need to know if the sprite is not moving before we can assign a movement. We can use IF and SPCHK for this, assuming the sprite is 0. IF SPCHK(0)==0 THEN ENDIF With that, we can assign the animation based on which direction was pressed. Not only it's a series of IF statements, but also using BUTTON(0) and what button we want to press. It's straightforward though. Here's what you need to know about SPANIM. SPANIM management number, "Animation target", [time, item,] . . . loop Management number refers to the number you want to control your sprite. I assume you used SPSET before all of this. "Animation target" is what animation we want to control for the sprite. "XY" is the position and "UV" is the display according to the sprite sheet from the graphic page for sprites. Time is the amount of frames it will take for one part of the animation to be done. However, if you use a negative value, you can make a smooth transition. We will be using negative numbers for the "XY" part, and positive numbers for "UV" ("UV" doesn't need a smooth transition). Item is the value of what will transition to. For a 16x16 grid, we can move the sprite by 16, depending on what direction we want. We can move the "UV" part to change the appearance of the sprite. Loop is the number of loops the animation can make. We only need one loop for this. Let's say that the sprite you want to control is 0, and you have variables for X,Y,U,and V. Let's say that your sprite sheet shows the animation from left to right. Also, the sprite will be moving right at 8 frames per second, and it loops once. It will look something like this. SPANIM 0,"XY",-8,X-16,Y,1 SPANIM 0,"UV",2,U+16,V,2,U+32,V,2,U+48,U,V,2,U,V,1 For the "XY" part, we only need one action, and it will last 8 frames. Since it's a negative number, the movement will be smooth. For the "UV" part, it depends on how many frames does the animation contains in the sprite sheet. Assuming it's 4, we assign 4 actions that last 2 frames (4+2=8). The time is a positive number, so it just jumps from one display to the next. Now that we better understand this, I can show you what would it look like in the end. IF SPCHK(0)==0 THEN IF BUTTON(0) AND #UP THEN SPANIM 0,"XY",-8,X,Y-16,1 SPANIM 0,"UV"2,U+16,V,2,U+32,V,2,U+48,V,2,U,V,1 ELSEIF BUTTON(0) AND #DOWN THEN SPANIM 0,"XY",-8,X,Y+16,1 SPANIM 0,"UV"2,U+16,V+16,2,U+32,V+16,2,U+48,V+16,2,U,V+16,1 ELSEIF BUTTON(0) AND #LEFT THEN SPANIM 0,"XY",-8,X-16,Y,1 SPANIM 0,"UV"2,U+16,V+32,2,U+32,V+32,2,U+48,V+32,2,U,V+32,1 ELSEIF BUTTON(0) AND #RIGHT THEN SPANIM 0,"XY",-8,X+16,Y,1 SPANIM 0,"UV"2,U+16,V+48,2,U+32,V+48,2,U+48,V+48,2,U,V+48,1 ENDIF ENDIF You may need to update the positions. There's a way with using SPOFS OUT. You can place this line that at the beginning of the first IF statement. SPOFS OUT X,Y There is a bit more complicated but effective method for all of this, but I think it's good enough. I never tried it before, though. You can test it out if you like.

Have you tried using SPANIM? It's the perfect way to create transitions for sprites, including position and display. Also what's perfect is detecting if the sprite are animating. That is where SPCHK can be used. First, we need to know if the sprite is not moving before we can assign a movement. We can use IF and SPCHK for this, assuming the sprite is 0. IF SPCHK(0)==0 THEN ENDIF With that, we can assign the animation based on which direction was pressed. Not only it's a series of IF statements, but also using BUTTON(0) and what button we want to press. It's straightforward though. Here's what you need to know about SPANIM. SPANIM management number, "Animation target", [time, item,] . . . loop Management number refers to the number you want to control your sprite. I assume you used SPSET before all of this. "Animation target" is what animation we want to control for the sprite. "XY" is the position and "UV" is the display according to the sprite sheet from the graphic page for sprites. Time is the amount of frames it will take for one part of the animation to be done. However, if you use a negative value, you can make a smooth transition. We will be using negative numbers for the "XY" part, and positive numbers for "UV" ("UV" doesn't need a smooth transition). Item is the value of what will transition to. For a 16x16 grid, we can move the sprite by 16, depending on what direction we want. We can move the "UV" part to change the appearance of the sprite. Loop is the number of loops the animation can make. We only need one loop for this. Let's say that the sprite you want to control is 0, and you have variables for X,Y,U,and V. Let's say that your sprite sheet shows the animation from left to right. Also, the sprite will be moving right at 8 frames per second, and it loops once. It will look something like this. SPANIM 0,"XY",-8,X-16,Y,1 SPANIM 0,"UV",2,U+16,V,2,U+32,V,2,U+48,U,V,2,U,V,1 For the "XY" part, we only need one action, and it will last 8 frames. Since it's a negative number, the movement will be smooth. For the "UV" part, it depends on how many frames does the animation contains in the sprite sheet. Assuming it's 4, we assign 4 actions that last 2 frames (4+2=8). The time is a positive number, so it just jumps from one display to the next. Now that we better understand this, I can show you what would it look like in the end. IF SPCHK(0)==0 THEN IF BUTTON(0) AND #UP THEN SPANIM 0,"XY",-8,X,Y-16,1 SPANIM 0,"UV"2,U+16,V,2,U+32,V,2,U+48,V,2,U,V,1 ELSEIF BUTTON(0) AND #DOWN THEN SPANIM 0,"XY",-8,X,Y+16,1 SPANIM 0,"UV"2,U+16,V+16,2,U+32,V+16,2,U+48,V+16,2,U,V+16,1 ELSEIF BUTTON(0) AND #LEFT THEN SPANIM 0,"XY",-8,X-16,Y,1 SPANIM 0,"UV"2,U+16,V+32,2,U+32,V+32,2,U+48,V+32,2,U,V+32,1 ELSEIF BUTTON(0) AND #RIGHT THEN SPANIM 0,"XY",-8,X+16,Y,1 SPANIM 0,"UV"2,U+16,V+48,2,U+32,V+48,2,U+48,V+48,2,U,V+48,1 ENDIF ENDIF You may need to update the positions. There's a way with using SPOFS OUT. You can place this line that at the beginning of the first IF statement. SPOFS OUT X,Y There is a bit more complicated but effective method for all of this, but I think it's good enough. I never tried it before, though. You can test it out if you like.
Wow thanks for this, the more I read the more I see just how much has changed between Petit computer and smilebasic. Everything you said here makes sense, and its a lot more compact that what I've been trying. Only thing I don't get, is the actual animating using UV. What's the difference between U and V? And why is it incrementing by a lot? (+16, +32, etc). I'm sure you explained it but for some reason I can't wrap my head around it. I really appreciate the post by the way!

Never mind, I understand all of it, thanks again

I figured that out here's key QKS4D3SD