I assume the code responsible for turning the beetle on edges is this, right?
CodeSUB_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.