找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 52|回复: 0

自移动平台代码

[复制链接]

31

主题

101

回帖

523

积分

老猫

积分
523
发表于 2024-8-24 13:48:52 | 显示全部楼层 |阅读模式

  1. var walk = qc.defineBehaviour('qc.engine.walk', qc.Behaviour, function() {
  2.     this.isOn = false;
  3.         this.rigidbody = null;
  4.     this.onSp = new qc.Point(0, 0);
  5.         this.offSp = new qc.Point(0, 0);
  6. }, {
  7.     startPos: qc.Serializer.POINT,
  8.         endPos: qc.Serializer.POINT,
  9.         startSpeed: qc.Serializer.NUMBER,
  10.         endSpeed: qc.Serializer.NUMBER
  11. });

  12. walk.prototype.awake = function() {
  13.         this.rigidbody = this.gameObject.getScript('qc.arcade.RigidBody');
  14.         this.onSp.x = (this.endPos.x - this.startPos.x) * this.startSpeed;
  15.         this.onSp.y = (this.endPos.y - this.startPos.y) * this.startSpeed;
  16.         this.offSp.x = (this.startPos.x - this.endPos.x) * this.endSpeed;
  17.         this.offSp.y = (this.startPos.y - this.endPos.y) * this.endSpeed;
  18. };

  19. walk.prototype.update = function() {
  20.         if(this.isOn){
  21.                 if(!this.checkReach(this.onSp, this.startPos, this.endPos)){
  22.                         this.rigidbody.velocity.x = this.onSp.x;
  23.                         this.rigidbody.velocity.y = this.onSp.y;
  24.                 }else{
  25.                         this.rigidbody.velocity.x = 0;
  26.                         this.rigidbody.velocity.y = 0;
  27.                         this.isOn = false;
  28.                 }
  29.         }else{
  30.                 if(!this.checkReach(this.offSp, this.endPos, this.startPos)){
  31.                         this.rigidbody.velocity.x = this.offSp.x;
  32.                         this.rigidbody.velocity.y = this.offSp.y;
  33.                 }else{
  34.                         this.rigidbody.velocity.x = 0;
  35.                         this.rigidbody.velocity.y = 0;
  36.                         this.isOn = true;
  37.                 }
  38.         }
  39. };

  40. walk.prototype.checkReach = function(vec, startPos, endPos) {
  41.         if(vec.x !== 0){
  42.                 if(vec.x < 0){
  43.                         if(this.gameObject.anchoredX <= endPos.x){
  44.                                 this.gameObject.anchoredX = endPos.x;
  45.                                 this.gameObject.anchoredY = endPos.y;
  46.                                 return true;
  47.                         }
  48.                 }else{
  49.                         if(this.gameObject.anchoredX >= endPos.x){
  50.                                 this.gameObject.anchoredX = endPos.x;
  51.                                 this.gameObject.anchoredY = endPos.y;
  52.                                 return true;
  53.                         }
  54.                 }
  55.         }else{
  56.                 if(vec.y < 0){
  57.                         if(this.gameObject.anchoredY <= endPos.y){
  58.                                 this.gameObject.anchoredX = endPos.x;
  59.                                 this.gameObject.anchoredY = endPos.y;
  60.                                 return true;
  61.                         }
  62.                 }else{
  63.                         if(this.gameObject.anchoredY >= endPos.y){
  64.                                 this.gameObject.anchoredX = endPos.x;
  65.                                 this.gameObject.anchoredY = endPos.y;
  66.                                 return true;
  67.                         }
  68.                 }
  69.         }
  70.         return false;
  71. };
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|WTFBBS

GMT+8, 2024-10-25 05:13 , Processed in 0.083945 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表