0

タイトル、ボタンを 1 回押すとボールが発射され、それが終わるまで何もできなくなります。以下の発砲のための現在のコード。

public void act() 
{
        // Steps for calculating the launch speed and angle
        ProjectileWorld myWorld = (ProjectileWorld) getWorld();
        double shotStrength = myWorld.getSpeedValue();
        double shotAngle = myWorld.getAngleValue();
        // Checks to see if the space key is pressed, if it is, the projectile is fired
        String key = Greenfoot.getKey();  
        if ("space".equals(key))
        { 
            //xSpeed and ySpeed changed as requirements say
            xSpeed = (int)(shotStrength*Math.cos(shotAngle));
            ySpeed = -(int)(shotStrength*Math.sin(shotAngle));
            actualX += xSpeed;
            actualY += ySpeed;
            setLocation ((int) actualX, (int) actualY);
        }

現在、これにより、スペースバーを押したときにのみボールが移動するようになります。そのため、スペースバーを離して、ボールがまだ空中にある間に shotStrength と shotAngle を変更できます

4

1 に答える 1

0

ここで重要なのは、「act」メソッドのコンテキストの状態を「moving ball」に変更することです。

これを実現する方法はいくつかありますが、基本的には act() の呼び出し方法によって異なります。

私の最初の考えは、次のようなことをすることです:

public enum ActionMode{  MovingBall, Shooting, Waiting, Etc }

public void act() 
{
        // Steps for calculating the launch speed and angle
        ProjectileWorld myWorld = (ProjectileWorld) getWorld();
        double shotStrength = myWorld.getSpeedValue();
        double shotAngle = myWorld.getAngleValue();
        // Checks to see if the space key is pressed, if it is, the projectile is fired
        String key = Greenfoot.getKey();  
        if ("space".equals(key)){
            mode = ActionMode.MovingBall;
        }
        while(mode==ActionMode.MovingBall){

            //xSpeed and ySpeed changed as requirements say
            xSpeed = (int)(shotStrength*Math.cos(shotAngle));
            ySpeed = -(int)(shotStrength*Math.sin(shotAngle));
            actualX += xSpeed;
            actualY += ySpeed;
            setLocation ((int) actualX, (int) actualY);
            if( ballHasFinishedMoving(actualX, actualY) ){
                 mode==ActionMode.Waiting;
            }
        }
        ...
}
private boolean ballHasFinishedMoving( int actualX, int actualY ){
       logic to determine if ball has finished.
}
于 2014-11-15T03:24:05.523 に答える