Language…
22 users online: crocodileman94, D1STYNCT, DanMario24YT, Dennsen86, deported, Domokun007, GamesInTweed, Green, GRIMMKIN, Heitor Porfirio,  idol, Knetog, Maw, Nayfal,  Noivern, Pink Gold Peach, ppp9q, Silver_Revolver, test212,  Thomas, VSmario90, yoshiatom - Guests: 304 - Bots: 294
Users: 64,795 (2,374 active)
Latest user: mathew

Getting a sprite to stay on ledges

I am attempting to have the Buster Beetle stay on ledges and it somewhat works aside from two issues. Upon inserting the code, the sprite no longer faces the player and goes in the opposite direction and it clips into the ground for a brief second whenever it turns around.
Here is the ASM file I'm using. Is there any way to fix these issues?
Don't eat the sandwich
I assume the code responsible for turning the beetle on edges is this, right?

Code
SUB_LEDGES:
	LDA $1588,x
	AND #$04
	BNE +
	LDA $157C,x
	EOR #$01
	STA $157C,x
+	RTS


If so, there's some more stuff you've got to do than just flipping the direction. You also need to convert the sprite's X speed from positive to negative and viceversa:

Code
    
    LDA !sprite_speed_x,x
    EOR #$FF : INC
    STA !sprite_speed_x,x


!sprite_speed_x is $B6 in a non SA-1 ROM.

This, however, still won't make the sprite stay on ledges. You'll also need to execute any code that changes the X speed only if the sprite is on ground. I suggest doing something like this:

Code
    
    LDA !1588,x
    AND #$04
    BNE .onGround
    
.onAir:
    LDA !157C,x
    EOR #$01
    STA !157C,x
    LDA !B6,x
    EOR #$FF : INC
    STA !B6,x
    BRA .continue
    
.onGround
    LDY !157C,x                     ; \  set x speed
    LDA X_Speed,y                   ;  | depending on
    STA !B6,x                       ; /  direction
    STZ !AA,x

.continue


Notice that I've added "STZ !AA,x". This keeps the sprite's Y speed to zero on ground (it's also how you get a better falling if the sprite is supposed to fall on edges).
Now, this code will be enough to make the sprite stay on ledges, but, it will behave badly on slopes. To have the sprite behave correctly, you'll need to do a few more checks. Specifically, if the sprite is on ground, you need to check if it's on a slope or on layer 2, and if it is, adjust its Y speed. I found this code to work pretty well:

Code
    
    LDA !1588,x
    AND #$04
    BNE .onGround
    
.onAir:
	LDA !157C,x
	EOR #$01
	STA !157C,x
    LDA !B6,x
    EOR #$FF : INC
    STA !B6,x
    BRA .continue

.onGround:
    LDA !1588,x                     ;\
    BMI .onL2orSlope                ; |
    LDA !15B8,x                     ; |
    BEQ .onFlatGround               ; | Adjust Y speed accordingly
.onL2orSlope                        ; |
    LDA #$18                        ; |
.onFlatGround                       ; |
    STA !AA,x                       ;/
    LDY !157C,x                     ; \  set x speed
    LDA X_Speed,y                   ;  | depending on
    STA !B6,x                       ; /  direction    
    
.continue


You can check this thread for (slightly) more details.
If you want to add this code to your sprite, remember to remove that "STZ !B6,x" instruction after the call to SUB_LEDGES, as it's useless with this code. (You can find the edited code here, it should work with a non SA-1 ROM).
Unfortunately, I wasn't able to figure out why the sprite doesn't face the player. Chances are it's unrelated to the SUB_LEDGES routine.
Originally posted by RudeGuy
I assume the code responsible for turning the beetle on edges is this, right?

Code
SUB_LEDGES:
	LDA $1588,x
	AND #$04
	BNE +
	LDA $157C,x
	EOR #$01
	STA $157C,x
+	RTS


If so, there's some more stuff you've got to do than just flipping the direction. You also need to convert the sprite's X speed from positive to negative and viceversa:

Code
    
    LDA !sprite_speed_x,x
    EOR #$FF : INC
    STA !sprite_speed_x,x


!sprite_speed_x is $B6 in a non SA-1 ROM.

This, however, still won't make the sprite stay on ledges. You'll also need to execute any code that changes the X speed only if the sprite is on ground. I suggest doing something like this:

Code
    
    LDA !1588,x
    AND #$04
    BNE .onGround
    
.onAir:
    LDA !157C,x
    EOR #$01
    STA !157C,x
    LDA !B6,x
    EOR #$FF : INC
    STA !B6,x
    BRA .continue
    
.onGround
    LDY !157C,x                     ; \  set x speed
    LDA X_Speed,y                   ;  | depending on
    STA !B6,x                       ; /  direction
    STZ !AA,x

.continue


Notice that I've added "STZ !AA,x". This keeps the sprite's Y speed to zero on ground (it's also how you get a better falling if the sprite is supposed to fall on edges).
Now, this code will be enough to make the sprite stay on ledges, but, it will behave badly on slopes. To have the sprite behave correctly, you'll need to do a few more checks. Specifically, if the sprite is on ground, you need to check if it's on a slope or on layer 2, and if it is, adjust its Y speed. I found this code to work pretty well:

Code
    
    LDA !1588,x
    AND #$04
    BNE .onGround
    
.onAir:
	LDA !157C,x
	EOR #$01
	STA !157C,x
    LDA !B6,x
    EOR #$FF : INC
    STA !B6,x
    BRA .continue

.onGround:
    LDA !1588,x                     ;\
    BMI .onL2orSlope                ; |
    LDA !15B8,x                     ; |
    BEQ .onFlatGround               ; | Adjust Y speed accordingly
.onL2orSlope                        ; |
    LDA #$18                        ; |
.onFlatGround                       ; |
    STA !AA,x                       ;/
    LDY !157C,x                     ; \  set x speed
    LDA X_Speed,y                   ;  | depending on
    STA !B6,x                       ; /  direction    
    
.continue


You can check this thread for (slightly) more details.
If you want to add this code to your sprite, remember to remove that "STZ !B6,x" instruction after the call to SUB_LEDGES, as it's useless with this code. (You can find the edited code here, it should work with a non SA-1 ROM).
Unfortunately, I wasn't able to figure out why the sprite doesn't face the player. Chances are it's unrelated to the SUB_LEDGES routine.

Can you please help me? I have the same objective of RollingRigatonis,
but I'm editing the Goomba of Blind Devil. I want to make a goombrat/paragoombrat Sprite.And that link gave me to nothing.
Thinking about what patcher to use.