movingCycle
Permet à un objet de déplacer dans une direction puis de retourner à sa position initiale
require
sprite()opacity()parameters
| Parameter | Default Value | Type | Description |
|---|---|---|---|
direction | vec2 | vec2(1, 0) | |
speed | number | 100 | |
distance | number | 300 | |
frequence | number | 3 | |
fadeInDelay | number | 0.6 | |
fadeOutDelay | number | 0.6 | |
respawnDelay | number | 0.2 | |
gridSize | number | 64 |
example
movingCycle({ direction: vec2(1, 0), speed: 100, distance: 300, frequence: 3, fadeInDelay: 0.6, fadeOutDelay: 0.6, respawnDelay: 0.2, gridSize: 64}),function movingCycle(p) { const param = { direction: vec2(1, 0), speed: 100, distance: 300, frequence: 3, fadeInDelay: 0.6, fadeOutDelay: 0.6, respawnDelay: 0.2, gridSize: 64, ...p } return { require: ["sprite", "opacity"], startPos: null, endCycle: false, direction: vec2(param.direction).unit(), add() { requestAnimationFrame(() => { this.startPos = this.pos let v = param.distance / (((this.pos.y / param.gridSize) % param.frequence) + 1) if (this.direction.x > 0) this.flipX = true this.pos = this.pos.add(this.direction.scale(v)) }) this.on("respawn", () => { this.collisionIgnore = this.collisionIgnore.filter(function (e) { return e != "body" }) }) }, update() { this.move(this.direction.scale(param.speed)) if (this.pos.dist(this.startPos) > param.distance && !this.endCycle) { this.fadeOut(param.fadeOutDelay) this.endCycle = true wait(param.fadeOutDelay, () => { this.collisionIgnore.push("body") wait(param.respawnDelay, () => { this.opacity = 1 this.fadeIn(param.fadeInDelay) this.pos = this.startPos this.endCycle = false this.collisionIgnore = this.collisionIgnore.filter(function (e) { return e != "body" }) }) }) } }, }}