2012-05-06 17 views
0

私はjMonekyEngineを使い始めました。これはSwing GUIと簡単にやりとりする方法です。 こちらのチュートリアルに続きますhttp://jmonkeyengine.org/wiki/doku.php/jme3:advanced:swing_canvas静的変数は変更されません

すべてが動作し、すべてがロードされますが、問題の修正には問題があります。

それらのチュートリアル、一定の更新によると、ここに起こる:

public void simpleUpdate(float tpf) { 
    geom.rotate(0, 2 * tpf, 0); 
} 

(これはオブジェクトを回転させるのチュートリアルの例です)。 私は何をしようとしているのは、スイングGUIのActionListener内で更新を取得する変数を使って2またはtpfを変更することによって回転の速度を増減させることです。

しかし、 Swing GUIのmainメソッド内で作成されることを、私は

static float rotate = 0.0f; 

が、それはmainメソッド内で変更されます。それを変更するために、静的な変数を作成する必要がありますが、同じようにそれを使用しようとするとso:

public void simpleUpdate(float tpf) { 
    geom.rotate(0, rotate * tpf, 0); 
} 

は初期値まで一定のままです。 gui(JPanelを拡張)を作成し、getterとsetterを使用してGUIクラスを作成しようとしましたが、まだ行っていません。 助けてください! ありがとう!

EDIT:メインメソッド内

JButton faster = new JButton("Faster"); 
faster.addActionListener(new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     rotate +=0.1f; 
    } 
}); 

: は、ここで私は回転値を変更する方法を説明します。 rotateは静的フィールドです。

+1

新しい値を割り当てる場所が表示されません...実際に値を変更するには 'rotate =(something) 'してください – Jared

+0

ofcourse、これはgui内部のボタンaddActionListenerの内部で発生します。これを示すために私の質問を編集 –

+0

私もこのコードを投稿します。 – Jared

答えて

1

これは私のために働いている

http://test.jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_main_event_loop http://test.jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_input_system?s[]=input

があなたのアクションリスナーです本当にクリックでイベントがトリガーされますか?多分あなたはそこに問題があり、回転変数にはありません。この例ではスイングを使用していないことに注意してください。

import com.jme3.app.SimpleApplication; 
import com.jme3.input.KeyInput; 
import com.jme3.input.controls.ActionListener; 
import com.jme3.input.controls.KeyTrigger; 
import com.jme3.material.Material; 
import com.jme3.math.ColorRGBA; 
import com.jme3.math.Vector3f; 
import com.jme3.scene.Geometry; 
import com.jme3.scene.shape.Box; 

/** Sample 4 - how to trigger repeating actions from the main update loop. 
* In this example, we make the player character rotate. */ 
public class HelloLoop extends SimpleApplication { 

    public static void main(String[] args){ 
     HelloLoop app = new HelloLoop(); 
     app.start(); 
    } 

    protected Geometry player; 

    @Override 
    public void simpleInitApp() { 

     Box b = new Box(Vector3f.ZERO, 1, 1, 1); 
     player = new Geometry("blue cube", b); 
     Material mat = new Material(assetManager, 
      "Common/MatDefs/Misc/Unshaded.j3md"); 
     mat.setColor("Color", ColorRGBA.Blue); 
     player.setMaterial(mat); 
     rootNode.attachChild(player); 

     initKeys(); 
    } 

    /* This is the update loop */ 
    @Override 
    public void simpleUpdate(float tpf) { 
     // make the player rotate 
     player.rotate(0, val*tpf, 0); 
    } 
    float val = 2f; 
    private void initKeys() { 
     // Adds the "u" key to the command "coordsUp" 
     inputManager.addMapping("sum", new KeyTrigger(KeyInput.KEY_ADD)); 
     inputManager.addMapping("rest", new KeyTrigger(KeyInput.KEY_SUBTRACT)); 

     inputManager.addListener(al, new String[]{"sum", "rest"}); 
    } 
     private ActionListener al = new ActionListener() { 
     public void onAction(String name, boolean keyPressed, float tpf) { 
      if (name.equals("sum")) { 
       val++; 
      }else if (name.equals("rest")){ 
       val--; 
      } 
     } 
     }; 
} 
関連する問題