Ich versuche mehrere Games in eins zu friggeln. Leider kann ich die Rakete nicht bewegen.
Im YT konnte er das Bewegen. Habe den Shoot rausgenommen, sollte trotzdem gehen.
Haupt Klasse:
Wie kann ich die Rakete nun bewegen mit dem Move Event?
Im YT konnte er das Bewegen. Habe den Shoot rausgenommen, sollte trotzdem gehen.
Haupt Klasse:
Java:
package ;
import static java.security.AccessController.getContext;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Handler;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import java.util.Random;
public class SpaceShooter extends View {
Context context;
Bitmap background;
Handler handler;
long UPDATE_MILLIS = 30;
static int screenWidth, screenHeight;
int points = 0;
int keys = 0;
int life = 1;
Paint schorePaint;
int TEXT_SIZE = 80;
boolean paused = false;
OurSpaceship ourSpaceship;
EnemySpaceship enemySpaceship;
Random random;
boolean enemyShotAction = false;
final Runnable runnable = new Runnable() {
@Override
public void run() {
invalidate();
}
};
public SpaceShooter(Context context){
super(context);
this.context = context;
random = new Random();
Display display = ((Activity)getContext()).getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
ourSpaceship = new OurSpaceship(context);
enemySpaceship = new EnemySpaceship(context);
background = BitmapFactory.decodeResource(context.getResources(), R.drawable.space_background);
schorePaint = new Paint();
schorePaint.setTextSize(TEXT_SIZE);
schorePaint.setTextAlign(Paint.Align.LEFT);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(background, 0, 0, null);
canvas.drawText("Point: " + points, 0, TEXT_SIZE, schorePaint);
/*
if(life == 0){
paused = true;
handler = null;
Intent intent = new Intent(context, GameOver.class);
intent.putExtra("points", points);
intent.putExtra("keys", keys);
context.startActivity(intent);
((Activity)context).finish();
}
*/
if (ourSpaceship.isAlive = true){
if(ourSpaceship.ox > screenWidth - ourSpaceship.getOurSpaceshipWidth()){
ourSpaceship.ox = screenWidth - ourSpaceship.getOurSpaceshipWidth();
}else if(ourSpaceship.ox < 0){
ourSpaceship.ox = 0;
}
canvas.drawBitmap(ourSpaceship.getOurSpaceship(), ourSpaceship.ox, ourSpaceship.oy, null);
}
}
public boolean onTouchEvent(MotionEvent event){
int touchX = (int)event.getX();
if(event.getAction() == MotionEvent.ACTION_UP){
ourSpaceship.ox = touchX;
}
if(event.getAction() == MotionEvent.ACTION_MOVE ){
ourSpaceship.ox = touchX;
}
return true;
}
}
Java:
package ;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.util.Random;
public class OurSpaceship {
Context context;
Bitmap ourSpaceship;
int ox, oy;
boolean isAlive = true;
int ourVelocity;
Random random;
public OurSpaceship(Context context){
this.context = context;
ourSpaceship = BitmapFactory.decodeResource(context.getResources(), R.drawable.rakete);
random = new Random();
resetOurSpaceship();
}
public Bitmap getOurSpaceship(){
return ourSpaceship;
}
int getOurSpaceshipWidth(){
return ourSpaceship.getWidth();
}
private void resetOurSpaceship() {
ox = random.nextInt(SpaceShooter.screenWidth);
oy = SpaceShooter.screenHeight - ourSpaceship.getHeight();
ourVelocity = 10 + random.nextInt(6);
}
}
Wie kann ich die Rakete nun bewegen mit dem Move Event?