Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Use subclass of sprite in Tilemap.createFromObjects


seiyria
 Share

Recommended Posts

I know this isn't possible, but it would be nice if we could specify a class to use when doing Tilemap.createFromObjects. Currently it always creates a Phaser.Sprite, which in most cases would be fine, but I would like to be able to specify a class that I want it to instantiate, assuming that class inherits from Phaser.Sprite. The advantages of this would include the ability to organize code better, and not have so much duplicate code. 

 

Currently I'm making a way to inject this functionality into a Tilemap instance, and I'll post back when I have something.

Link to comment
Share on other sites

Surprisingly this was easier than I expected:

    injectMapFunctionality: (map) ->        map.createFromObjectsCtor = (name, gid, key, frame, ctor = Phaser.Sprite, exists = true, autoCull = true, group = @game.world) ->            if not @objects[name]                console.warn "Tilemap.createFromObjectsCtor: invalid objectgroup name given: #{name}"                return            if not ctor                console.warn "Tilemap.createFromObjectsCtor: no ctor given"                return            i = 0            len = @objects[name].length            while i < len                if @objects[name][i].gid is gid                    sprite = new ctor @game, @objects[name][i].x, @objects[name][i].y, key, frame                    sprite.exists = exists                    group.add sprite                    sprite.anchor.setTo 0, 1                    sprite.name = @objects[name][i].name                    sprite.visible = @objects[name][i].visible                    sprite.autoCull = autoCull                    for property of @objects[name][i].properties                        group.set sprite, property, @objects[name][i].properties[property], false, false, 0                i++

Here is the solution in CoffeeScript. You would have to call @injectMapFunctionality on the given map and you will be able to create objects with any constructor. Horray!

Link to comment
Share on other sites

Hmm. Now this poses an interesting dilemma that I didn't quite think through. Normally, before this change, I would do tileMap.createFromObjects [..], @group, and then do @group.forEach() to make them do whatever it was they do. Now when I set custom properties on my objects, they don't have them at the time they're created (since the property loop happens after the constructor runs). I think I'll have to extend this to call something like object.isReady() after the function is done, so I can guarantee those properties are available.

 

Here's my modified function:

		map.createFromObjects = (name, gid, key, frame, exists = true, autoCull = true, group = @game.world, ctor = Phaser.Sprite) ->			if not @objects[name]				console.warn "Tilemap.createFromObjectsCtor: invalid objectgroup name given: #{name}"				return			i = 0			len = @objects[name].length			while i < len				if @objects[name][i].gid is gid					sprite = new ctor @game, @objects[name][i].x, @objects[name][i].y, key, frame					sprite.exists = exists					sprite.anchor.setTo 0, 1					sprite.name = @objects[name][i].name					sprite.visible = @objects[name][i].visible					sprite.autoCull = autoCull					group.add sprite					for property of @objects[name][i].properties						group.set sprite, property, @objects[name][i].properties[property], false, false, 0					sprite.isReady() if 'isReady' of sprite and typeof sprite.isReady is 'function'				i++

All I added was the sprite.isReady() line and rearranged the arguments to match those in the official repo.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...