2016-10-12 11 views
-1

私たちの大学では、現在処理中ですので少しゲームをすることにしました。しかし、私がゲームを開始すると、「応答なし」の後にクラッシュします。私は学校でC++の無限ループに入っても同様の問題を抱えていましたが、間違いを見つけられませんでした。うまくいけば、誰かが私を助けることができます:)処理:無限ループ(?)

/* 

Project: Circle Munch 

Project explanation: The player has to collect the green circles with the moveable circle via W, A, S, D or arrow keys and avoid the red ones -- Project for "Medieninformatik" WS 2016/2017      

Coder: Jannik Indorf 

Date of last edit: 12.10.2016 

Ideas of interest: diff. big circles, different points 


*/ 


float m_x = width/2;      // X-coordinate starting point (center) for: player 
float m_y = height/2;      // Y-coordinate "           " 
float [] o_x;        // X-c. for : object 
float [] o_y;        // Y-c. "    " 
float background_r = random(100);   //Background rgb values. 0-100 are dark, player and objects are bright 
float background_g = random(100); 
float background_b = random(100); 
int Score = 0;         // Player-Score 
int m_radius = 40;       // player radius 
int o_radius= 30;       // Object radia 
color g = color(255,0,0);     // color green 
color r = color(0,255,0);     // color red 
int[] o_color = {g,r};      // Array for color red && green → Function: color detection 
int Counter_o = 1;       // Obstacle Counter 
int o_c_v = o_color.length;     // length of Array: o_color for randomizing the outcome 

void setup()         
{ 
    size(640,480); 
    frameRate(60*4); 
    ellipseMode(CENTER); 
} 

void draw() 
{ 

// ********** Background color declaration ************************* 
    background(background_r,background_g,background_b); 

// ***********Drawing the Player ************************ 
fill(255,255,200); 
    ellipse(m_x,m_y, m_radius, m_radius);      // Moving Object 

// ********** TextOut f. Score ************************* 
    fill(0); 
    textSize(16); 
    textAlign(CENTER,CENTER); 
    text(Score,m_x,m_y); 

// ********** User-Input ************************* 
UserInput(); 
//****** Staying inside the window.. ********* 
InsideWindow(); 

    // 
    if(Score % 10 == 0 || Score == 0) 
    { 
     Counter_o = Score/10 +1; 

      /*for every Counter_o : draw 1 Ellipse. → for every Counter_o, make 2 new variables for every new object x and object y */ 


      for (int i=0; i<= Counter_o; i++) 
      { 

       for (int j=0; j <= i; j++) 
       { 
        o_x[j] = random(15,625); 
        o_y[j] = random(15,465);   

        o_c_v= (int) random(1,2); 
        fill(o_color[o_c_v]); 

        ellipse(o_x[j],o_y[j], o_radius, o_radius); 

       } 


      } 
    } 

// *********** Collision detection ************************ 

    for(int k =0 ; k< o_x.length ; k++) 
    { 
    if (dist(m_x , m_y, o_x[k], o_y[k]) <= m_radius/2 + o_radius/2) 
    { 
     Score = color_check(o_color[k]); 
     o_x[k] = random(15,625); 
     o_y[k] = random(15,465); 
    } 
    } 

} // *************End of loop************************************************************** 
/************************************************************************* 


Collision detection between circles 
Jannik Indorf 
12.10.2016 

**************************************************************************/ 

int color_check(int check_o_color) 
{ 
    int Score_difference = 0; 


    if (check_o_color == o_color[0]) // Green Obstacles increase the score 
    { 
    Score_difference = 5; 

    background_r = random(100); 
    background_g = random(100); 
    background_b = random(100); 
    } 
    else       // Red Obstacles lower the score 
    { 
    Score_difference = -10; 
    background_r = random(100); 
    background_g = random(100); 
    background_b = random(100); 

    } 

    m_radius = m_radius + Score_difference; 

return Score_difference; 
} 
/************************************************************************/ 
/************************************************************************* 


User Input via W,A,S,D or ↑ ↓ → ← 
Jannik Indorf 
12.10.2016 

**************************************************************************/ 

void UserInput() 
{ 
    if (keyPressed){           
    if(key =='w' || key == 'W'||keyCode == UP) 
    { 
     m_y = m_y - 1; 
    } 
    if(key =='a' || key == 'A'||keyCode == LEFT) 
    { 
     m_x = m_x - 1; 
    } 
    if(key =='d' || key == 'D'||keyCode == RIGHT) 
    { 
     m_x = m_x + 1; 
    } 
    if(key =='s' || key == 'S'||keyCode == DOWN) 
    { 
     m_y = m_y + 1; 
    } 

    } 
} 

/************************************************************************* 
************************************************************************* 

Staying Inside the Window, get out right, come out left 
Jannik Indorf 
12.10.2016 

**************************************************************************/ 

void InsideWindow() 
{ 
    if (m_x > width) 
    { 
    m_x = 0; 
    } 
    if (m_x < 0) 
    { 
    m_x = width; 
    } 
    if (m_y > height) 
    { 
    m_y = 0; 
    } 
    if (m_y < 0) 
    { 
    m_y = height; 
    } 


} 
/************************************************************************* 
*************************************************************************/ 

答えて

0

あなたのコードは無限ループに入りません。これは、この行のNullPointerExceptionを打つ:

o_x[j] = random(15, 625); 

あなたがo_x値を与えられたことがありませんので、このNullPointerExceptionを打っています。これに値を与えることでこれを修正できます。

他の多くの変数でも同様の問題が発生します。一度にあなたの全体のスケッチを書こうとしているように見えますが、頭痛の原因になるだけです。代わりに、私は一歩踏み出し、空のスケッチから始めてみることをお勧めします。一度に小さなものを1つだけ追加し、新しい行を追加するたびにコードをテストしてください!

+0

返信いただきありがとうございます! –

+0

編集:もう一度最初からやります。一度に完全に書かれたわけではありませんが、より多くの円と配列を使いたいと思っていたので、ほとんどすべてを変更しなければなりませんでした。 –