一直兩個座標點求旋轉角度,或已知角度求移動的方向和位置

來源:互聯網
上載者:User

標籤:tga   camera   names   ttl   manage   class   instanced   math   namespace   

namespace Pioneer{ export class Game extends Behaviour{private nState:number;private Camera:Camera2D; nScore:number;nGold:number;private nRotationAngle:number = 0;private nRotationCount:number = 0;
private nFrameRateTimer:number; private nScoreTimer:number; private bLoaded:boolean;
private Bg:any; private Ball:any; private Player1:any; private Player2:any; private Player1Base:any; private Player2Base:any; private Btn1:any; private YuanStage:any;
private vSpeed:any; private nPlayer1PosX:number = Laya.stage.width/2; private nPlayer1PosY:number = Laya.stage.height/2+260; private nPlayer2ToBallDistance:number; private nBallPos:any; private nPlayer2Pos:any; private nOffsetX:any; private nOffsetY:any; private nMoveTimer:number = 0; private bisPlayer2Rotate:boolean = true; private nPlayer2Angle:number = 0; private nBallDestroyCount:number = 0; private nYuanStagePos:any; private nPlayer1Pos:any;
private Pawn:any; private PawnOffset:any; private bDrag = false;
private Scene:Laya.Sprite; private GameField:Laya.Rectangle;
private nEndTimer = -1; YCenter = 0; super() { this.bLoaded = false; this.nFrameRateTimer = 0; } Start() { this.vSpeed = new EVector3(1, 1, 1); this.OnAssetLoaded(); } OnAssetLoaded() { this.bLoaded = true; this.Scene = new Laya.Sprite(); this.Scene.hitArea = new Laya.Rectangle(0, 0, Laya.stage.width, Laya.stage.height);
this.InitEvents(this.Scene);  SceneManager.SetStage(this.Scene); Laya.stage.addChildAt(this.Scene, 0);  this.Bg = new Entity(1); this.Bg.Show(); this.Bg.SetPosition(Laya.stage.width / 2, Laya.stage.height / 2);
this.Ball = new Ball(4); this.Ball.Show(); this.Ball.SetPosition(Laya.stage.width / 2, Laya.stage.height / 2 + 260 ); this.Ball.SetLayer(4);
this.YuanStage = new Entity(10); this.YuanStage.Show(); this.YuanStage.SetPosition(Laya.stage.width / 2, Laya.stage.height / 2 + 260); this.YuanStage.SetLayer(1);
this.Player1 = new Player(5, Laya.stage.width / 2, Laya.stage.height / 2 + 520, 4); this.Pawn=this.Player1;
this.Player2 = new Player(6, Laya.stage.width / 2, Laya.stage.height / 2, 4 );
this.Player1Base = new Player(11, Laya.stage.width / 2, Laya.stage.height / 2 + 520, 3);
this.Player2Base = new Player(12, Laya.stage.width / 2, Laya.stage.height / 2, 3);
this.nPlayer1PosX = Laya.stage.width / 2; this.nPlayer1PosY = Laya.stage.height / 2 + 520; this.nYuanStagePos = this.YuanStage.GetPosition();
this.ResetGame(); this.StartGame(); }
StartGame() { DlgBattle.GetInstance().Show(true); } GetPlayerScore(id) { if (id == 1) { return this.Player1.nPlayerScore; } else { return this.Player2.nPlayerScore; } }
CreatBall() { this.Ball = new Ball(4); this.Ball.Show(); this.Ball.SetPosition(this.Ball.RandamPosX(), this.Ball.RandamPosY()); this.Ball.SetLayer(4); }
CreatPlayer1() { this.Player1 = new Player(5, Laya.stage.width / 2, Laya.stage.height / 2 + 260, 4 ); this.Pawn = this.Player1; }
CreatPlayer2() { this.Player2 = new Player(6, Laya.stage.width / 2, Laya.stage.height / 2 + 260, 4 ); }
CreatPlayer1Base() { this.Player1Base = new Player(11, Laya.stage.width / 2, Laya.stage.height / 2 + 260, 3); }
CreatPlayer2Base() { this.Player2Base = new Player(12, Laya.stage.width / 2, Laya.stage.height / 2 + 260, 3 ); }
ResetGame() { this.nGold = 0; this.nScore = 0; }
Update() { if(this.nRotationCount % 2 == 0) { this.nRotationAngle++; this.Pawn.SetRotation(this.nRotationAngle); } else { this.nRotationAngle--; this.Pawn.SetRotation(this.nRotationAngle); } if(this.Ball.Obj == null) { this.bisPlayer2Rotate = true; this.nBallDestroyCount++; this.CreatBall(); }
this.nBallPos = this.Ball.GetPosition(); this.nPlayer2Pos = this.Player2.GetPosition(); this.nPlayer1Pos = this.Player1.GetPosition(); let nStageToBallPos = Math.pow(this.nBallPos.x - this.nYuanStagePos.x, 2) + Math.pow(this.nBallPos.y - this.nYuanStagePos.y, 2); let nStageToPlayer1Pos = Math.pow(this.nPlayer1Pos.x - this.nYuanStagePos.x, 2) + Math.pow(this.nPlayer1Pos.y - this.nYuanStagePos.y, 2); let nStageToPlayer2Pos = Math.pow(this.nPlayer2Pos.x - this.nYuanStagePos.x, 2) + Math.pow(this.nPlayer2Pos.y - this.nYuanStagePos.y, 2);  if(nStageToPlayer1Pos > 90000) { this.Player1.Destroy(); this.Player1Base.Destroy(); this.nPlayer1PosX = Laya.stage.width / 2; this.nPlayer1PosY = Laya.stage.height / 2 + 260; this.CreatPlayer1(); this.CreatPlayer1Base(); }
if(nStageToPlayer2Pos > 90000) { this.Player2.Destroy(); this.Player2Base.Destroy(); this.nPlayer1PosX = Laya.stage.width / 2; this.nPlayer1PosY = Laya.stage.height / 2 + 260; this.CreatPlayer2(); this.CreatPlayer2Base(); }
if(nStageToBallPos > 90000) { this.Ball.Destroy(); this.CreatBall(); }
this.nPlayer2ToBallDistance = Math.pow(this.nBallPos.x - this.nPlayer2Pos.x, 2) + Math.pow(this.nBallPos.y - this.nPlayer2Pos.y, 2); this.nOffsetX = this.nBallPos.x - this.nPlayer2Pos.x; this.nOffsetY = this.nBallPos.y - this.nPlayer2Pos.y;
let angle = Math.atan2(this.nOffsetY, this.nOffsetX) * 180 / Math.PI + 90; if(angle <= 0){ angle = 360 + angle; }  if(this.bisPlayer2Rotate) { if(this.nBallDestroyCount % 2 == 0){ this.nPlayer2Angle = this.nPlayer2Angle % 360 + 1; } if(this.nBallDestroyCount % 2 == 1) { this.nPlayer2Angle = this.nPlayer2Angle % 360 - 1 ; if(this.nPlayer2Angle <= 0){ this.nPlayer2Angle = 360 + this.nPlayer2Angle; } }  if(Math.abs(this.nPlayer2Angle - Math.abs(angle)) < 1){ this.bisPlayer2Rotate = false; }
this.Player2.SetRotation(this.nPlayer2Angle);
if(!this.bisPlayer2Rotate){ this.Player2.SetRotation(angle); }
}  this.nMoveTimer++;
if(this.nMoveTimer % 20 == 0 && !this.bisPlayer2Rotate) { this.Player2.SetPosition(this.nPlayer2Pos.x + this.nOffsetX / 5, this.nPlayer2Pos.y + this.nOffsetY / 5); this.Player2Base.SetPosition(this.nPlayer2Pos.x + this.nOffsetX / 5, this.nPlayer2Pos.y + this.nOffsetY / 5); }  if(this.Player2.TestCollider(this.Ball)) { this.Ball.Destroy(); this.Player2.nPlayerScore++; console.log("玩家2的分數" + this.Player2.nPlayerScore);
if (DlgBattle.IsInstanced()) { DlgBattle.GetInstance().UpdateLifeList(); } }
} OnMouse(e:Laya.Event) { if (!this.Pawn) { return; } if (e.type == Laya.Event.MOUSE_DOWN) { this.nRotationCount++; if (this.Bg.TestContains(e.stageX, e.stageY)) { //this.Pawn.SetScale(1.2,1.2); let x1 = Math.cos((this.nRotationAngle - 90) * Math.PI / 180); let y1 = Math.sin((this.nRotationAngle - 90) * Math.PI / 180); this.Pawn.SetPosition(this.nPlayer1PosX += x1 * 30,this.nPlayer1PosY += y1 * 30); this.Player1Base.SetPosition(this.nPlayer1PosX, this.nPlayer1PosY);  if(this.Pawn.TestCollider(this.Ball)) { this.Ball.Destroy(); this.Player1.nPlayerScore++; console.log("玩家1的分數" + this.Player1.nPlayerScore);  if (DlgBattle.IsInstanced()) { DlgBattle.GetInstance().UpdateLifeList(); } } }
} else if(e.type == Laya.Event.MOUSE_UP){ //this.Pawn.SetScale(1, 1); } else if (e.type == Laya.Event.MOUSE_OUT) { } }
  }}   

 

 

 

一直兩個座標點求旋轉角度,或已知角度求移動的方向和位置

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.