代码

来源:互联网 发布:淘宝清洗订单后果 编辑:程序博客网 时间:2024/05/21 08:55


var g_GameLayer;var GamerLayer = cc.Layer.extend({    ctor:function(){        this._super();        this.init();    },   init:function(){      g_GameLayer=this;      var bg = cc.Sprite.create("res/bg/background.png")      bg.setAnchorPoint(cc.p(0,0))      this.addChild(bg)      this.hero = new Unit();      this.addChild(this.hero,1);      this.hero.setPosition(300,300)   },}); GamerLayer.create = function(){    var layer = new GamerLayer();    var uiLayer = new ControlLayer();    layer.addChild(uiLayer)    return layer;}GamerLayer.scene = function(){    var s = cc.Scene.create();    s.addChild(GamerLayer.create());    return s;}var ControlLayer = cc.Layer.extend({   ctor:function(){      this._super();      this.setTouchMode(cc.TOUCH_ONE_BY_ONE);       this.setTouchEnabled(true);       this.setTouchPriority(-199);      this.init();    },    init:function(){      this.attackBtn = cc.Sprite.create("res/ui/skill1.png")      this.addChild(this.attackBtn);      this.attackBtn.setPosition(850,90)      this.controlBtn = cc.Sprite.create("res/ui/circle.png")      this.addChild(this.controlBtn,1);      this.controlBtn.setPosition(100,100)      this.controlBtnbg = cc.Sprite.create("res/ui/circleBg.png")      this.addChild(this.controlBtnbg);      this.controlBtnbg.setPosition(100,100)   },  onTouchBegan : function(pos,event){     if(pos_.x>400){         g_GameLayer.hero.run()     }else{        g_GameLayer.hero.attack();     }     return true;  },  onTouchMoved : function(pos,event){  },  onTouchEnded : function(pos,event){  },})var g_Hero;var Unit = cc.Node.extend({    ctor:function(){        this._super();        this.init();  }, init:function(){     g_Hero=this;     this.initSprite();     this.initAction(); }, initSprite:function(){     this.sprite = cc.Sprite.create("res/roles/player1-1-1.png")     this.addChild(this.sprite) }, initAction:function(){     // 跑动动作     var animation = cc.Animation.create();     for (var i = 1; i <= 4; i++){         var frameName = "res/roles/player1-1-" + i + ".png";         animation.addSpriteFrameWithFile(frameName);     }     animation.setDelayPerUnit(2.8 / 14);     animation.setRestoreOriginalFrame(true);    cc.AnimationCache.getInstance().addAnimation(animation,"run")     // 普通攻击     var anAttack = cc.Animation.create();     for (var attackIndex = 1; attackIndex <= 4; attackIndex ++){         var attackFrame = "res/roles/player1-5-" + attackIndex + ".png";         anAttack.addSpriteFrameWithFile(attackFrame);     }     anAttack.setDelayPerUnit(1.8 / 14);    cc.AnimationCache.getInstance().addAnimation(anAttack,"attack")      var attackAnimation = cc.AnimationCache.getInstance().getAnimation("attack")    this._actionAttack=cc.Animate.create(attackAnimation)    }, stand:function(){    this.sprite.setTexture(cc.TextureCache.getInstance().addImage("res/roles/player1-1-4.png")) }, run:function(){    var runAnimation = cc.AnimationCache.getInstance().getAnimation("run")    _actionRun=cc.Animate.create(runAnimation)    this.sprite.runAction(_actionRun)------------用缓存中的动画重新create 一个animate就不会报错 }, attack:function(){    this.runAction(this._actionAttack)  ---------这样写在调用的时候就会报错了                                                            ----------打log看了this._actionAttack是个animate对象,为何不能用? }, dead:function(){ }, update:function(dt){ },}); 



0 0
原创粉丝点击