2016-09-28 10 views
0

これで衝突検知に関する別のアクティビティを開始する必要があるので、私はこのシンプルなゲームを作ろうとする初心者ですが、これを理解できません。私はそれを開始する別のアクティビティを開始しようとしている場所は、現在のコードは、私が活動衝突検出に関する別のアクティビティを開始する

package fi.itsn.jetfighter; 

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

import java.util.ArrayList; 

import static android.support.v4.app.ActivityCompat.startActivity; 

/** 
* Created by h on 21.9.2016. 
*/ 

public class GameView extends SurfaceView implements Runnable { 

    volatile boolean playing; 
    private Thread gameThread = null; 
    private Player player; 

    private Paint paint; 
    private Canvas canvas; 
    private SurfaceHolder surfaceHolder; 

    private Enemy[] enemies; 
    private Broccoli[] broccolis; 

    private int enemyCount = 3; 
    private int broccoliCount = 1; 

    private ArrayList<Star> stars = new 
      ArrayList<Star>(); 

    //defining a boom object to display blast 
    private Boom boom; 

    public GameView(Context context, int screenX, int screenY) { 
     super(context); 

     player = new Player(context, screenX, screenY); 

     surfaceHolder = getHolder(); 
     paint = new Paint(); 

     int starNums = 100; 
     for (int i = 0; i < starNums; i++) { 
      Star s = new Star(screenX, screenY); 
      stars.add(s); 
     } 

     enemies = new Enemy[enemyCount]; 
     for (int i = 0; i < enemyCount; i++) { 
      enemies[i] = new Enemy(context, screenX, screenY); 
     } 

     //initializing boom object 
     boom = new Boom(context); 

     broccolis = new Broccoli[broccoliCount]; 
     for (int i = 0; i < broccoliCount; i++) { 
      broccolis[i] = new Broccoli(context, screenX, screenY); 
     } 
    } 

    @Override 
    public void run() { 
     while (playing) { 
      update(); 
      draw(); 
      control(); 
     } 
    } 

    private void update() { 

     player.update(); 

     //setting boom outside the screen 
     boom.setX(-250); 
     boom.setY(-250); 

     for (Star s : stars) { 
      s.update(player.getSpeed()); 
     } 

     for (int i = 0; i < enemyCount; i++) { 
      enemies[i].update(player.getSpeed()); 

      //if collision occurrs with player 
      if (Rect.intersects(player.getDetectCollision(), enemies[i].getDetectCollision())) { 

       //displaying boom at that location 
       boom.setX(enemies[i].getX()); 
       boom.setY(enemies[i].getY()); 

       enemies[i].setX(-200); 

      } 
     } 

     for (int i = 0; i < broccoliCount; i++) { 
      broccolis[i].update(player.getSpeed()); 

      if (Rect.intersects(player.getDetectCollision(), broccolis[i].getDetectCollision())) { 

       startActivity(new Intent(this, end.class)); 
//This is the place I am trying to start another activity 
      } 
     } 
    } 




    private void draw() { 
     if (surfaceHolder.getSurface().isValid()) { 
      canvas = surfaceHolder.lockCanvas(); 
      canvas.drawColor(Color.BLUE); 

      paint.setColor(Color.WHITE); 

      for (Star s : stars) { 
       paint.setStrokeWidth(s.getStarWidth()); 
       canvas.drawPoint(s.getX(), s.getY(), paint); 
      } 

      canvas.drawBitmap(
        player.getBitmap(), 
        player.getX(), 
        player.getY(), 
        paint); 

      for (int i = 0; i < enemyCount; i++) { 
       canvas.drawBitmap(
         enemies[i].getBitmap(), 
         enemies[i].getX(), 
         enemies[i].getY(), 
         paint 
       ); 
      } 

      for (int i = 0; i < broccoliCount; i++) { 
       canvas.drawBitmap(
         broccolis[i].getBitmap(), 
         broccolis[i].getX(), 
         broccolis[i].getY(), 
         paint 
       ); 
      } 

      //drawing boom image 
      canvas.drawBitmap(
        boom.getBitmap(), 
        boom.getX(), 
        boom.getY(), 
        paint 
      ); 


      surfaceHolder.unlockCanvasAndPost(canvas); 

     } 
    } 

    private void control() { 
     try { 
      gameThread.sleep(17); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void pause() { 
     playing = false; 
     try { 
      gameThread.join(); 
     } catch (InterruptedException e) { 
     } 
    } 

    public void resume() { 
     playing = true; 
     gameThread = new Thread(this); 
     gameThread.start(); 
    } 


    @Override 
    public boolean onTouchEvent(MotionEvent motionEvent) { 
     switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { 
      case MotionEvent.ACTION_UP: 
       player.stopBoosting(); 
       break; 
      case MotionEvent.ACTION_DOWN: 
       player.setBoosting(); 
       break; 
     } 
     return true; 
    } 

} 

を開始する必要があり、ここではブロッコリーです

GameView.javaファイルをエラーいます.javaファイル

package fi.itsn.jetfighter; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Rect; 

import java.util.Random; 

/** 
* Created by h on 22.9.2016. 
*/ 

public class Broccoli { 

    //bitmap for the enemy 
    //we have already pasted the bitmap in the drawable folder 
    private Bitmap bitmap; 
    private int x; 
    private int y; 
    private int speed = 1; 

    private int maxX; 
    private int minX; 

    private int maxY; 
    private int minY; 

    //creating a rect object 
    private Rect detectCollision; 

    public Broccoli(Context context, int screenX, int screenY) { 
     bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.broccoli); 
     maxX = screenX; 
     maxY = screenY; 
     minX = 0; 
     minY = 0; 
     Random generator = new Random(); 
     speed = generator.nextInt(6) + 10; 
     x = screenX; 
     y = generator.nextInt(maxY) - bitmap.getHeight(); 

     //initializing rect object 
     detectCollision = new Rect(x, y, bitmap.getWidth(), bitmap.getHeight()); 
    } 

    public void update(int playerSpeed) { 
     x -= playerSpeed; 
     x -= speed; 
     if (x < minX - bitmap.getWidth()) { 
      Random generator = new Random(); 
      speed = generator.nextInt(10) + 10; 
      x = maxX; 
      y = generator.nextInt(maxY) - bitmap.getHeight(); 
     } 

     //Adding the top, left, bottom and right to the rect object 
     detectCollision.left = x; 
     detectCollision.top = y; 
     detectCollision.right = x + bitmap.getWidth(); 
     detectCollision.bottom = y + bitmap.getHeight(); 
    } 

    //adding a setter to x coordinate so that we can change it after collision 
    public void setX(int x){ 
     this.x = x; 
    } 

    //one more getter for getting the rect object 
    public Rect getDetectCollision() { 
     return detectCollision; 
    } 

    //getters 
    public Bitmap getBitmap() { 
     return bitmap; 
    } 

    public int getX() { 
     return x; 
    } 

    public int getY() { 
     return y; 
    } 

    public int getSpeed() { 
     return speed; 
    } 


} 
+0

「CURRでこのmContextを渡します私はエラーが発生します " - それなら、何か問題があるはずです。 –

答えて

0

はあなたがコンテキストではないthisを渡す必要があり

startActivity(new Intent(getContext(), end.class)); 

で試してみてください。あなたは、コンストラクタにコンテキストを渡しているので、それはあなたがまた

public GameView(Context context, int screenX, int screenY) {{ 
    super(context); 
    this.mContext = context; 
} 
のようにそれを行うことができますので Activity

OR

になりますようにここでは、SurfaceViewでクラスを拡張していることはありませんActivityのでthisはあなたにコンテキストを与えることはありません

Intent

startActivity(new Intent(mContext, end.class)); 
+0

これで私にエラーが発生し、メソッド "startActivity"を作成することを提案します。ここで何をすべきですか? – Henyl95