Personnages volants

Charger une image

load.js
const PNG = [ "btfly" , "grass" , "bean" , ]

Créer un composant

component.js
// Ajoutes ici tes propres composants
function fly( fallSpeed = 200 , magnetic = true ) { // permet aux objets volants de tomber lorsqu'un objet est sur eux const magneticSpeed = 60; const stop_distance = 20; return { magneticPos: null, falling: false, id: "fly", add() { this.onCollide(() => (this.falling = true)); this.onCollideUpdate((obj, col) => this.checkFlyColission(obj, col)); this.onCollideEnd(() => (this.falling = false)); }, checkFlyColission(obj, col) { if (obj.is("body") && !obj.is("fly") && !obj.isStatic && col.isTop()) { this.move(0, fallSpeed); obj.move(0, fallSpeed); this.falling = true; } }, update() { if (magnetic) { if (this.magneticPos == null) this.magneticPos = vec2(this.pos); if (!this.falling) { const t = this.magneticPos.sub(this.pos).unit(); const d = this.pos.dist(this.magneticPos); if (d > stop_distance) this.move(t.scale(magneticSpeed)); } } }, } }

Déclarer un symbole

level.js
const levelConf = { // paramètres du niveau tileWidth: 64, tileHeight: 64, tiles: { // listes des objet à placer dans les niveaux
"w": () => [ // papillon sprite("btfly"), area(), anchor("bot"), body({gravityScale:0}), fly(), ],
"#": () => [ // player sprite("bean"), platformerController(), health(1), character(), area(), anchor("bot"), body(), ], "=": () => [ // bloc sprite("grass"), area(), body({ isStatic: true }), anchor("bot"), offscreen({ hide: true }), ], }, }

Placer les objets

level.js
const LEVELS = [ // liste des niveaux du jeu [ " === B " , " B" , " B " , " B" , " B " , " B " , " # " , "===== " , ], ];

la vittesse de chute

fly( 200 )

si le personnage retourne à sa position

fly( 200 , true )

avec un personnage qui suit les joueurs

Personnages volants qui suivent les joueurs

avec un block en mouvement

Blocs en mouvement qui volent