Okay, I think I got the basic idea down. Two versions here for you to test out:
One that moves +1, 0, -1, 0, etc.
SHAKE_MAGNITUDE = 5
class Sprite_Battler < RPG::Sprite
alias init_before_setting_counter initialize
def initialize(viewport, battler = nil)
init_before_setting_counter(viewport, battler)
@shake_frame_count = 0
end
alias call_update_before_shaking_sprite update
def update
# Check if damage being done and damage pop is true
unless @shake_frame_count > 0 or @battler.is_a?(Game_Actor)
if @battler.damage.is_a?(Numeric) and @battler.damage > 0 and @battler.damage_pop
@shake_frame_count = 20
end
end
# Call update alias
call_update_before_shaking_sprite
# Shaking the sprite
if @shake_frame_count > 0
move_amount = 0
if @shake_frame_count % 4 == 0
move_amount = SHAKE_MAGNITUDE
elsif (@shake_frame_count+2) % 4 == 0
move_amount = -SHAKE_MAGNITUDE
end
self.x = @battler.screen_x + move_amount
@shake_frame_count -= 1
end
end
end
One that moves +1, +2, +1, 0, -1, -2, -1, 0, etc.
SHAKE_MAGNITUDE = 5
class Sprite_Battler < RPG::Sprite
alias init_before_setting_counter initialize
def initialize(viewport, battler = nil)
init_before_setting_counter(viewport, battler)
@shake_frame_count = 0
end
alias call_update_before_shaking_sprite update
def update
# Check if damage being done and damage pop is true
unless @shake_frame_count > 0 or @battler.is_a?(Game_Actor)
if @battler.damage.is_a?(Numeric) and @battler.damage > 0 and @battler.damage_pop
@shake_frame_count = 20
end
end
# Call update alias
call_update_before_shaking_sprite
# Shaking the sprite
if @shake_frame_count > 0
move_amount = 0
if (@shake_frame_count+1) % 4 == 0
if (@shake_frame_count+1) / 4 % 2 == 1
move_amount = SHAKE_MAGNITUDE * 2
else
move_amount = -SHAKE_MAGNITUDE * 2
end
elsif [0,2].include?((@shake_frame_count+2) % 4)
if (@shake_frame_count+2) / 4 % 2 == 1
move_amount = SHAKE_MAGNITUDE
else
move_amount = -SHAKE_MAGNITUDE
end
end
self.x = @battler.screen_x + move_amount
@shake_frame_count -= 1
end
end
end
I did the calculations based on 20 frames of motion--I'm pretty sure that changing this value in the script will cause errors.