Cocos2d Sprite 对象内的动画
Cocos2d Animation inside a Sprite Object
在一个 Cocos2d-x V 3.81 项目中,我试图在一个扩展 Sprite 的对象中为一个 sprite 设置动画,但它似乎没有做任何事情。甚至可以在这样的节点内有动画精灵,还是必须扩展 Layer 才能有动画?
var Player = cc.Sprite.extend ({
ctor: function () {
this._super(res.Player_png);
this.UP = false;
this.DOWN = false;
this.LEFT = false;
this.RIGHT = false;
this.ACTION = false;
this.speed = 5;
cc.spriteFrameCache.addSpriteFrame(res.Player_Left_plist);
var leftFrames = [];
for(var i = 0; i < 4; i++)
{
var str = "Left" + i + ".png";
var frame = cc.spriteFrameCache.getSpriteFrame(str);
leftFrames.push(frame);
}
this.leftAnim = new cc.Animation(leftFrames, 0.3);
this.runLeft = new cc.repeatForever(new cc.Animate(this.leftAnim));
this.state = "nothing";
this.nothing = "nothing";
this.scheduleUpdate();
cc.eventManager.addListener (
cc.EventListener.create ({
event: cc.EventListener.KEYBOARD ,
onKeyPressed: function(key, event)
{
if(key == 87) this.UP = true;
else if(key == 65) this.LEFT = true;
else if(key == 83) this.DOWN = true;
else if(key == 68) this.RIGHT = true;
else if (key == 69 || key == 32) this.ACTION = true;
}.bind(this),
onKeyReleased: function(key, event)
{
if(key == 87) this.UP = false;
else if(key == 65) this.LEFT = false;
else if(key == 83) this.DOWN = false;
else if(key == 68) this.RIGHT = false;
else if (key == 69 || key == 32) this.ACTION = false;
}.bind(this)
}),this);
return true;
},
update:function(dt) {
if(this.UP)
{
this.y += this.speed;
this.runAction(this.runLeft);
}
else if(this.DOWN)
{
this.y -= this.speed;
}
if(this.LEFT)
{
this.x -= this.speed;
this.runAction(this.runLeft);
}
else if(this.RIGHT)
{
this.x += this.speed;
}
}
});
此代码在 sprite 之外对您有效 class?例如在常规精灵中?
我认为这是错误的,但我没有测试过:
this.leftAnim = new cc.Animation(leftFrames, 0.3);
this.runLeft = 新 cc.repeatForever(新 cc.Animate(this.leftAnim));
使用你的代码我会做什么:
var leftAnim = new cc.Animation();
for(var i = 0; i < 4; i++)
{
var str = "Left" + i + ".png";
var frame = cc.spriteFrameCache.getSpriteFrame(str);
leftAnim.addSpriteFrame(frame);
}
leftAnim.setDelayPerUnit(0.08);
this.runAction(cc.animate(leftAnim)); //or this.runAction(cc.animate(leftAnim).repeatForever());
希望对您有所帮助
我认为问题出在这一行:
cc.spriteFrameCache.addSpriteFrame(res.Player_Left_plist);
应该是
cc.spriteFrameCache.addSpriteFrames(res.Player_Left_plist);
其中 SpriteFrames 是复数。在手动创建 SpriteFrame 之后,末尾没有 's' 的方法用于将单个 cc.SpriteFrame 对象加载到缓存中。最后带有 's' 的方法是使用 URL 让 plist 一次加载整个 spritesheet 的方法。
http://www.cocos2d-x.org/reference/html5-js/V3.8/symbols/cc.spriteFrameCache.html
多元化是主要的问题。
对于那些好奇的人,这是功能代码块:
你是对的。非常感谢你。对于那些感兴趣的人,这是功能代码块:
cc.spriteFrameCache.addSpriteFrames(res.playerRun_plist);
var i,f;
var frames=[];
for (i=1; i <= 4; i++) {
f=cc.spriteFrameCache.getSpriteFrame("playerRun"+i+".png");
frames.push(f);
}
var playerRunAnim = new cc.Animation(frames, 0.1);
this.playerAction = new cc.RepeatForever(new cc.Animate(playerRunAnim));
this.runAction(this.playerAction);
在一个 Cocos2d-x V 3.81 项目中,我试图在一个扩展 Sprite 的对象中为一个 sprite 设置动画,但它似乎没有做任何事情。甚至可以在这样的节点内有动画精灵,还是必须扩展 Layer 才能有动画?
var Player = cc.Sprite.extend ({
ctor: function () {
this._super(res.Player_png);
this.UP = false;
this.DOWN = false;
this.LEFT = false;
this.RIGHT = false;
this.ACTION = false;
this.speed = 5;
cc.spriteFrameCache.addSpriteFrame(res.Player_Left_plist);
var leftFrames = [];
for(var i = 0; i < 4; i++)
{
var str = "Left" + i + ".png";
var frame = cc.spriteFrameCache.getSpriteFrame(str);
leftFrames.push(frame);
}
this.leftAnim = new cc.Animation(leftFrames, 0.3);
this.runLeft = new cc.repeatForever(new cc.Animate(this.leftAnim));
this.state = "nothing";
this.nothing = "nothing";
this.scheduleUpdate();
cc.eventManager.addListener (
cc.EventListener.create ({
event: cc.EventListener.KEYBOARD ,
onKeyPressed: function(key, event)
{
if(key == 87) this.UP = true;
else if(key == 65) this.LEFT = true;
else if(key == 83) this.DOWN = true;
else if(key == 68) this.RIGHT = true;
else if (key == 69 || key == 32) this.ACTION = true;
}.bind(this),
onKeyReleased: function(key, event)
{
if(key == 87) this.UP = false;
else if(key == 65) this.LEFT = false;
else if(key == 83) this.DOWN = false;
else if(key == 68) this.RIGHT = false;
else if (key == 69 || key == 32) this.ACTION = false;
}.bind(this)
}),this);
return true;
},
update:function(dt) {
if(this.UP)
{
this.y += this.speed;
this.runAction(this.runLeft);
}
else if(this.DOWN)
{
this.y -= this.speed;
}
if(this.LEFT)
{
this.x -= this.speed;
this.runAction(this.runLeft);
}
else if(this.RIGHT)
{
this.x += this.speed;
}
}
});
此代码在 sprite 之外对您有效 class?例如在常规精灵中?
我认为这是错误的,但我没有测试过:
this.leftAnim = new cc.Animation(leftFrames, 0.3); this.runLeft = 新 cc.repeatForever(新 cc.Animate(this.leftAnim));
使用你的代码我会做什么:
var leftAnim = new cc.Animation();
for(var i = 0; i < 4; i++)
{
var str = "Left" + i + ".png";
var frame = cc.spriteFrameCache.getSpriteFrame(str);
leftAnim.addSpriteFrame(frame);
}
leftAnim.setDelayPerUnit(0.08);
this.runAction(cc.animate(leftAnim)); //or this.runAction(cc.animate(leftAnim).repeatForever());
希望对您有所帮助
我认为问题出在这一行:
cc.spriteFrameCache.addSpriteFrame(res.Player_Left_plist);
应该是
cc.spriteFrameCache.addSpriteFrames(res.Player_Left_plist);
其中 SpriteFrames 是复数。在手动创建 SpriteFrame 之后,末尾没有 's' 的方法用于将单个 cc.SpriteFrame 对象加载到缓存中。最后带有 's' 的方法是使用 URL 让 plist 一次加载整个 spritesheet 的方法。
http://www.cocos2d-x.org/reference/html5-js/V3.8/symbols/cc.spriteFrameCache.html
多元化是主要的问题。
对于那些好奇的人,这是功能代码块:
你是对的。非常感谢你。对于那些感兴趣的人,这是功能代码块:
cc.spriteFrameCache.addSpriteFrames(res.playerRun_plist);
var i,f;
var frames=[];
for (i=1; i <= 4; i++) {
f=cc.spriteFrameCache.getSpriteFrame("playerRun"+i+".png");
frames.push(f);
}
var playerRunAnim = new cc.Animation(frames, 0.1);
this.playerAction = new cc.RepeatForever(new cc.Animate(playerRunAnim));
this.runAction(this.playerAction);