Cocos2d-html5之MoveTo&MoveBy

前端之家收集整理的这篇文章主要介绍了Cocos2d-html5之MoveTo&MoveBy前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文测试所用Cocos2d-html5版本:2.2.1。

MoveTo:移动到某个位置,是绝对距离。

MoveBy:移动一段距离,是相对距离。

使用cc.MoveTo.create(duration,deltaPosition)和cc.MoveBy.create(duration,deltaPosition)来创建动作。

duration
运动周期,单位为s。
deltaPosition
坐标点,使用cc.p(x,y)或者new cc.Point(x,y)

请看下列代码

  1. var GameScene = cc.Scene.extend({
  2. enemy1: null,// 敌人1
  3. enemy2: null,// 敌人2
  4. enemy3:null,// 敌人3
  5. layer: null,// 布景
  6. winSize: null,// 游戏运行窗口尺寸
  7. onEnter: function () {
  8. this._super();
  9. this.initData();
  10. },
  11. initData: function () {
  12. // 获取尺寸
  13. this.winSize = cc.Director.getInstance().getWinSize();
  14. // 添加布景
  15. this.layer = cc.LayerColor.create(cc.c4(200,200,255),this.winSize.width,this.winSize.height);
  16. this.addChild(this.layer);
  17. // 创建动作
  18. var actionTo = cc.MoveTo.create(2,cc.p(this.winSize.width - 40,this.winSize.height - 40));
  19. var actionBy = cc.MoveBy.create(2,cc.p(40,40));
  20. var actionByBack = actionBy.reverse();
  21. // 添加敌人1
  22. this.enemy1 = cc.Sprite.create(s_enemy_1);
  23. this.layer.addChild(this.enemy1);
  24. this.enemy1.runAction(actionTo);
  25. // 添加敌人2
  26. this.enemy2 = cc.Sprite.create(s_enemy_2);
  27. this.enemy2.setPosition(cc.p(40,40));
  28. this.layer.addChild(this.enemy2);
  29. this.enemy2.runAction(cc.Sequence.create(actionBy,actionByBack));
  30. // 添加敌人3
  31. this.enemy3 = cc.Sprite.create(s_enemy_3);
  32. this.enemy3.setPosition(cc.p(80,80));
  33. this.layer.addChild(this.enemy3);
  34. this.enemy3.runAction(cc.MoveBy.create(2,cc.p(100,0)));
  35. }
  36. });

以下是运行结果截图:

猜你在找的Cocos2d-x相关文章