Javascript 精灵对象内的Cocos2d动画
在Cocos2d-xv3.81项目中,我试图在扩展精灵的对象内设置精灵动画,但它似乎什么都不做。甚至有可能在这样一个节点中有一个动画精灵,或者它必须扩展层才能有动画Javascript 精灵对象内的Cocos2d动画,javascript,animation,cocos2d-js,Javascript,Animation,Cocos2d Js,在Cocos2d-xv3.81项目中,我试图在扩展精灵的对象内设置精灵动画,但它似乎什么都不做。甚至有可能在这样一个节点中有一个动画精灵,或者它必须扩展层才能有动画 var Player = cc.Sprite.extend ({ ctor: function () { this._super(res.Player_png); this.UP = false; this.DOWN = false; this.LEFT = false; this.RIG
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;
}
}
});
var Player=cc.Sprite.extend({
ctor:函数(){
这个。_super(res.Player_png);
this.UP=false;
this.DOWN=false;
this.LEFT=false;
this.RIGHT=false;
这个动作=错误;
这个速度=5;
cc.spriteFrameCache.addSpriteFrame(res.Player\u Left\u plist);
var leftFrames=[];
对于(变量i=0;i<4;i++)
{
var str=“Left”+i+“.png”;
var frame=cc.spriteFrameCache.getSpriteFrame(str);
左框。推(框);
}
this.leftAnim=新的cc.Animation(leftFrames,0.3);
this.runLeft=new cc.repeatForever(new cc.Animate(this.leftAnim));
this.state=“无”;
this.nothing=“nothing”;
this.scheduleUpdate();
cc.eventManager.addListener(
cc.EventListener.create({
事件:cc.EventListener.KEYBOARD,
onKeyPressed:功能(键、事件)
{
如果(key==87)this.UP=true;
如果(key==65)this.LEFT=true,则为else;
如果(key==83)this.DOWN=true,则为else;
如果(key==68)this.RIGHT=true,则为else;
如果(key==69 | | key==32)this.ACTION=true;
}.绑定(此),
onKeyReleased:功能(键、事件)
{
如果(key==87)this.UP=false;
如果(key==65)this.LEFT=false,则为else;
如果(key==83)this.DOWN=false,则为else;
如果(key==68)this.RIGHT=false,则为else;
如果(key==69 | | key==32)this.ACTION=false;
}.绑定(此)
}),这个);
返回true;
},
更新:功能(dt){
如果(这个向上)
{
this.y+=this.speed;
this.runAction(this.runLeft);
}
否则,如果(此。向下)
{
this.y-=this.speed;
}
如果(左)
{
这个.x-=这个速度;
this.runAction(this.runLeft);
}
否则,如果(这个。对)
{
这个.x+=这个.speed;
}
}
});
此代码在sprite类之外适用吗?例如在普通雪碧中
我认为这是错误的,但我没有测试它:
this.leftAnim=新的cc.Animation(leftFrames,0.3);
this.runLeft=new cc.repeatForever(new 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());
var leftAnim=new cc.Animation();
对于(变量i=0;i<4;i++)
{
var str=“Left”+i+“.png”;
var frame=cc.spriteFrameCache.getSpriteFrame(str);
leftAnim.addSpriteFrame(框架);
}
leftAnim.setDelayPerUnit(0.08);
this.runAction(cc.animate(leftAnim))//或者这个.runAction(cc.animate(leftAnim.repeatForever());
希望能有所帮助我认为问题在于这一行:
cc.spriteFrameCache.addSpriteFrame(res.Player_Left_plist);
应该是
cc.spriteFrameCache.addSpriteFrames(res.Player_Left_plist);
其中SpriteFrames是复数。在手动创建一个cc.SpriteFrame对象后,末尾没有“s”的方法用于将单个cc.SpriteFrame对象加载到缓存中。末尾带有“s”的方法是一个plist获取URL以一次加载整个spritesheet的方法
多元化在很大程度上是个问题 对于那些好奇的人,下面是一段函数代码:
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);
你说得对。非常感谢你。对于感兴趣的人,这是功能代码块:
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);
cc.spriteFrameCache.addSpriteFrames(res.playerRun_plist);
变量i,f;
var框架=[];
对于(i=1;i),您应该将user1615873的答案标记为正确