Skip to content

Instantly share code, notes, and snippets.

@vkgtaro
Last active August 29, 2015 14:15
Show Gist options
  • Save vkgtaro/a747eea4ebdeb85d3d25 to your computer and use it in GitHub Desktop.
Save vkgtaro/a747eea4ebdeb85d3d25 to your computer and use it in GitHub Desktop.
cocos2d-js でアニメーションを読み込む関数書いた ref: http://qiita.com/vkgtaro/items/e9fa63f0b6a54c7dba50
var animationLoad = function (animation_name, width, height, turn_point, count, delay_time, image_path) {
var x_count = 0;
var y_count = 0;
var frames_array = [];
for (var i = 0; i < count; i++ ) {
var current_x = width * x_count;
var current_y = height * y_count;
var frame = cc.SpriteFrame.create(
image_path,
cc.rect( current_x, current_y, width, height )
);
frames_array.push(frame);
if ( i % turn_point ) {
x_count = 0;
y_count++;
}
else {
x_count++;
}
}
var animation = cc.Animation.create(frames_array, delay_time);
cc.AnimationCache.getInstance().addAnimation(animation, animation_name);
}
var loadAnimation = function (animation_name, width, height, turn_point, count, delay_time, image_path) {
var x_count = 0;
var y_count = 0;
var frames_array = [];
for (var i = 1; i < count; i++ ) {
var current_x = width * x_count;
var current_y = height * y_count;
var frame = cc.SpriteFrame.create(
image_path,
cc.rect( current_x, current_y, width, height )
);
frames_array.push(frame);
if ( turn_point == 1 || i % turn_point == 0 ) {
x_count = 0;
y_count++;
}
else {
x_count++;
}
}
var animation = cc.Animation.create(frames_array, delay_time);
cc.AnimationCache.getInstance().addAnimation(animation, animation_name);
};
animationLoad("a", 320, 240, 3, 15, 1/20, "parapara_anime.png");
var anim_cache = cc.AnimationCache.getInstance();
var a = anim_cache.getAnimation("a");
a.setRestoreOriginalFrame(true);
var anim = cc.Animate.create(a);
var anim_sprite = cc.Sprite.create();
anim_sprite.setTextureRect(cc.rect(0, 0, 320, 240));
anim_sprite.setPosition(200, 200);
this.addChild(anim_sprite);
anim_sprite.runAction(cc.RepeatForever.create(anim));
loadAnimation("a", 320, 240, 3, 15, 1/20, "parapara_anime.png");
var anim_cache = cc.AnimationCache.getInstance();
var a = anim_cache.getAnimation("a");
a.setRestoreOriginalFrame(true);
var anim = cc.Animate.create(a);
var anim_sprite = cc.Sprite.create();
anim_sprite.setTextureRect(cc.rect(0, 0, 320, 240));
anim_sprite.setPosition(200, 200);
this.addChild(anim_sprite);
anim_sprite.runAction(cc.RepeatForever.create(anim));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment