2016-08-27 5 views
0

ゲームエンジンなしで2Dゲームを作成する方法を学んでいますが、とにかく私はすでに背景スクロールを作成しました。私の目標はキャラクターを飛ばすことです。しかし、私のアプリを起動するたびに、キャラクターが上下に回転していて、バックグラウンドに移動するだけです。単純なジャンプをしようとする

ここに私の文字コード

public class Deer extends GameCharacter { 

private Bitmap spritesheet; 
private double dya; 
private boolean playing; 
private long startTime; 
private boolean Jump; 
private Animate Animation = new Animate(); 

public Deer(Bitmap res, int w, int h, int numFrames) { 

    x = 20; 
    y = 400; 
    dy = 0; 
    height = h; 
    width = w; 

    Bitmap[] image = new Bitmap[numFrames]; 
    spritesheet = res; 

    for (int i = 0; i < image.length; i++) 
    { 
     image[i] = Bitmap.createBitmap(spritesheet, i*width, 0, width, height); 
    } 

    Animation.setFrames(image); 
    Animation.setDelay(10); 
    startTime = System.nanoTime(); 

} 

public void setJump(boolean b){ 
    Jump = b; 
} 

public void update() 
{ 
    long elapsed = (System.nanoTime()-startTime)/1000000; 
    if(elapsed>100) 
    { 
    } 
    Animation.update(); 

    if(Jump){ 
     dy = (int)(dya+=5.5); 

    } 
    else{ 
     dy = (int)(dya+=5.5); 
    } 

    if(dy>14)dy = 14; 
    if(dy>14)dy = -14; 

    y += dy*2; 
    dy = 0; 
} 


public void draw(Canvas canvas) 
{ 
    canvas.drawBitmap(Animation.getImage(),x,y,null); 
} 
public boolean getPlaying(){return playing;} 
public void setPlaying(boolean b){playing = b;} 
public void resetDYA(){dya = 0;} 
} 

Xの - 文字の水平位置 Y - 文字の垂直位置 DX - キャラクターの水平加速度の のDY - 文字の垂直加速度

public boolean onTouchEvent(MotionEvent event) { 
     if(event.getAction()==MotionEvent.ACTION_DOWN) { 
     if(!deer.getPlaying()) { 
      deer.setPlaying(true); 
     } 
     deer.setJump(true); 
     return true; 
    } 

    return super.onTouchEvent(event); 
} 

答えて

0

は、私が言うことができませんあなたが他の疑わしいコードを持っているので、これが唯一の問題であるかどうか確かめてください。

if(Jump){ 
    dy = (int)(dya+=5.5); 
} else { 
    dy = (int)(dya+=5.5); 
} 

Jumpもしあなたが上下方向の加速度を設定しtrueです。しかし、Jumpfalseの場合は、垂直方向の加速度を同じ値に設定します。また、Jumpfalseに設定されているコードには表示されません。

コードのもう一つの奇数ビットは次のとおりです。ここで

if(dy>14)dy = 14; 
if(dy>14)dy = -14; 

、あなたは次にあなたが直後dy>14をチェック14に設定している場合dy>14。もちろん、今回はfalseです。しかし、これらの2つの条件が同じであるため、2つ目の条件は、それが保証されていないことを前にして以来、決して通過しません。唯一の選択肢は、両方とも失敗することです。 IOW、あなたは決して第二のifに入ることはできません。


すべてのことはさておき、は、私はあなたがこのアプローチを取っている理由はわかりません。一定の加速力、初期速度、地面との衝突(または元々の高さ)を確認し、それを走らせるだけで、物理方程式に簡単に頼ることができます。たとえば、次のように

// These are the variables you need. 
int x = 200, y0 = 0, y = 0, velocity = 15; 
double t = 0.0, gravity = -9.8; 

// This is the statement that should run when you update the GUI. 
// It is the fundamental equation for motion with constant acceleration. 
// The acceleration is the gravitational constant. 
y = (int) (y0 + velocity * t + .5 * gravity * t * t); 
if (y < 0) { 
    y = y0 = 0; 
    //Stop jumping! 
    Jump = false; 
} else { 
    // Swap the y values. 
    y0 = y; 
    // Increase the time with the frame rate. 
    t += frameRate; 
} 
// Draw the character using the y value 

これについての最もよい部分式が自動的にあなたをもたらすので、あなたはあなたが最大の高さに到達したときを心配する必要はありませんです。あたかも機械が本物であるかのように自然に見える。やってみて。


あなたがで遊ぶことができ、簡単なSwingの例。値は、コンポーネントが画面に描画される方法を扱うために異なることに注意してください。通常、変換でそれを処理しますが、これはタスクのために行います。

public class Main { 

    static Timer timer; 

    Main() { 
     JFrame frame = new JFrame("Hello sample"); 
     frame.setSize(new Dimension(550, 550)); 
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     JPanel panel = new MyPanel(); 
     frame.add(panel); 
     frame.setVisible(true); 
     timer = new Timer(5, (e) -> panel.repaint()); 
     timer.start(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(Main::new); 
    } 

    class MyPanel extends JPanel { 
     int x = 200, y0 = 300, y = 0, w = 200, h = 200, v = -8; 
     double t = 0.0, gravity = 9.8; 
     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.setColor(Color.RED); 
      y = (int) (y0 + v * t + .5 * gravity * t * t); 
      if (y > 300) { 
       y = y0 = 300; 
       // To prevent it from stopping comment the timer.stop() and 
       // uncomment the t = 0.0 statements. 
       //t = 0.0; 
       timer.stop(); 
      } else { 
       y0 = y; 
       t += .025; 
      } 
      g.drawOval(x, y, w, h); 
     } 
    } 
} 
+0

(ジャンプ){ dy =(int)(dya- = 1.1); } else { dy =(int)(dya + = 1。1)。 } if(dy> 14)dy = 14; if(dy <-14)dy = -14; y + = dy * 2; dy = 0; しかし、チャプターが下がり、キャラクターが消えてしまいます – noobkoder

+0

答えに加えた解決策を考えてください。それはあなた自身の物理学を取ることよりもはるかにうまく役立つでしょう。 @noobkoder – ChiefTwoPencils

+0

ありがとう、私はそれを試してみるつもりです。私はそれがフレームワークまたはゲームエンジンを使用して独自の2Dゲームを作る方が良いと聞いた – noobkoder

関連する問題