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

Commit

Permalink
speed increased
Browse files Browse the repository at this point in the history
  • Loading branch information
qertis committed Feb 23, 2016
1 parent 07a893e commit 2474a45
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,6 +3,7 @@
*.iml
out
gen
*.log

/temp
/node_modules
Expand Down
84 changes: 44 additions & 40 deletions cc_spriter.js
Expand Up @@ -9,33 +9,28 @@
!function (window, cc, spriter) {
'use strict';


const xxx = [];
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

sconLink: '', // Resource scon link
sconPath: '', // Resource scon path

_entity: null,
_animation: null,

data: null,
pose: null,

timeStep: 0.0,// delta time in milliseconds

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

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

this.sconLink = sconLink;
this.preload(data => {
if (data.error) {
throw data.error;
Expand All @@ -56,7 +51,7 @@
this._entity = entity;

if (this._ready) {
this.pose.setEntity(entity);
pose.setEntity(entity);
}
},

Expand All @@ -68,7 +63,7 @@
this._animation = animation;

if (this._ready) {
this.pose.setAnim(animation);
pose.setAnim(animation);
}
},

Expand All @@ -77,24 +72,21 @@
* @param {function} callback
*/
preload (callback) {
let sconLink = this.sconLink;

if (this._ready) {
return callback({
error: 'is ready'
});
}

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

let sconPath = scon.sconPath = sconLink.replace(/\w+.scon$/, '');
let loaderIndex = 0;

let data = this.data = new spriter.Data().load(scon); // create and load Spriter data from SCON file
this.pose = new spriter.Pose(data); // create Spriter pose and attach data
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

/* Getting file count */
scon.folder.forEach(folder => folder.file.forEach(() => ++loaderIndex));
Expand All @@ -106,7 +98,7 @@
case 'image':
{
let image_key = file.name;
let fileUrl = sconPath + file.name;
let fileUrl = _sconPath + file.name;

cc.loader.loadImg(fileUrl, (error, img) => {
if (error) {
Expand Down Expand Up @@ -144,12 +136,16 @@

},


/**
*
* @returns {Array}
* @private
*/
_getObjectArraySprites() {

return this.pose.object_array.map(object => {
return pose.object_array.map(object => {
if (object.type === 'sprite') {
const folder = this.pose.data.folder_array[object.folder_index];
const folder = pose.data.folder_array[object.folder_index];
if (!folder) {
return;
}
Expand All @@ -176,23 +172,33 @@

},

/**
*
* @private
*/
_initSpriteFrames() {

const pose = this.pose;

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

let sprite = new cc.Sprite(e.spriteFrame);

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

});

},

/**
* Обновить спрайт
* @param sprite
* @param worldSpace
* @param e
* @param i
* @private
*/
_updateSprite(sprite, worldSpace, e, i) {
sprite.setName(e.imageKey);
sprite.opacity = e.object.alpha * 255;
Expand All @@ -202,7 +208,6 @@
sprite.scaleY = worldSpace.scale.y;
sprite.rotation = -worldSpace.rotation.deg;


sprite.myFile = e.file;
sprite.myFolder = e.folder;
sprite.myIndex = i;
Expand All @@ -213,15 +218,17 @@
// У xxx иногда может быть больше length и поэтому логику нужно ставить туда
_updateSpriteFrames() {

xxx.forEach(x => {
x.opacity = 0;
const objectArraySprites = this._getObjectArraySprites();

sprites.forEach((sprite) => {
sprite.opacity = 0;
});

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

// Find like object
let sprite = xxx.find(a => {
let sprite = sprites.find(a => {
return (
Object.is(e.file, a.myFile) &&
Object.is(e.folder, a.myFolder) &&
Expand All @@ -236,29 +243,26 @@
if (sprite) {
this._updateSprite(sprite, worldSpace, e, index);
} else {
console.log('not found')
sprite = new cc.Sprite(e.spriteFrame);
this._updateSprite(sprite, worldSpace, e, index);

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

sprite.zIndex = index;

});

},

/**
* Update every tick
*/
update () {
let pose = this.pose;

pose.update(this.timeStep); // accumulate time
pose.update(timeStep); // accumulate time
pose.strike(); // process time slice

if (xxx.length) {
if (sprites.length) {
this._updateSpriteFrames();
} else {
this._initSpriteFrames();
Expand Down
6 changes: 3 additions & 3 deletions demo/main.js
Expand Up @@ -3,11 +3,11 @@ cc.game.onStart = function () {
cc.view.resizeWithBrowserSize(true);
//load resources
cc.LoaderScene.preload([], function () {
var testScene = new TestScene();
cc.director.runScene(testScene);
var animationScene = new AnimationScene();
cc.director.runScene(animationScene);
}, this);

var TestScene = cc.Scene.extend({
var AnimationScene = cc.Scene.extend({
ctor: function () {
this._super();

Expand Down
3 changes: 1 addition & 2 deletions demo/project.json
Expand Up @@ -11,7 +11,6 @@
"cocos2d"
],
"jsList": [
"../dist/cc_spriter_min.js",
"../cc_spriter.js"
"../dist/cc_spriter_min.js"
]
}
3 changes: 2 additions & 1 deletion dist/cc_spriter_min.js

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

3 changes: 2 additions & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "cc_spriter",
"version": "1.0.2",
"version": "1.1.1",
"description": "spriter animation for cocos2d-js",
"main": "cc_spriter.js",
"dependencies": {
Expand All @@ -12,6 +12,7 @@
},
"devDependencies": {},
"scripts": {
"start": "bash bin/compile.sh",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand Down

0 comments on commit 2474a45

Please sign in to comment.