Skip to content

Commit

Permalink
Merge pull request #28 from livewires/forever
Browse files Browse the repository at this point in the history
forever -> sprite.forever
  • Loading branch information
tjvr authored Aug 20, 2017
2 parents a2761d9 + 15cc9be commit 70ddc5f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
13 changes: 1 addition & 12 deletions docs/sheets/2-animation.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,18 +323,7 @@ Finally, if we don't destroy the bullets, eventually the game will get really sl
```
_This should go inside the bullet's `forever`, just after the code to move it._
The `destroy()` function attached to a Sprite removes it from the screen permanently.
However, we haven't quite cleaned up after ourselves--the forever block for the bullet will keep running, even though it's no longer on screen! This isn't super-important, but in larger games it might cause us to run out of memory. So let's `return false` to stop the `forever` block from running:
* We should also `return false`, to make sure the `forever` block stops.
```js
if (!bullet.isOnScreen()) {
bullet.destroy()
return false // stop this forever block
}
```
The `destroy()` function attached to a Sprite removes it from the screen permanently. This also stops any `forever` loops attached to it.
## Fin
Expand Down
47 changes: 23 additions & 24 deletions uw/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ var World = function(props) {
document.body.style.margin = '0px'
document.body.appendChild(this._wrap)
this._resize()
this._deadLoops = []

window.addEventListener('resize', () => { this._needsResize = true })
this._bindPointer()
Expand All @@ -174,7 +173,7 @@ var World = function(props) {
this.stop = this.stop.bind(this)
this.start()
}
emitter(World.prototype, ['frame', 'tap', 'drag', 'drop'])
emitter(World.prototype, ['tick', 'tap', 'drag', 'drop'])

World.prototype.start = function() {
if (this.isRunning) return
Expand All @@ -194,7 +193,9 @@ World.prototype.pause = function() {
World.prototype.stop = function() {
if (!this.isRunning) return
this.pause()
this._deadLoops = this._deadLoops.concat(this.listeners('frame'))
for (var i=0; i<sprites.length; i++) {
sprites[i].destroy()
}

window.removeEventListener('blur', this.pause)
window.removeEventListener('focus', this.start)
Expand Down Expand Up @@ -250,15 +251,24 @@ World.prototype._frame = function() {
}

World.prototype._tick = function() {
// remove stopped `forever` loops
for (var i=this._deadLoops.length; i--; ) {
this.unlisten('frame', this._deadLoops[i])
}
this._deadLoops = []

// trigger `forever` loops
this.emitFrame()
this.tick()
const sprites = this.sprites
for (var i=0; i<sprites.length; i++) {
sprites[i].tick()
}
}
World.prototype.tick = function(cb) {
// TODO opt?
var dead = []
for (let fn of this.listeners('tick')) {
if (fn() === false) {
dead.push(fn)
}
}
for (let fn of dead) this.unlisten('tick', fn)
}
World.prototype.forever = World.prototype.onTick

World.prototype._draw = function() {
if (this._needsResize) this._resize()
Expand Down Expand Up @@ -558,7 +568,7 @@ const Base = function(props, init) {
this.dead = false
world.sprites.push(this)
}
emitter(Base.prototype, ['tap', 'drag', 'drop'])
emitter(Base.prototype, ['tick', 'tap', 'drag', 'drop'])

// TODO: cache rotated sprites.

Expand Down Expand Up @@ -798,6 +808,8 @@ Base.prototype.getTouchingFast = function() {
return result
}

Base.prototype.tick = World.prototype.tick
Base.prototype.forever = Base.prototype.onTick


/* Sprite */
Expand Down Expand Up @@ -1119,18 +1131,6 @@ Sound.prototype.play = function() {



// forever

function forever(cb) {
if (typeof cb !== 'function') throw new Error('oops')
const w = world
w.onFrame(function listener() {
if (cb() === false) {
w._deadLoops.push(listener)
}
})
}


/* events */

Expand Down Expand Up @@ -1185,7 +1185,6 @@ module.exports = {
Polygon,
Rect,
Sound,
forever,
}
Object.assign(module.exports, maths)

0 comments on commit 70ddc5f

Please sign in to comment.