I've been following some tutorials online to try to get a grasp of basic android development, and I seem to be stuck. There are no errors in my code, but a few warnings and whenever I press play from the main menu, it crashes. If anyone could help out that'd be greatly appreciated!!
This is my MainActivity.java
package com.example.shadowassassinstu3;
import com.example.shadowassassinstu3.Game;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void play(View v)
{
Intent i=new Intent(this,Game.class);
startActivity(i);
}
public void exit(View v)
{
System.exit(0);
}
}
This is my Game.java
package com.example.shadowassassinstu3;
import com.example.shadowassassinstu3.gameloop;
import com.example.shadowassassinstu3.Game.GameView;
import com.example.shadowassassinstu3.Game.TeleListener;
import com.example.shadowassassinstu3.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
public class Game extends Activity {
gameloop gameLoopThread;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//phone state
TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
TelephonyMgr.listen(new TeleListener(),PhoneStateListener.LISTEN_CALL_STATE);
//for no title
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GameView(this));
}
public class GameView extends SurfaceView {
Bitmap bmp,pause;
Bitmap background;
Bitmap run1;
Bitmap run2;
Bitmap run3;
Bitmap exit;
private SurfaceHolder holder;
private int x = 0,y=0,z=0,delay=0,getx,gety;
int show=0,sx,sy;
int cspeed=0,kspeed=0,gameover=0;
int score=0,health=100,reset=0;
int pausecount=0,volume,power=0,powerrun=0,shieldrun=0;
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public GameView(Context context)
{
super(context);
gameLoopThread = new gameloop(this);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@SuppressWarnings("deprecation")
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
gameLoopThread.setRunning(false);
gameLoopThread.getThreadGroup().interrupt();
}
@SuppressLint("WrongCall")
@Override
public void surfaceCreated(SurfaceHolder holder)
{
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,int width, int height)
{
}
});
Display display = getWindowManager().getDefaultDisplay();
sx = display.getWidth();
sy = display.getHeight();;
background = BitmapFactory.decodeResource(getResources(), R.drawable.back);
run1=BitmapFactory.decodeResource(getResources(), R.drawable.run1);
run2=BitmapFactory.decodeResource(getResources(), R.drawable.run2);
run3=BitmapFactory.decodeResource(getResources(), R.drawable.run3);
exit=BitmapFactory.decodeResource(getResources(), R.drawable.exit);
pause=BitmapFactory.decodeResource(getResources(), R.drawable.pause);
exit=Bitmap.createScaledBitmap(exit, 25,25, true);
pause=Bitmap.createScaledBitmap(pause, 25,25, true);
run1=Bitmap.createScaledBitmap(run1, sx/9,sy/7, true);
run2=Bitmap.createScaledBitmap(run2, sx/9,sy/7, true);
run3=Bitmap.createScaledBitmap(run3, sx/9,sy/7, true);
background=Bitmap.createScaledBitmap(background, 2*sx,sy, true);
}
// on touch method
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
show=1;
getx=(int) event.getX();
gety=(int) event.getY();
// restart game
if(getx>91&&gety<25)
{
if(health<=0)
{
gameLoopThread.setPause(0);
health=100;
score=0;
}
}
//pause game
if((getx>(sx-25)&&gety<25&&pausecount==0))
{
gameLoopThread.setPause(1);
pausecount=1;
}
else if(getx>(sx-25)&&gety<25&&pausecount==1)
{
gameLoopThread.setPause(0);
pausecount=0;
}
}
return true;
}
@SuppressLint("WrongCall")
@Override
protected void onDraw(Canvas canvas)
{
//background moving
z=z-10;
if(z==-sx)
{
z=0;
canvas.drawBitmap(background, z, 0, null);
}
else
{
canvas.drawBitmap(background, z, 0, null);
}
//running player
x+=5;
if(x==20)
{
x=5;
}
if(show==0)
{
if(x%2==0)
{
canvas.drawBitmap(run3, sx/16, 15*sy/18, null);
}
else
{
canvas.drawBitmap(run1, sx/16, 15*sy/18, null);
}
}
canvas.drawBitmap(exit, 0, 0, null);
canvas.drawBitmap(pause, (sx-25), 0, null);
}
}
//phone state
public class TeleListener extends PhoneStateListener
{
public void onCallStateChanged(int state,String incomingNumber)
{
if(state==TelephonyManager.CALL_STATE_RINGING)
{
System.exit(0);
}
}
}
}
and this is my gameloop.java
package com.example.shadowassassinstu3;
import com.example.shadowassassinstu3.Game.GameView;
import android.annotation.SuppressLint;
import android.graphics.Canvas;
public class gameloop extends Thread {
private GameView view;
static final long FPS = 10;
private boolean running = false;
boolean isPaused;
public gameloop(GameView view) {
this.view = view;
}
public void setRunning(boolean run) {
running = run;
}
public void setPause(int i)
{
synchronized (view.getHolder())
{
if(i==0)
{
isPaused=false;
}
if(i==1)
{
isPaused = true;
}
}
}
public void run() {
long ticksPS = 100;
long startTime = 0;
long sleepTime;
while (running) {
//pause and resume
if (isPaused)
{
try
{
this.sleep(50);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
else
{
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder())
{
view.onDraw(c);
}
}
finally
{
if (c != null)
{
view.getHolder().unlockCanvasAndPost(c);
}
}
}
sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
}
catch (Exception e) {}
}
}
}
A lot of the code I've gotten through watching tutorials and I really can't figure out what's wrong, if anyone could help I'd really appreciate it!
Aucun commentaire:
Enregistrer un commentaire