Arrière plan

pour Sullivan

Charger une image

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

Créer un nouveau composant

component.js
// Ajoutes ici tes propres composants
function movingBackground( scaling = 1.4 , movement = 0.1 ) { const x = width() / 2 - ( width() / 2 ) * scaling const y = height() / 2 - ( height() / 2 ) * scaling const ax = ( width() * movement ) / 2 const ay = ( height() * movement ) / 2 let w = 0 , h = 0 , init = false return { add() { h = LEVELS[CURRENT_LEVEL].length; for (let i = 0; i < h; i++) { let n = LEVELS[CURRENT_LEVEL][i].length if (n > w) w = n } this.scale = scaling }, update() { const camera = get( "camera" , { recursive: true } )[0] if (camera && camera.pos){ const vx = camera.pos.x / (w * levelConf.tileWidth ) - 0.5 * 2 const vy = camera.pos.y / (h * levelConf.tileHeight) - 0.5 * 2 this.pos.x = x - vx * ax this.pos.y = y - vy * ay } }, } }

Ajouter le nouvel objet dans la scène

game.js
scene("game", () => { // scène dans laquelle se déroulent les niveaux setGravity(3200) setBackground( [ 141 , 183 , 255 ] )
const background = add( [ sprite( "background" , { width : width() , height : height() } ) , pos(0,0), scale(), fixed(), movingBackground(), ] )
const level = addLevel( LEVELS[ CURRENT_LEVEL ] , levelConf ) const camera = add( [ multiplayerCamera( ) ] ) if (debugmode) debug.inspect = true })

l'échelle de l'arrière plan

movingBackground( 1.4 )

la vitesse de déplacement de l'arrière-plan

movingBackground( 1.4 , 0.1 )