使用脚本

脚本组件

脚本组件是用来扩展 C++ 节点对象的一种方式,你可以将脚本组件绑定到节点对象上,然后脚本组件就能收到 onEnteronExitupdate 事件。

脚本组件支持两种脚本语言 JavaScript 和 Lua,使用的脚本组件应该和绑定脚本的语言类型对应,比如 ComponentJS 用来绑定 JavaScript 脚本,ComponentLua 用来绑定 Lua 脚本。有了脚本组件,你就可以在 Cocos2d-x 的项目中,很方便的使用脚本进行一些控制。需要注意的是,在一个项目中不能混用脚本组件,也就是说一个项目要么只使用 JavaScript 脚本,要么只使用 Lua 脚本。

使用 Lua 脚本:

C++

  1. // create a Sprite and add a LUA component
  2. auto player = Sprite::create("player.png");
  3. auto luaComponent = ComponentLua::create("player.lua");
  4. player->addComponent(luaComponent);

Lua

  1. -- player.lua
  2. local player = {
  3. onEnter = function(self)
  4. -- do some things in onEnter
  5. end,
  6. onExit = function(self)
  7. -- do some things in onExit
  8. end,
  9. update = function(self)
  10. -- do some things every frame
  11. end
  12. }
  13. -- it is needed to return player to let c++ nodes know it
  14. return player

使用 JavaScript 脚本:

C++

  1. // create a Sprite and add a LUA component
  2. auto player = Sprite::create("player.png");
  3. auto jsComponent = ComponentJS::create("player.js");
  4. player->addComponent(jsComponent);

JavaScript

  1. // player.js
  2. Player = cc.ComponentJS.extend({
  3. generateProjectile: function (x, y) {
  4. var projectile = new cc.Sprite("components/Projectile.png", cc.rect(0, 0, 20, 20));
  5. var scriptComponent = new cc.ComponentJS("src/ComponentTest/projectile.js");
  6. projectile.addComponent(scriptComponent);
  7. this.getOwner().getParent().addChild(projectile);
  8. // set position
  9. var winSize = cc.director.getVisibleSize();
  10. var visibleOrigin = cc.director.getVisibleOrigin();
  11. projectile.setPosition(cc.p(visibleOrigin.x + 20, visibleOrigin.y + winSize.height/2));
  12. // run action
  13. var posX = projectile.getPositionX();
  14. var posY = projectile.getPositionY();
  15. var offX = x - posX;
  16. var offY = y - posY;
  17. if (offX <= 0) {
  18. return;
  19. }
  20. var contentSize = projectile.getContentSize();
  21. var realX = visibleOrigin.x + winSize.width + contentSize.width/2;
  22. var ratio = offY / offX;
  23. var realY = (realX * ratio) + posY;
  24. var realDest = cc.p(realX, realY);
  25. var offRealX = realX - posX;
  26. var offRealY = realY - posY;
  27. var length = Math.sqrt((offRealX * offRealX) + (offRealY * offRealY));
  28. var velocity = 960;
  29. var realMoveDuration = length / velocity;
  30. projectile.runAction(cc.moveTo(realMoveDuration, realDest));
  31. },
  32. onEnter: function() {
  33. var owner = this.getOwner();
  34. owner.playerComponent = this;
  35. cc.eventManager.addListener({
  36. event: cc.EventListener.TOUCH_ALL_AT_ONCE,
  37. onTouchesEnded: function (touches, event) {
  38. var target = event.getCurrentTarget();
  39. if (target.playerComponent) {
  40. var location = touches[0].getLocation();
  41. target.playerComponent.generateProjectile(location.x, location.y);
  42. jsb.AudioEngine.play2d("pew-pew-lei.wav");
  43. }
  44. }
  45. }, owner);
  46. }
  47. });

注意,两种组件的使用上,有一个重要的区别。使用 Lua 组件,Lua 脚本最后需要返回 Lua 对象,使用 JavaScript 组件,JavaScript 脚本需要扩展 cc.ComponentJS

更详细用法,请参考 Cocos2d-x 引擎的测试项目:tests/lua-tests/src/ComponentTest and tests/js-tests/src/ComponentTest

原文: http://docs.cocos.com/cocos2d-x/manual/zh/scripting/