2

これは私が開発している最初の練習ゲームであり、加速度計を初めて使用するので、答えが明らかな場合は事前に謝罪します. また、コードと説明で回答したいと思います。

そのため、ゲームの一部として、画面外でボールが転がっています。傾きに合わせてボールが動きます。このアプリのデバイスの向きは、横向きのみです。電話の横向きを右に持ったまま電話を上下に傾けると、それに応じてボールが動きます。(上端を下に傾けるとボールが転がり、下端を下に傾けるとボールが転がります)。したがって、これらの 2 つの傾きは問題ありません。画面の左側を下に傾けると、ボールが右に転がり、その逆も同様です。私が望むのは、画面を左に傾け、ボールを左に傾け、画面を右に傾けるとボールが右に行くことです。以下のコードでこれをどのように修正しますか。おそらく2行を変更するのと同じくらい簡単だと思います。

//delegate method
-(void)outputAccelertionData:(CMAcceleration)acceleration
{
    currentMaxAccelX = 0;
    currentMaxAccelY = 0;


    if(fabs(acceleration.x) > fabs(currentMaxAccelX))
    {
        // this needs to be currentMaxAccelY not currentMaxAccelX for those of you thinking this is the solution
        currentMaxAccelY = acceleration.x; 
    }
    if(fabs(acceleration.y) > fabs(currentMaxAccelY))
    {
        // this needs to be currentMaxAccelX not currentMaxAccelY for those of you thinking this is the solution
        currentMaxAccelX = acceleration.y;
    }
}

-(void)update:(CFTimeInterval)currentTime {

    /* Called before each frame is rendered */


    float maxY = 480;
    float minY = 0;


    float maxX = 320;
    float minX = 0;

    float newY = 0;
    float newX = 0;

    //Im pretty sure the problem is in this if statement as this is what deals with the left and right tilt
    if(currentMaxAccelX > 0.05){
        newX = currentMaxAccelX * 10;
    }
    else if(currentMaxAccelX < -0.05){
        newX = currentMaxAccelX*10;
    }
    else{
        newX = currentMaxAccelX*10;
    }

    newY = currentMaxAccelY *10;

    newX = MIN(MAX(newX+self.ball.position.x,minY),maxY);
    newY = MIN(MAX(newY+self.ball.position.y,minX),maxX);


    self.ball.position = CGPointMake(newX, newY);

}
4

1 に答える 1