From 0b948d4a1e14ad8c1c007145bd5962b8f82239e2 Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Thu, 18 Sep 2014 10:03:13 -0700 Subject: [PATCH] Got the SpriteBoss to handle removing sprites again. --- app/lib/surface/SpriteBoss.coffee | 2 +- app/lib/surface/WebGLLayer.coffee | 8 ++++++++ test/app/lib/surface/SpriteBoss.spec.coffee | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/lib/surface/SpriteBoss.coffee b/app/lib/surface/SpriteBoss.coffee index 6c83d6559..034c45987 100644 --- a/app/lib/surface/SpriteBoss.coffee +++ b/app/lib/surface/SpriteBoss.coffee @@ -170,7 +170,7 @@ module.exports = class SpriteBoss extends CocoClass sprite removeSprite: (sprite) -> - sprite.imageObject.parent.removeChild sprite.imageObject + sprite.layer.removeCocoSprite(sprite) thang = sprite.thang delete @sprites[sprite.thang.id] @spriteArray.splice @spriteArray.indexOf(sprite), 1 diff --git a/app/lib/surface/WebGLLayer.coffee b/app/lib/surface/WebGLLayer.coffee index 350ceebf8..871147fc4 100644 --- a/app/lib/surface/WebGLLayer.coffee +++ b/app/lib/surface/WebGLLayer.coffee @@ -42,12 +42,20 @@ module.exports = class WebGLLayer extends CocoClass addCocoSprite: (cocoSprite) -> cocoSprite.options.resolutionFactor = @resolutionFactor + if cocoSprite.layer + console.warn 'CocoSprite being re-added to a layer?' + + cocoSprite.layer = @ cocoSprite.updateBaseScale() @cocoSprites.push cocoSprite @loadThangType(cocoSprite.thangType) @addDefaultActionsToRender(cocoSprite) @setImageObjectToCocoSprite(cocoSprite) # TODO: actually add it as a child + + removeCocoSprite: (cocoSprite) -> + cocoSprite.imageObject.parent.removeChild cocoSprite.imageObject + @cocoSprites = _.without @cocoSprites, cocoSprite loadThangType: (thangType) -> if not thangType.isFullyLoaded() diff --git a/test/app/lib/surface/SpriteBoss.spec.coffee b/test/app/lib/surface/SpriteBoss.spec.coffee index 65fc5553a..dede862b2 100644 --- a/test/app/lib/surface/SpriteBoss.spec.coffee +++ b/test/app/lib/surface/SpriteBoss.spec.coffee @@ -24,6 +24,7 @@ describe 'SpriteBoss', -> world = new World() world.thangs = [ {id: 'Tree 1', spriteName: 'Tree 1', exists: true, pos: {x:4, y:0}, action: 'idle', health: 20, maxHealth: 20, rotation: Math.PI/2, acts: true } + {id: 'Tree Will Disappear', spriteName: 'Tree 1', exists: true, pos: {x:2, y:0}, action: 'idle', health: 20, maxHealth: 20, rotation: Math.PI/2, acts: true } ] world.thangMap = {} world.thangMap[thang.id] = thang for thang in world.thangs @@ -64,6 +65,7 @@ describe 'SpriteBoss', -> {id: 'Fangrider 1', spriteName: 'Ogre Fangrider', exists: true, pos: {x:8, y:8}, action: 'move', health: 20, maxHealth: 20, rotation: 0, acts: true } ] + _.find(world.thangs, {id: 'Tree Will Disappear'}).exists = false world.thangMap[thang.id] = thang for thang in world.thangs spriteBoss.update(true) defaultLayer.once 'new-spritesheet', ->