Always two coordinate points for the rotation angle, or the direction and position of a known angle for movement

Source: Internet
Author: User
Tags export class

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); Onassetloaded (); } onassetloaded () {this.bloaded = true; 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 (one, LAYA.STAGE.WIDTH/2, LAYA.STAGE.HEIGHT/2 + 520, 3);
This. Player2base = new Player (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 (one, LAYA.STAGE.WIDTH/2, LAYA.STAGE.HEIGHT/2 + 260, 3); }
Creatplayer2base () {this. Player2base = new Player (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++; Pawn.setrotation (This.nrotationangle); } else {this.nrotationangle--; Pawn.setrotation (This.nrotationangle); } if (this. Ball.obj = = null) {this.bisplayer2rotate = true; this.nballdestroycount++; 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 = n + angle;} if (this.bisplayer2rotate) {if (this.nballdestroycount% 2 = = 0) {this.nplayer2angle = this.nplayer2angle% + 1;} if (t His.nballdestroycount% 2 = = 1) {this.nplayer2angle = this.nplayer2angle% 360-1; if (this.nplayer2angle <= 0) {This . Nplayer2angle = 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% = = 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 ("Player 2 score" + 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++; 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 ("Player 1 score" + 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) {}}
}}

Always two coordinate points for the rotation angle, or the direction and position of a known angle for movement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.