2016-11-12 17 views
0

私はアンドロイドスタジオで2dゲームを作る方法についてレトロチキンチュートリアルをオンラインでフォローしていましたが、問題を横断して実行しましたが、解決策を見つけるように思われる。彼のコードを実行した2番目のビデオ(https://www.youtube.com/watch?v=Rliwg0sELJo)に問題が発生し、画面上に四角形が表示され、彼は動き回ることができます。私の場合、何らかの理由でキャンバスに四角形が表示されません。私は彼とまったく同じコードを持っています(私は戻ってチェックしました)。キャンバスにキャンバスを描く[Androidスタジオゲーム]

Cold swapped changes. 
$ adb shell am start -n "com.example.ridhavraj.stardrifter/com.example.ridhavraj.stardrifter.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER 
Connected to process 3319 on device emulator-5554 
W/System: ClassLoader referenced unknown path: /data/app/com.example.ridhavraj.stardrifter-2/lib/x86 
I/InstantRun: Instant Run Runtime started. Android package is com.example.ridhavraj.stardrifter, real application class is null. 
W/System: ClassLoader referenced unknown path: /data/app/com.example.ridhavraj.stardrifter-2/lib/x86 
I/OpenGLRenderer: Initialized EGL, version 1.4 
D/OpenGLRenderer: Swap behavior 1 

        [ 11-11 20:20:52.858 3319: 3343 D/   ] 
        HostConnection::get() New Host Connection established 0xaee13300, tid 3343 
A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 3343 (Thread-5) 

     [ 11-11 20:20:53.138 1214: 1214 W/   ] 
     debuggerd: handling request: pid=3319 uid=10072 gid=10072 tid=3343 
Application terminated. 

ここで私はビデオから作られているコードは次のとおりです。回私のアプリケーションで

は、時には(私は最新のSDKのバージョンとすべてのものを持っている)を起動し、私にこのようなエラーが発生しますがdoesntの

[ゲームオブジェクトインターフェイス]

package com.example.ridhavraj.stardrifter; 

import android.graphics.Canvas; 

public interface GameObject { 
    public void draw(Canvas canvas); 
    public void update(); 
} 

[MainActivityクラス】

package com.example.ridhavraj.stardrifter; 

import android.os.Bundle; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.pm.ActivityInfo; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class MainActivity extends Activity { 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.activity_main); 
     //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(new GamePanel(this)); 
    } 

} 

[MainThreadクラス]

package com.example.ridhavraj.stardrifter; 

import android.graphics.Canvas; 
import android.provider.Settings; 
import android.view.SurfaceHolder; 

public class MainThread extends Thread{ 

    public static final int MAX_FPS = 30; 
    private double averageFPS; 
    private SurfaceHolder surfaceHolder; 
    private GamePanel gamePanel; 
    private boolean running; 
    public static Canvas canvas; 

    public void setRunning(boolean running) 
    { 
     this.running = running; 
    } 

    public MainThread(SurfaceHolder surfaceHolder, GamePanel gamePanel) 
    { 
     super(); 
     this.surfaceHolder = surfaceHolder; 
     this.gamePanel = gamePanel; 
    } 

    @Override 
    public void run() 
    { 
     long startTime; 
     long timeMillis = 1000/MAX_FPS; 
     long waitTime; 
     int frameCount = 0; 
     long totalTime = 0; 
     long targetTime = 1000/MAX_FPS; 

     while(running) 
     { 
      startTime = System.nanoTime(); 
      canvas = null; 

      try 
      { 
       canvas = this.surfaceHolder.lockCanvas(); 
       synchronized (surfaceHolder) 
       { 
        this.gamePanel.update(); 
        this.gamePanel.draw(canvas); 
       } 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
      finally 
      { 
       if (canvas != null) 
       { 
        try 
        { 
         surfaceHolder.unlockCanvasAndPost(canvas); 
        } 
        catch (Exception e) 
        { 
         e.printStackTrace(); 
        } 
       } 

      } 

      timeMillis = (System.nanoTime()-startTime)/1000000; 
      waitTime = targetTime - timeMillis; 

      try 
      { 
       if (waitTime > 0) 
       { 
        this.sleep(waitTime); 
       } 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 

      totalTime += System.nanoTime() - startTime; 
      frameCount++; 
      if (frameCount == MAX_FPS) 
      { 
       averageFPS = 1000/((totalTime/frameCount)/1000000); 
       frameCount = 0; 
       totalTime = 0; 
       System.out.println(averageFPS); 
      } 
     } 
    } 

} 

[GamePanelのクラス]

package com.example.ridhavraj.stardrifter; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Point; 
import android.graphics.Rect; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback { 

    private MainThread thread; 

    private Player player; 
    private Point playerPoint; 

    public GamePanel(Context context) 
    { 
     super(context); 

     getHolder().addCallback(this); 

     thread = new MainThread(getHolder(), this); 

     player = new Player(new Rect(100,100,200,200), Color.rgb(255,0,0)); 
     playerPoint = new Point(150,150); 

     setFocusable(true); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder Holder, int format, int width, int height) 
    { 

    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) 
    { 
     thread = new MainThread(getHolder(), this); 
     thread.setRunning(true); 
     thread.start(); 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) 
    { 
     boolean retry = true; 
     while(true) 
     { 
      try 
      { 
       thread.setRunning(false); 
       thread.join(); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
      retry = false; 
     } 
    } 

    public boolean onTouchEvent(MotionEvent event) 
    { 
     switch (event.getAction()) 
     { 
      case MotionEvent.ACTION_DOWN: 
      case MotionEvent.ACTION_MOVE: 
       playerPoint.set((int)event.getX(), (int)event.getY()); 
     } 
     return true; 
     //return super.onTouchEvent(event); 
    } 

    public void update() 
    { 
     player.update(playerPoint); 
    } 

    @Override 
    public void draw(Canvas canvas) 
    { 
     super.draw(canvas); 

     canvas.drawColor(Color.WHITE); 
     player.draw(canvas); 
    } 
} 

[プレーヤークラス]

package com.example.ridhavraj.stardrifter; 

import android.graphics.Paint; 
import android.graphics.Point; 
import android.graphics.Canvas; 
import android.graphics.Rect; 

public class Player implements GameObject{ 

    private Rect rectangle; 
    private int color; 

    public Player(Rect rectangle, int Color) 
    { 
     this.rectangle = rectangle; 
     this.color = color; 
    } 

    @Override 
    public void draw(Canvas canvas) 
    { 
     Paint paint = new Paint(); 
     paint.setColor(color); 
     canvas.drawRect(rectangle, paint); 
    } 

    @Override 
    public void update() 
    { 

    } 

    public void update(Point point) 
    { 
     //l,t,r,b 
     rectangle.set(point.x-rectangle.width()/2, point.y-rectangle.height()/2, point.x+rectangle.width()/2, point.y+rectangle.height()/2); 

    } 
} 

私は私が間違っているの何の手がかりをいけない、任意のヘルプは次のようになります大いに感謝します。

答えて

0

まあ、私は選手クラス

民間のRect矩形にタイプミスがあった、遅滞午前、男が判明します。 プライベートint色;

public Player(Rect rectangle, int Color [THIS SHOULD BE 'color' and not 'Color') 
{ 
    this.rectangle = rectangle; 
    this.color = color; 
} 
関連する問題