私はトップ ダウン シューティング ゲームに取り組んでいます。基本的に、キャラクターは画面の中央、四角形 (セーフ ゾーン) 内から始まります。キャラクターは静的ではなく、シーンは静的です。彼は安全地帯の中を歩き回ることができます。キャラクターがこのゾーンから出るとすぐに、静態が切り替わります...キャラクターは静態であり、シーンは彼の周りを移動しています。
これに関する唯一の問題は、セーフ ゾーンに戻ることができず、スタティックが再び切り替わってしまうことです。
だから私は永遠にゾーンの外で立ち往生しています。私がしているのは、キャラクターの位置が特定の値 (rect) の「範囲内」にあるかどうかを確認することだけです。彼が外にいる場合は、KeyControls がキャラクターではなくマップに影響します。
これが私の境界 (セーフ ゾーン) チェッカーです。
//Walking Window Boundaries
    var boundarySizeX = 400;
    var boundarySizeY = 200;
    ctxWalkBoundary.fillStyle = "grey";
    ctxWalkBoundary.fillRect(gameWidth/2 - boundarySizeX/2, gameHeight/2 - boundarySizeY/2, boundarySizeX, boundarySizeY);
    ctxWalkBoundary.clearRect((gameWidth/2 - boundarySizeX/2) + 2, (gameHeight/2 - boundarySizeY/2) + 2, (boundarySizeX) - 4, (boundarySizeY) -4 );
    var paddingLeft = (gameWidth - boundarySizeX) / 2;
    var paddingRight = gameWidth - ((gameWidth - boundarySizeX) / 2) - this.charWidth;
    var paddingTop = (gameHeight - boundarySizeY) / 2;
    var paddingBottom = gameHeight - ((gameHeight - boundarySizeY) / 2) - this.charHeight;
    var paddingY = (gameHeight - boundarySizeY) / 2;
    if(this.drawX > paddingLeft && this.drawX < paddingRight && this.drawY > paddingTop && this.drawY < paddingBottom){
        inBoundary = true;
    }
    else{
        inBoundary = false;
        console.debug("Out Of Boundary!");
    }
そして、これは私のキーチェッカーです:
//UP
    if(this.isUpKey){
        //Character movement
        if(inBoundary){
            this.drawX = this.drawX + this.speed * Math.cos((this.characterRotation));
            this.drawY = this.drawY + this.speed * Math.sin((this.characterRotation));
        }
        else{
            mapPositionX = mapPositionX - this.speed * Math.cos((this.characterRotation));
            mapPositionY = mapPositionY - this.speed * Math.sin((this.characterRotation));
        }
キャラクターは常にマウスの方を向いています (回転します)。したがって、ユーザーが W または Up を押すたびに、キャラクターは常にマウスの位置に向かって歩きます。
ゾーンに戻る方法はありますか?
- - - アップデート - - -
どうにかして、まだセーフ ゾーンの外を向いているかどうかを確認する必要があると思います。そうでない場合は、スタティックを逆にします。