2016-05-12 11 views
0

Die folgende Methodeöffentlichen classname() wird nie in der Klasse classname genannt - Java

öffentlichen JSound() {

}

nie für diesen Code aufgerufen wird? Gründe warum?

Wenn ich es auf diese Weise nicht funktionieren kann, gibt es eine zweite Möglichkeit, die ich Maus-Listener verwenden kann? Es brauchte eine nicht statische Methode, daher war ich mir nicht sicher, wie ich das erreichen könnte.

machte ich die Zugabe von

new JSound(); 

und

JSound JS = new JSound(); 

aber weder Arbeit?

package Sound; 

import java.awt.Component; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

import javax.swing.JFrame; 

public class JSound extends JFrame implements MouseListener { 

//IDK what its for but its important for jframe 
private static final long serialVersionUID = 3314065768834871224L; 

//Gets General volume 
public static double volume = Audio.getMasterOutputVolume()*3*100; 

//Universal variables are static, and i usually put them here 
public static boolean running = true; 
public static String title = "Advanced Java Sound"; 
public static int width = 410; 
public static int height = 600; 

public static int ticks = 1; 

//class setups 
public JSound sound; 

public JSound() { 
    //This never gets called for some reason 

    //initialises mouse input 
    System.out.println("apples"); 
    setTitle("Simple Frame"); 
    addMouseListener(this); 
} 

public static void main(String args[]) { 
    //Creates the display basicly just the empty window you will see all the stuff drawn to 
    Display.Window(); 
    //Calls the main loop method 
    mainloop(); 
    //SoundLoad(); 
    //addMouseListener(sound); 
} 

public static void mainloop() { 
    render.quickrender(); 

    try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } 

    while(running) { 
     long lastTime = System.nanoTime(); 
     double amountOfTicks = 60.0; 
     double ns = 1000000000/amountOfTicks; 
     double delta = 0; 
     long timer = System.currentTimeMillis(); 
     int frames = 0; 
     while (running) { 
      long now = System.nanoTime(); 
      delta += (now - lastTime)/ns; 
      lastTime = now; 
      while (delta >= 1) { 
       ticks++; 
       delta--; 
      } 
      if (running) 
       tick(); 
       render.renderer(); 
      frames++; 

      if(System.currentTimeMillis() - timer > 1000) { 
       timer += 1000; 
       System.out.println("FPS: " + frames + " Ticks: " + ticks); 
       frames = 0; 
      } 
     } 
    } 
} 

public static void tick() { 
    //Put any tick method stuff here, it will be executed in priority to render, and will occur more then 60 times per second 

    //Audio.setMasterOutputVolume(0.5f); 
    volume = Audio.getMasterOutputVolume()*4.8*100; 
    //System.out.println(Audio.getMasterOutputVolume()); 
    //System.out.println((int)volume); 
} 

public void mouseClicked(MouseEvent e) { 
    System.out.println(e.getX()); 
    System.out.println(e.getY()); 
} 

public void mouseEntered(MouseEvent e) { 

} 

public void mouseExited(MouseEvent e) { 

} 

@Override 
public void mousePressed(MouseEvent e) { 
    System.out.println(e.getX()); 
    System.out.println(e.getY()); 
} 

public void mouseReleased(MouseEvent e) { 

} 

}

+1

Das ist der Konstruktor. Du musst ihn woanders anrufen. – Seth

+0

Alles, was nach 'mainLoop' aufgerufen wird, wird nie ausgeführt (bis die Ausführung falsch ist) – MadProgrammer

Antwort

1
import javax.swing.JFrame; 

class JSound extends JFrame { 
    //Universal variables are static, and i usually put them here 
    public static boolean running = true; 
    public static String title = "Advanced Java Sound"; 
    public static int width = 410; 
    public static int height = 600; 

    public static int ticks = 1; 

    //class setups 
    public JSound sound; 

    public JSound() { 
     //This never gets called for some reason 

     //initialises mouse input 
     System.out.println("apples"); 
     setTitle("Simple Frame"); 
    } 

    public static void main(String args[]) { 
     System.out.println("I'm before main loop"); 
     new JSound(); 
     mainloop(); 
     System.out.println("I'm after main loop"); 
     new JSound(); 
    } 

    public static void mainloop() { 

     try { 
      Thread.sleep(10); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 

     while (running) { 
      long lastTime = System.nanoTime(); 
      double amountOfTicks = 60.0; 
      double ns = 1000000000/amountOfTicks; 
      double delta = 0; 
      long timer = System.currentTimeMillis(); 
      int frames = 0; 
      while (running) { 
       long now = System.nanoTime(); 
       delta += (now - lastTime)/ns; 
       lastTime = now; 
       while (delta >= 1) { 
        ticks++; 
        delta--; 
       } 
       if (running) { 
        tick(); 
       } 
       frames++; 

       if (System.currentTimeMillis() - timer > 1000) { 
        timer += 1000; 
        System.out.println("FPS: " + frames + " Ticks: " + ticks); 
        frames = 0; 
       } 
      } 
     } 
    } 

    public static void tick() { 

    } 
} 

Zunächst einmal ruft Ihre vorherigen Code nie JSound, zweite von allen alles wird AFTER mainLoop genannt nie ausgeführt werden, bis runningfalse ist, so kann ich nur annehmen, dass JSound wird einige Zeit genannt bekommen, nachdem Sie mainLoop nennen

+0

Dieser funktioniert, Danke – CGameing

+0

@CGameing - aber verstehst du warum ???? –

+0

Ja, ich lege es nach meiner Hauptschleife – CGameing

1

Der JSound nie für diesen Code aufgerufen wird? Irgendwelche Gründe warum?

Erstens ist es keine Methode. Es ist ein Konstruktor.

Der Grund, warum es nie aufgerufen wird, ist, dass nirgendwo der Code haben Sie den Ausdruck new JSound(). Warum? Ich weiß es nicht. Fragen Sie die Person, die den Code geschrieben hat!

+0

Ich habe einen neuen JSound(); in meiner Hauptmethode? Funktioniert immer noch nicht? – CGameing

0

Es wird nie aufgerufen, weil Sie nie den JSound-Konstruktor aufrufen. Ich nehme an, Sie möchten dies in der Hauptmethode vor der Hauptschleife tun.