mirror of
https://github.com/Leo501/CocosCreatorTutorial.git
synced 2026-05-21 01:48:18 +08:00
优化代码、添加原理说明
This commit is contained in:
@@ -11,7 +11,7 @@ cc.Class({
|
||||
onLoad() {
|
||||
this.halfBoxLength = parseInt(this.boxLength / 2);
|
||||
this.perimeter = 0;
|
||||
this.perimeterMax = 1000 + 500 * Math.PI;
|
||||
this.perimeterMax = this.boxLength * 2 + this.r * 2 * Math.PI;
|
||||
},
|
||||
|
||||
registerEvent() {
|
||||
@@ -21,11 +21,12 @@ cc.Class({
|
||||
|
||||
},
|
||||
/**
|
||||
* 定义
|
||||
* 0到500为右直线
|
||||
* 500到500+PI*r为上半圆
|
||||
* 500+PI*r到1000+PI*r为左直线
|
||||
* 最后1000+2PI*4为下半圆
|
||||
* 原理:距离s=时间t*速度v
|
||||
* 实现 -- 分割为四部分
|
||||
* 第一部分 -- 0到500为右直线
|
||||
* 第二部分 -- 500到500+PI*r为上半圆
|
||||
* 第三部分 -- 500+PI*r到1000+PI*r为左直线
|
||||
* 第四部分 -- 最后1000+2PI*4为下半圆
|
||||
* @param {*} dt
|
||||
* @param {*} speed
|
||||
*/
|
||||
@@ -35,7 +36,7 @@ cc.Class({
|
||||
let point = cc.v2(0, 0);
|
||||
if (this.perimeter <= this.boxLength) {
|
||||
point.x = this.r;
|
||||
point.y = -250 + this.perimeter;
|
||||
point.y = -this.halfBoxLength + this.perimeter;
|
||||
} else if (this.perimeter <= (this.boxLength + Math.PI * this.r)) {
|
||||
let length = this.perimeter - this.boxLength;
|
||||
let radian = length / this.r;
|
||||
@@ -43,7 +44,7 @@ cc.Class({
|
||||
point.y = this.halfBoxLength + this.r * Math.sin(radian);
|
||||
} else if (this.perimeter <= (2 * this.boxLength + Math.PI * this.r)) {
|
||||
point.x = -this.r;
|
||||
point.y = 250 - (this.perimeter - this.boxLength - Math.PI * this.r);
|
||||
point.y = this.halfBoxLength - (this.perimeter - this.boxLength - Math.PI * this.r);
|
||||
} else {
|
||||
let length = this.perimeter - 2 * this.boxLength;
|
||||
let radian = length / this.r;
|
||||
@@ -53,42 +54,6 @@ cc.Class({
|
||||
return point;
|
||||
},
|
||||
|
||||
getTrackByPoint(point, length) {
|
||||
console.log('getTrackByPoint');
|
||||
// point.x = Math.floor(point.x);
|
||||
// point.y = Math.floor(point.y);
|
||||
//效正由于半圆带来偏差
|
||||
if (point.x > this.r) {
|
||||
point.x = this.r;
|
||||
} else if (point.x < -this.r) {
|
||||
point.x = -this.r;
|
||||
}
|
||||
console.log(Math.abs(point.y) <= this.halfBoxLength, point.x == this.r);
|
||||
//box的右边轨道
|
||||
if (point.x == this.r && Math.abs(point.y) <= this.halfBoxLength) {
|
||||
point.y += length;
|
||||
return point;
|
||||
//box的上半圆
|
||||
} else if (Math.abs(point.x) <= this.r && point.y >= this.halfBoxLength) {
|
||||
//弧度
|
||||
let radian = length / this.r;
|
||||
point.x = this.r * Math.cos(radian);
|
||||
point.y = this.halfBoxLength + this.r * Math.sin(radian);
|
||||
return point;
|
||||
//左边
|
||||
} else if (point.x == -this.r && Math.abs(point.y) <= this.halfBoxLength) {
|
||||
point.y -= length;
|
||||
return point;
|
||||
//下半圆
|
||||
} else if (Math.abs(point.x) < this.r && point.y <= - this.halfBoxLength) {
|
||||
//弧度
|
||||
let radian = length / this.r;
|
||||
point.x = this.r * Math.cos(radian);
|
||||
point.y = -this.halfBoxLength - this.r * Math.sin(radian);
|
||||
return point;
|
||||
}
|
||||
},
|
||||
|
||||
update(dt) {
|
||||
// let point = this.nodeTarget.getPosition();
|
||||
let movePoint = this.getPointByPerimeter(dt, this.speed);
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
{
|
||||
"ver": "2.2.0",
|
||||
"ver": "1.0.0",
|
||||
"uuid": "6aa0aa6a-ebee-4155-a088-a687a6aadec4",
|
||||
"type": "sprite",
|
||||
"wrapMode": "clamp",
|
||||
"filterMode": "bilinear",
|
||||
"premultiplyAlpha": false,
|
||||
"subMetas": {
|
||||
"HelloWorld": {
|
||||
"ver": "1.0.3",
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
{
|
||||
"ver": "2.2.0",
|
||||
"ver": "1.0.0",
|
||||
"uuid": "a8027877-d8d6-4645-97a0-52d4a0123dba",
|
||||
"type": "sprite",
|
||||
"wrapMode": "clamp",
|
||||
"filterMode": "bilinear",
|
||||
"premultiplyAlpha": false,
|
||||
"subMetas": {
|
||||
"singleColor": {
|
||||
"ver": "1.0.3",
|
||||
|
||||
Reference in New Issue
Block a user