Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Commit

Permalink
optimized memory
Browse files Browse the repository at this point in the history
  • Loading branch information
qertis committed Mar 5, 2016
1 parent 3081dbf commit 58bc32e
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 87 deletions.
7 changes: 3 additions & 4 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"name": "cc_spriter",
"version": "1.1.1",
"version": "1.1.3",
"homepage": "https://github.com/qertis/cc_spriter",
"authors": [
"Denis Baskovsky <denis@baskovsky.ru>"
],
"description": "Spriter implementation for Cocos2D-JS",
"description": "Spriter implementation for Cocos2d-HTML5",
"main": "dist/cc_spriter_min.js",
"moduleType": [],
"install" : {
"path" : "lib"
},
"keywords": [
"spriter",
"cocos2d animation"
"cocos2d skelet animation"
],
"license": "MIT",
"ignore": [
Expand All @@ -24,7 +24,6 @@
"tests"
],
"dependencies": {
"cocos2d-html5": "*",
"spriterjs": "https://github.com/flyover/spriter.js.git",
"google-closure-library": "*"
}
Expand Down
189 changes: 109 additions & 80 deletions cc_spriter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Spriter plugin for Cocos2D-JS
* @version 1.1.1
* @version 1.1.3
* @author Denis Baskovsky (denis@baskovsky.ru)
*
* Based on Spriter.js by:
Expand All @@ -11,30 +11,32 @@

const sprites = [];
let pose = {};
let timeStep = 0.0;// delta time in milliseconds
let _sconLink = ''; // Resource scon link
let _sconPath = ''; // Resource scon path

cc.Spriter = cc.Sprite.extend({
_ready: false, // Loading indicator
_entity: null,
_animation: null,

timeStep : 0.0,// delta time in milliseconds

/**
* @constructor
* @param {String} sconLink scon file to use for this animation
*/
ctor (sconLink) {
ctor(sconLink) {
this._super();

timeStep = cc.director.getAnimationInterval() * 1000;
_sconLink = sconLink;
_sconPath = sconLink.replace(/\w+.scon$/, '');
this.timeStep = cc.director.getAnimationInterval() * 1000;
this._sconLink = sconLink;

_sconPath = this._getSconPath(sconLink);

this.preload(data => {
if (data.error) {
throw data.error;
}

this._ready = true;
this.removeAllChildren();
this.setEntity(this._entity);
Expand All @@ -43,11 +45,29 @@
});
},

/**
* Every tick timer
* @public
*/
update() {
pose.update(this.timeStep); // accumulate time
pose.strike(); // process time slice

if (sprites.length) {
const objectArraySprites = this._getObjectArraySprites();
this._hideAllSprites();
this._updateSpriteFrames(objectArraySprites);
} else {
this._initSpriteFrames();
}
},

/**
* Set entity
* @param {String} entity
* @public
*/
setEntity (entity) {
setEntity(entity) {
this._entity = entity;

if (this._ready) {
Expand All @@ -58,8 +78,9 @@
/**
* Set animation
* @param {String} animation
* @public
*/
setAnim (animation) {
setAnim(animation) {
this._animation = animation;

if (this._ready) {
Expand All @@ -70,43 +91,46 @@
/**
* Prealod scon resource
* @param {function} callback
* @return {function}
* @public
*/
preload (callback) {
preload(callback) {
if (this._ready) {
return callback({
error: 'is ready'
});
}

cc.loader.loadJson(_sconLink, (error, scon) => {
cc.loader.loadJson(this._sconLink, (error, scon) => {
if (error) {
return callback({error});
}

let loaderIndex = 0;

const data = new spriter.Data().load(scon); // create and load Spriter data from SCON file
pose = new spriter.Pose(data); // create Spriter pose and attach data
// create and load Spriter data from SCON file
const data = new spriter.Data().load(scon);
// create Spriter pose and attach data
pose = new spriter.Pose(data);

/* Getting file count */
// Getting file count
scon.folder.forEach(folder => folder.file.forEach(() => ++loaderIndex));

data.folder_array.forEach(folder => {
folder.file_array.forEach(file => {

switch (file.type) {
case 'image':
{
let image_key = file.name;
let fileUrl = _sconPath + file.name;
case 'image': {
const image_key = file.name;
const fileUrl = _sconPath + file.name;

cc.loader.loadImg(fileUrl, (error, img) => {
if (error) {
return callback({error});
}

let rect = cc.rect(0, 0, file.width, file.height);
let sprite = new cc.Sprite(img, rect);
const rect = cc.rect(0, 0, file.width, file.height);
const sprite = new cc.Sprite(img, rect);

if (!cc.spriteFrameCache.getSpriteFrame(image_key)) {
cc.spriteFrameCache.addSpriteFrame(sprite.getSpriteFrame(), image_key);
Expand All @@ -120,8 +144,7 @@
break;
}

default:
{
default: {
// TODO: Add
// pose.bone_array
// pose.event_array
Expand All @@ -137,7 +160,17 @@
},

/**
*
* Get clear scon path
* @param link {String}
* @returns {string}
* @private
*/
_getSconPath(link) {
return link.replace(/\w+.scon$/, '');
},

/**
* Dynamic getting Array Sprite objects
* @returns {Array}
* @private
*/
Expand Down Expand Up @@ -173,33 +206,31 @@
},

/**
*
* Initialize Sprite Frames
* @private
*/
_initSpriteFrames() {

this._getObjectArraySprites().forEach((e, i) => {
let worldSpace = e.object.world_space;

let sprite = new cc.Sprite(e.spriteFrame);
e.myIndex = i;
const worldSpace = e.object.world_space;
const sprite = new cc.Sprite(e.spriteFrame);

this._updateSprite(sprite, worldSpace, e, i);
this._updateSprite(sprite, worldSpace, e);
sprites.push(sprite);
this.addChild(sprite);

});

},

/**
* Обновить спрайт
* @param sprite
* @param worldSpace
* @param e
* @param i
* Update sprite
* @param sprite {cc.Sprite}
* @param worldSpace {Object}
* @param e {Object}
* @private
*/
_updateSprite(sprite, worldSpace, e, i) {
_updateSprite(sprite, worldSpace, e) {
sprite.setName(e.imageKey);
sprite.opacity = e.object.alpha * 255;
sprite.x = worldSpace.position.x;
Expand All @@ -210,65 +241,63 @@

sprite.myFile = e.file;
sprite.myFolder = e.folder;
sprite.myIndex = i;
sprite.myIndex = e.myIndex;
},

// TODO: too many forEach! Их сейчас 4! (forEach, forEach, forEach и find)
// Возможно в цикл с opacity = 0 нужно внедрить логику this._getObjectArraySprites()
// У xxx иногда может быть больше length и поэтому логику нужно ставить туда
_updateSpriteFrames() {

const objectArraySprites = this._getObjectArraySprites();
/**
* Find Sprite by object
* @param e {Object}
* @returns {cc.Sprite | null}
* @private
*/
_findSpriteByObject(e) {
let sprite = null;

sprites.forEach((sprite) => {
sprite.opacity = 0;
});
for (let i = 0; i < sprites.length; i++) {
sprite = sprites[i];

objectArraySprites.forEach((e, index) => {
let worldSpace = e.object.world_space;
if (Object.is(e.file, sprite.myFile) &&
Object.is(e.folder, sprite.myFolder) &&
Object.is(e.myIndex, sprite.myIndex)) {
return sprite;
}
}
},

// Find like object
let sprite = sprites.find(a => {
return (
Object.is(e.file, a.myFile) &&
Object.is(e.folder, a.myFolder) &&
Object.is(a.myIndex, index)
);
});
/**
* Hide opacity for all sprites
* @private
*/
_hideAllSprites() {
for (let i = 0, len = sprites.length; i < len; i++) {
sprites[i].opacity = 0;
}
},

/**
* Если спрайт найден - просто обновляем
* Иначе создаем новый спрайт и добавляем его
*/
if (sprite) {
this._updateSprite(sprite, worldSpace, e, index);
} else {
/**
* Update positions, opacity, rotate and other
* @param objectArraySprites {Array}
* @private
*/
_updateSpriteFrames(objectArraySprites) {
for (let index = 0, len = objectArraySprites.length; index < len; index++) {
const e = objectArraySprites[index];
e.myIndex = index;
const worldSpace = e.object.world_space;
let sprite = this._findSpriteByObject(e);

// If sprite not found - creating a new sprite
if (!sprite) {
sprite = new cc.Sprite(e.spriteFrame);
this._updateSprite(sprite, worldSpace, e, index);

sprites.push(sprite);
this.addChild(sprite);
}

this._updateSprite(sprite, worldSpace, e);
sprite.zIndex = index;
});

},

/**
* Update every tick
*/
update () {
pose.update(timeStep); // accumulate time
pose.strike(); // process time slice

if (sprites.length) {
this._updateSpriteFrames();
} else {
this._initSpriteFrames();
}

}

});

}(window, window.cc, spriter);
2 changes: 1 addition & 1 deletion dist/cc_spriter_min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cc_spriter",
"version": "1.1.1",
"version": "1.1.3",
"description": "spriter animation for cocos2d-js",
"main": "cc_spriter.js",
"dependencies": {
Expand Down
1 change: 0 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module.exports = {
entry: [
'./cc_spriter.js'
],
//devtool : 'source-map',
output: {
path: path.join(__dirname, 'bower_components/temp/'),
filename: 'cc_spriter_component.js'
Expand Down

0 comments on commit 58bc32e

Please sign in to comment.