Vote count:
0
I just want to ask if how can I decrement the life of my character each time he touches with TAYA except when he touches the coins? Touching the coins can gain points while touching with the other object which is the TAYA in my declaration should decrease the character's life.
Relevant Codes Regarding My Question:
//running player
x+=5;
if(x==20)
{
x=5;
}
if(show==0)
{
if(x%2==0)
{
int height = run6.getHeight();
canvas.drawBitmap(run6, sx/16, (15*sy/18)-height / 2, null);
}
else
{
int height = run1.getHeight();
canvas.drawBitmap(run1, sx/16, (15*sy/18) - height / 2, null);
}
//kinfe hit
if(kspeed==20)
{
kspeed=sx;
health-=25;
canvas.drawBitmap(note1, 0, 0, null);
}
}
//kinfe
kspeed=kspeed-20;
int height1 = taya.getHeight();
canvas.drawBitmap(taya, kspeed, (15*sy/18) - height1 / 2, null);
if(kspeed<50)
{
kspeed=sx;
}
// for jump
if(show==1)
{
if(sound==1)
{
jump.start();
}
int height2 = jump2.getHeight();
canvas.drawBitmap(jump2, sx/16, (3*sy/6) - height2 / 2, null);
//score
if(cspeed<=sx/8&&cspeed>=sx/16)
{
if(sound==1)
{
takecoin.start();
}
cspeed=sx/2;
score+=10;
}
// jump-hold
delay+=1;
if(delay==3)
{
show=0;
delay=0;
}
}
//for coins
cspeed=cspeed-5;
if(cspeed==-sx/2)
{
cspeed=sx/2;
canvas.drawBitmap(coin, cspeed, 3*sy/4, null);
}
else
{
canvas.drawBitmap(coin, cspeed, 3*sy/4, null);
}
Whole Code of my Class:
public class LunetaPark1 extends Activity {
MediaPlayer mp1,jump,takecoin;
GameLoopThread3 gameLoopThread;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 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,taya,note1,note2;
Bitmap run1;
Bitmap run6;
Bitmap jump2;
Bitmap coin;
Bitmap exit;
private SurfaceHolder holder;
private int x = 0,y=0,z=0,delay=0,getx,gety,sound=1;
int show=0,sx,sy;
int cspeed=0,kspeed=0,gameover=0;
int score=0,health=25,reset=0;
int pausecount=0,volume,power=0,powerrun=0,shieldrun=0;
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public GameView(Context context)
{
super(context);
gameLoopThread = new GameLoopThread3(this);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@SuppressWarnings("deprecation")
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
//for stoping the game
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)
{
}
});
//getting the screen size
Display display = getWindowManager().getDefaultDisplay();
sx = display.getWidth();
sy = display.getHeight();;
cspeed=sx/2;
kspeed=sx/2;
powerrun=(3*sx/4);
shieldrun=sx/8;
background = BitmapFactory.decodeResource(getResources(),R.drawable.imgluneta);
run1 = BitmapFactory.decodeResource(getResources(), R.drawable.run1);
run6 = BitmapFactory.decodeResource(getResources(), R.drawable.run6);
jump2 = BitmapFactory.decodeResource(getResources(),R.drawable.run11);
coin = BitmapFactory.decodeResource(getResources(), R.drawable.coin);
exit = BitmapFactory.decodeResource(getResources(), R.drawable.exit);
taya = BitmapFactory.decodeResource(getResources(), R.drawable.taya);
note1 = BitmapFactory.decodeResource(getResources(),R.drawable.note1);
pause = BitmapFactory.decodeResource(getResources(), R.drawable.pause);
exit=Bitmap.createScaledBitmap(exit, 25,25, true);
pause=Bitmap.createScaledBitmap(pause, 25,25, true);
coin=Bitmap.createScaledBitmap(coin, sx/16,sy/24, true);
background=Bitmap.createScaledBitmap(background, 2*sx,sy, true);
//health dec
note1=Bitmap.createScaledBitmap(note1, sx,sy, true);
mp1=MediaPlayer.create(LunetaPark1.this,R.raw.game);
jump=MediaPlayer.create(LunetaPark1.this,R.raw.jump);
takecoin=MediaPlayer.create(LunetaPark1.this,R.raw.cointake);
}
// 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();
//exit
if(getx<25&&gety<25)
{
//high score
SharedPreferences pref = getApplicationContext().getSharedPreferences("higher", MODE_PRIVATE);
Editor editor = pref.edit();
editor.putInt("score", score);
editor.commit();
System.exit(0);
}
// 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);
mp1.stop();
pausecount=1;
}
else if(getx>(sx-25)&&gety<25&&pausecount==1)
{
gameLoopThread.setPause(0);
mp1.start();
pausecount=0;
}
}
return true;
}
@SuppressLint("WrongCall")
@Override
protected void onDraw(Canvas canvas)
{
//volume
SharedPreferences pref = getApplicationContext().getSharedPreferences("higher", MODE_PRIVATE);
Editor editor = pref.edit();
volume=pref.getInt("vloume", 0);
if(volume==0)
{
sound=0;
}
canvas.drawColor(Color.BLACK);
//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)
{
int height = run6.getHeight();
canvas.drawBitmap(run6, sx/16, (15*sy/18)-height / 2, null);
}
else
{
int height = run1.getHeight();
canvas.drawBitmap(run1, sx/16, (15*sy/18) - height / 2, null);
}
//kinfe hit
if(kspeed==20)
{
kspeed=sx;
health-=25;
canvas.drawBitmap(note1, 0, 0, null);
}
}
//kinfe
kspeed=kspeed-20;
int height1 = taya.getHeight();
canvas.drawBitmap(taya, kspeed, (15*sy/18) - height1 / 2, null);
if(kspeed<50)
{
kspeed=sx;
}
// for jump
if(show==1)
{
if(sound==1)
{
jump.start();
}
int height2 = jump2.getHeight();
canvas.drawBitmap(jump2, sx/16, (3*sy/6) - height2 / 2, null);
//score
if(cspeed<=sx/8&&cspeed>=sx/16)
{
if(sound==1)
{
takecoin.start();
}
cspeed=sx/2;
score+=10;
}
// jump-hold
delay+=1;
if(delay==3)
{
show=0;
delay=0;
}
}
//for coins
cspeed=cspeed-5;
if(cspeed==-sx/2)
{
cspeed=sx/2;
canvas.drawBitmap(coin, cspeed, 3*sy/4, null);
}
else
{
canvas.drawBitmap(coin, cspeed, 3*sy/4, null);
}
//score
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
paint.setTextSize(15);
paint.setTextAlign(Align.LEFT);
canvas.drawText("Score :"+score, 3*sx/4, 20, paint);
//exit
canvas.drawBitmap(exit, 0, 0, null);
if(sound==1)
{
mp1.start();
mp1.setLooping(true);
}
else
{
mp1.stop();
}
//health
Paint myPaint = new Paint();
myPaint.setColor(Color.RED);
myPaint.setStrokeWidth(10);
myPaint.setAntiAlias(true);
myPaint.setFakeBoldText(true);
canvas.drawText("Health :"+health, 0, (sy/8)-5, myPaint);
canvas.drawRect(0, sy/8, health, sy/8+10, myPaint);
//game over
if(health<=0)
{
gameover=1;
mp1.stop();
//high score
editor.putInt("score", score);
editor.commit();
canvas.drawText("GAMEOVER OVER", sx/2, sy/2, myPaint);
canvas.drawText("YOUR SCORE : "+score, sx/2, sy/4, myPaint);
canvas.drawText("Restart", 91, 25, myPaint);
gameLoopThread.setPause(1);
canvas.drawBitmap(background, sx, sy, null);
}
// restart
if(reset==1)
{
gameLoopThread.setPause(0);
health=100;
score=0;
}
canvas.drawBitmap(pause, (sx-25), 0, null);
}
}
}
Any idea guys? Anyway, the declaration of my health is really 25 and it should decrease by 25 as well. It means, the player has only 1 life.
asked 43 secs ago
The Health/Life of The Game is Not Working
Aucun commentaire:
Enregistrer un commentaire