资讯专栏INFORMATION COLUMN

用EC5/EC6自定义class的区别及用法 -- Phaser3网页游戏框架

HtmlCssJs / 2735人阅读

摘要:自定义自定义完整代码更多游戏教学为游戏开发深感自豪

EC6 自定义class

</>复制代码

  1. class Brain extends Phaser.GameObjects.Sprite {
  2. constructor (scene, x, y)
  3. {
  4. super(scene, x, y);
  5. this.setTexture("brain");
  6. this.setPosition(x, y);
  7. }
  8. preUpdate (time, delta)
  9. {
  10. super.preUpdate(time, delta);
  11. this.rotation += 0.01;
  12. }
  13. }

EC5 自定义class

</>复制代码

  1. var Bunny = new Phaser.Class({
  2. Extends:Phaser.GameObjects.Sprite,
  3. initialize:function Bunny(scene,x,y,speed){
  4. Phaser.GameObjects.Sprite.call(this,scene);
  5. this.setTexture("bunny");
  6. this.setPosition(x, y);
  7. this.setScale(0.3);
  8. this.speed = speed;
  9. },
  10. preUpdate(time,delta){
  11. this.rotation += (0.01+ this.speed * 0.0001) ;
  12. }
  13. });

完整代码:

</>复制代码

  1. var Bunny = new Phaser.Class({
  2. Extends:Phaser.GameObjects.Sprite,
  3. initialize:function Bunny(scene,x,y,speed){
  4. Phaser.GameObjects.Sprite.call(this,scene);
  5. this.setTexture("bunny");
  6. this.setPosition(x, y);
  7. this.setScale(0.3);
  8. this.speed = speed;
  9. },
  10. preUpdate(time,delta){
  11. this.rotation += (0.01+ this.speed * 0.0001) ;
  12. }
  13. });
  14. var config = {
  15. type: Phaser.AUTO,
  16. width: 600,
  17. height: 480,
  18. parent: "phaser-example",
  19. scene: {
  20. preload: preload,
  21. create: create
  22. }
  23. };
  24. var game = new Phaser.Game(config);
  25. function preload (){
  26. //http://www.ifiero.com/uploads/ifiero-logo_512x512.png
  27. // this.load.image("bunny", "http://www.ifiero.com/images/ifiero-logo_512x512.png");
  28. this.load.setBaseURL("http://labs.phaser.io");
  29. this.load.image("bg", "assets/pics/purple-dots.png");
  30. this.load.image("bunny", "assets/sprites/bunny.png");
  31. }
  32. function create (){
  33. this.add.image(0, 0, "bg").setOrigin(0).setScale(0.8);
  34. this.add.existing(new Bunny(this, 150, 150,100));
  35. this.add.existing(new Bunny(this, 250, 250,200));
  36. this.add.existing(new Bunny(this, 350, 350,300));
  37. }

更多游戏教学:www.iFIERO.com -- 为游戏开发深感自豪

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/99749.html

相关文章

  • EC5/EC6定义class区别法 -- Phaser3网页游戏框架

    摘要:自定义自定义完整代码更多游戏教学为游戏开发深感自豪 showImg(https://segmentfault.com/img/remote/1460000017262663); EC6 自定义class class Brain extends Phaser.GameObjects.Sprite { constructor (scene, x, y) { ...

    Imfan 评论0 收藏0
  • Phaser3 场景Scene之间传值 -- HTML网页游戏开发

    摘要:一首先当然得有至少有二个场景二从场景传值到场景二种方法通过事件从通过时传值到时有个需要特别注的事项就是,得把的设为否则因为还未激活,是监听不到事件的通过场景启动具体详见代码在整个工程中只会执行一次每次调用场景会执行一次从事件传 showImg(https://segmentfault.com/img/remote/1460000016953682); 一、首先当然得有至少有二个场景sc...

    you_De 评论0 收藏0
  • Phaser3 场景Scene之间传值 -- HTML网页游戏开发

    摘要:一首先当然得有至少有二个场景二从场景传值到场景二种方法通过事件从通过时传值到时有个需要特别注的事项就是,得把的设为否则因为还未激活,是监听不到事件的通过场景启动具体详见代码在整个工程中只会执行一次每次调用场景会执行一次从事件传 showImg(https://segmentfault.com/img/remote/1460000016953682); 一、首先当然得有至少有二个场景sc...

    ARGUS 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<