현재 스프라이트가 캔버스에서 움직이는 경우 캔버스의 측면을 치면 튀어 나오게됩니다. 스프라이트가 캔버스의 임의의 위치에서 다른 방향으로 바뀌게하는 방법이 있습니까?스프라이트 이미지의 무작위 이동 위치
여기 방향의 변화에 대한 내 코드이며 이동 방법 :
Fish.prototype.changeDirection = function() {
speedXSign = this.speedX > 0 ? 1 : -1;
speedYSign = this.speedY > 0 ? 1 : -1;
this.speedX = speedXSign * (1 + Math.random() * 2);
this.speedY = speedYSign * (1 + Math.random() * 2);
};
Fish.prototype.move = function() {
this.animIndex++;
if (this.animIndex == animFrames.length) this.animIndex = 0;
this.xPos += this.speedX;
if ((this.xPos + this.frameWidth * this.frameScale/2) >= canvas.width && this.speedX > 0 ||
(this.xPos - this.frameWidth * this.frameScale/2) <= 0 && this.speedX <= 0) {
this.speedX = -this.speedX;
}
this.yPos += this.speedY;
if ((this.yPos + this.frameHeight * this.frameScale/2) >= canvas.height && this.speedY > 0 ||
(this.yPos - this.frameHeight * this.frameScale/2) <= 0 && this.speedY <= 0) {
this.speedY = -this.speedY;
}
};
@Bernard 당신은 별도의 질문을해야 속도를 변경해야하는 경우, Zacru 아주 잘 현재의 질문에 대답했다과 upvote에를 얻고 내게 투표 +1을 받아 들여야한다 좋은 작업. – Loktar