Last active
May 26, 2017 11:10
-
-
Save lukz/0867e9f1875f9771873fc540862e6179 to your computer and use it in GitHub Desktop.
Finding regions in @orange-games phaser-spine atlas cache and creating sprite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function create() { | |
var atlasKey = 'atlasKey'; | |
var regionKey = 'someRegionInsideAtlas'; | |
var data = this.game.cache.getSpine(atlasKey); | |
var textureLoader = new PhaserSpine.SpineTextureLoader(game); | |
var spineAtlas = new spine.Atlas(game.cache.getText(data.atlas), textureLoader); | |
var sprite = createSprite(game, getRegionByName(spineAtlas, regionKey)); | |
} | |
function getRegionByName(spineAtlas, name) { | |
for (var i = 0; i < spineAtlas.regions.length; i++) { | |
if(spineAtlas.regions[i].name === name) return spineAtlas.regions[i]; | |
} | |
return null; | |
} | |
function createSprite(game, atlasRegion) { | |
var baseTexture = atlasRegion.page.rendererObject; | |
var spriteRect = new PIXI.Rectangle(atlasRegion.x, | |
atlasRegion.y, | |
atlasRegion.rotate ? atlasRegion.height : atlasRegion.width, | |
atlasRegion.rotate ? atlasRegion.width : atlasRegion.height); | |
var spriteTexture = new PIXI.Texture(baseTexture, spriteRect); | |
var sprite = new Phaser.Sprite(game, 0, 0, spriteTexture); | |
var baseRotation = atlasRegion.rotate ? Math.PI * 0.5 : 0.0; | |
sprite.scale.x = atlasRegion.width / atlasRegion.originalWidth; | |
sprite.scale.y = atlasRegion.height / atlasRegion.originalHeight; | |
sprite.rotation = baseRotation; | |
sprite.anchor.x = (0.5 * atlasRegion.originalWidth - atlasRegion.offsetX) / atlasRegion.width; | |
sprite.anchor.y = 1.0 - ((0.5 * atlasRegion.originalHeight - atlasRegion.offsetY) / atlasRegion.height); | |
if (atlasRegion.rotate) { | |
var x1 = sprite.scale.x; | |
sprite.scale.x = sprite.scale.y; | |
sprite.scale.y = x1; | |
} | |
return sprite; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment