getAngle: function(obj1, obj2) {
// angle in radians
var angleRadians = Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x);
// angle in degrees
var angleDeg = (Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x) * 180 / Math.PI);
return angleDeg;
},
각도를 얻으려면이 두 객체를 전달해야합니다.
var angle = this.getAngle (goodGuy, monster);
다음은 우주선을 돌리기 위해 마우스와 우주선 사이의 각도를 사용하는 예입니다.
우주선이 마우스를 가리 키도록하려면 아무 곳이나 클릭하십시오.
그리고 예제 코드는 다음과 같습니다.
var StateMain = {
preload: function() {
game.load.image("ship", "images/ship.png");
},
create: function() {
this.ship = game.add.sprite(game.world.centerX, game.world.centerY, "ship");
this.ship.anchor.set(0.5, 0.5);
game.input.onUp.add(this.clickCanvas, this);
},
clickCanvas: function() {
//make an object from the mouse postion
var obj1 = {
x: game.input.x,
y: game.input.y
};
//get the angle between the mouse and the ship and assign that to the
//ship's angle
this.ship.angle = this.getAngle(obj1, this.ship);
},
getAngle: function(obj1, obj2) {
//I use the offset because the ship is pointing down
//at the 6 o'clock position
//set to 0 if your sprite is facing 3 o'clock
//set to 180 if your sprite is facing 9 o'clock
//set to 270 if your sprite is facing 12 o'clock
//
offSet = 90;
// angle in radians
var angleRadians = Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x);
// angle in degrees
var angleDeg = (Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x) * 180 / Math.PI);
//add the offset
angleDeg += offSet;
return angleDeg;
},
update: function() {}
}
댓글 없음:
댓글 쓰기