Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

程序设计存在问题. #8

Open
cocos543 opened this issue Mar 30, 2017 · 0 comments
Open

程序设计存在问题. #8

cocos543 opened this issue Mar 30, 2017 · 0 comments

Comments

@cocos543
Copy link

//
现在要添加主角收集星星的行为逻辑了,这里的重点在于,星星要随时可以获得主角节点的位置,才能判断他们之间的距离是否小于可收集距离,如何获得主角节点的引用呢?别忘了我们前面做过的两件事:

  1. Game组件中有个名叫player的属性,保存了主角节点的引用。
  2. 每个星星都是在Game脚本中动态生成的。
    所以我们只要在Game脚本生成Star节点实例时,将Game组件的实例传入星星并保存起来就好了,之后我们可以随时通过game.player来访问到主角节点。
    //

这句话,原本Star就被Game引用了,现在又把Game赋值给Star.这种设计思路不太好会不会?循环引用容易出现内存泄漏问题.最好就是把player直接赋值给Star,因为player和Star是并行关系.

按照我的思路,拾取到星星后的处理应该定义在Game里面,由Game去生成新的星星,摧毁老的星星,增加积分.这个功能可以在Star类里声明一个回调函数,在Game里面设置回调函数的具体工作.

//Game.js
spawnNewStar: function() {
// 使用给定的模板在场景中生成一个新节点
var newStar = cc.instantiate(this.starPrefab);
var star = newStar.getComponent('Star');
var self = this;
star.player = this.player;
star.pickedCallBackFunc = (star) => {
star.node.destroy();
self.spawnNewStar();
self.gainScore();
};

    // 重置计时器,根据消失时间范围随机取一个值
    star.duration = this.starDuration = this.minStarDuration + cc.random0To1() * (this.maxStarDuration - this.minStarDuration);
    this.timer = 0;
    
    // 将新增的节点添加到 Canvas 节点下面
    this.node.addChild(newStar);
    // 为星星设置一个随机位置
    newStar.setPosition(this.getNewStarPosition());
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant