2012-03-28 7 views
0

Ich entwickle ein Spiel Mein Code funktioniert gut in der 4.0 Emulator aber nicht auf meinem Samsung Galaxy W mit 2.3.6 Ich habe nicht Code, der nicht mit dem Gerät geeignet ist Version aber wie auch immer auf meinem Gerät nichts bewegt , aber es gibt nicht einmal einen Fehler in logcat oder etw.Android Api Emulator gegen Gerät

MainActivity:

public class ControlsActivity extends Activity implements OnTouchListener{ 

    Button up; 
    Button down; 
    Button left; 
    Button right; 

    String view; 

    static boolean touch=false; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
           WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     setContentView(R.layout.main); 

     up = (Button) findViewById(R.id.up); 
     up.setOnTouchListener(this); 

     down = (Button) findViewById(R.id.down); 
     down.setOnTouchListener(this); 

     left = (Button) findViewById(R.id.left); 
     left.setOnTouchListener(this); 

     right = (Button) findViewById(R.id.right); 
     right.setOnTouchListener(this); 



    } 


    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     /* 
     view = v.toString(); 
     if (ControlsActivity.view == "up" && ControlsActivity.touch == true) 
     { 
      ph--; 
     } 
     if (ControlsActivity.view == "down" && ControlsActivity.touch == true) 
     { 
      ph++; 
     } 
     if (ControlsActivity.view == "left" && ControlsActivity.touch == true) 
     { 
      pw--; 
     } 
     if (ControlsActivity.view == "right" && ControlsActivity.touch == true) 
     { 
      pw++; 
     } 
     */ 
     if(event.toString().contains("action=ACTION_UP")) 
     { 
      touch = false; 
     } 

     if(event.toString().contains("action=ACTION_DOWN")) 
     { 

      touch = true; 
     } 


     return touch; 

    } 



} 

Leinwand:

public class draw extends View { 
    //Canvas ca; 
    View v; 
    Paint paint; 

    int width; 
    int height; 

    static final int MAX_GAME_SPEED=25; 
    static int fps; 



    static int speed=3; 
    static int pw=0; 
    static int ph=0; 





    public draw(Context context, AttributeSet attr){ 
      super(context, attr); 
      Thread myThread = new Thread(new UpdateThread()); 
      myThread.start(); 
    } 


    /* public draw(Context context) { 
      super(context); 
      Thread myThread = new Thread(new UpdateThread()); 
      myThread.start(); 
     } */ 

      @Override 
     protected void onDraw(Canvas c){ 
     super.onDraw(c); 
     paint = new Paint(); //Paint paint = new Paint(); 
     paint.setStyle(Paint.Style.FILL); 


     //get screen size 
     WindowManager wm = (WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE); 
     Display display = wm.getDefaultDisplay(); 


     width = display.getWidth(); // deprecated 
     height = display.getHeight(); // deprecated 







      // make the entire canvas white 
      paint.setColor(Color.WHITE); 
      c.drawPaint(paint); 


      //ca = c; 
      paint.setColor(Color.BLACK); 
      c.drawRect(Math.round(width/3),Math.round(height/3),Math.round((width/3)*2),Math.round((height/3)*2), paint); //position width, position height,width,height 




      paint.setColor(Color.GREEN); 
      c.drawRect(pw,ph, pw+50,ph+50, paint); 

      //pw++; 
      //ph++; 

      /*if(pw >= width) 
      { 
       pw = 0; 
      } 
      if(ph >= height) 
      { 
       ph = 0; 
      } 
      if(pw <= 0) 
      { 
       pw = width; 
      } 
      if(ph <= 0) 
      { 
       ph = height; 
      }*/ 

      if(ControlsActivity.touch == true) 
      { 
       pw=pw+speed; 
       ph=ph+speed; 
      } 
      else 
      { 

      } 


    } 

      public Handler updateHandler = new Handler(){ 
       /** Gets called on every message that is received */ 
       // @Override 
       public void handleMessage(Message msg) { 

        invalidate(); 
        super.handleMessage(msg); 
       } 
      }; 



      public class UpdateThread implements Runnable { 

       @Override 
       public void run() { 
        while(true){ //Game Loop 

         long startTime = System.currentTimeMillis(); 
         draw.this.updateHandler.sendEmptyMessage(0); //veranlassen, dass paint() erneut aufgerufen werden soll 
         //for (int i=0; i<999999; i++); //Bremse 

         Thread.yield();     
         long executionTime = System.currentTimeMillis()-startTime; 

         if (executionTime<MAX_GAME_SPEED){ 
          try { 
           Thread.sleep(MAX_GAME_SPEED-(int)executionTime); 
          } catch (InterruptedException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
          fps=1000/MAX_GAME_SPEED; 
         } else fps=(int) (1000/executionTime); 

        } 

       } 

      } 

    } 

Antwort