Jetpack Hero IV - Première animation du personnage
On donne un peu de vie au personnage en l’orientant à droite ou à gauche et en le modifiant légèrement quand il vole.
Orientation droite/gauche
flip_horizontally
fait partie des paramètres optionnels d’un sprite.
Sans surprise DragonRuby s’en sert pour un effet mirroir.
args.state.hero ||= {
x: 600,
y: 200,
w: 7 * HERO_SCALE,
h: 17 * HERO_SCALE,
path: 'sprites/hero-standing.png',
impulse: 0,
moving: :none,
flip_horizontally: false
}
On met à jour ce paramètre quand on déplace le personnage :
if args.state.hero.moving == :left
args.state.hero.x -= RL_SPEED
args.state.hero.flip_horizontally = false
elsif args.state.hero.moving == :right
args.state.hero.x += RL_SPEED
args.state.hero.flip_horizontally = true
end
En vol ou stationnaire
On a déjà tout ce qu’il nous faut pour modifier la représentation du personnage suivant qu’il est debout sur une plateforme ou bien en vol. Par défaut on lui colle une image “en vol”, qu’on modifie uniquement lorsqu’il aborde une plateforme par le haut :
def calc(args)
args.state.hero.path = 'sprites/hero-flying.png'
# [...]
if args.state.hero.intersect_rect?(args.state.platform)
if (x_before + args.state.hero.w) < args.state.platform.x
# [...]
elsif x_before >= (args.state.platform.x + args.state.platform.w)
# [...]
elsif ascending
# [...]
else
args.state.hero.path = 'sprites/hero-standing.png'
# [...]
end
end
end
Références
- Vous trouverez le code de Jetpack Hero sur github
- Documentation de DragonRuby
Cet article fait partie d’une série :
Commentaires
Pas encore trouvé de solution simple et non-invasive pour avoir des commentaires sur le blog. En attendant vous pouvez laisser votre Commentaire sur mastodon@lkdjiin