I am building a snake game, and it works, but with a little bug that really irritates me.
when I click on one of the buttons that in the xml, the snake changes it's direction according to the button.
the problem is that if you have clicked on a button after it's has already re-entered the loop inside the run() of the thread and entered the loop inside the the onDraw() (you can see in the code) it will first finish the loop of the run() and then re-enter and do the turn. and by that a delay is created from the click time to when it turns.
the turn works like this- there is a String variable that gets "LEFT","RIGHT", "UP" or "DOWN", according to what it gets it moves the snake. when you click on a button it changes the variable accoring to what button u have clicked on.
heres the code of how it moves the snake accoring to the direction:
public void onDraw(Canvas canvas){
switch(direction){
case "RIGHT"
xLoc=xLoc+PART_SIZE;
break;
case "UP"
yLoc=yLoc-PART_SIZE;
break;
case "LEFT"
xLoc=xLoc-PART_SIZE;
break;
case "DOWN"
yLoc=yLoc+PART_SIZE;
break;
}//swich
int x=xLoc;
int y=yLoc;
for(BodyPart bp:WholeBody){
Rect r=new Rect();
r.set(0+x, 0+y, PART_SIZE+x, PART_SIZE+y);
bp.setR(r);
canvas.drawRect(bp.getR(),bp.getP());
int xtemp=bp.getX();
int ytemp=bp.getY();
bp.setX(x);
bp.setY(y);
x=xtemp;
y=ytemp;
}//for each
}//onDraw
the xLoc and the yLoc is the location of where the head of the snake will go to, and the its moves each body part(each rect of the snake-each bodypart is an equall rect) the the location of where the bodypart before it was.
here are the onClick()s:
left.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(gv.snake.getDir()!="LEFT"&&gv.snake.getDir()!="RIGHT"){
gv.snake.Left();
}
}
});
right.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(gv.snake.getDir()!="RIGHT"&&gv.snake.getDir()!="LEFT"){
gv.snake.Right();
}
}
});
up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(gv.snake.getDir()!="UP"&&gv.snake.getDir()!="DOWN"){
gv.snake.Up();
}
}
});
down.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(gv.snake.getDir()!="DOWN"&&gv.snake.getDir()!="UP"){
gv.snake.Down();
}
}
});
gv is an variable from class gameView which in it has a variable from the class snake
here are the setDirection:
public void Left(){
direction="LEFT";
}
public void Up(){
direction="UP";
}
public void Right(){
direction="RIGHT";
}
public void Down(){
direction="DOWN";
}
and here is the run of the thread:
public void run() {
long stepPerSecond=1000/FPS;
long startTime;
long sleepTime;
while(isRunning){
Canvas c= null;
startTime=System.currentTimeMillis();
try{
c=this.getHolder().lockCanvas();
synchronized (this.getHolder()) {
this.onDraw(c);
}
}
catch(Exception e){
}
finally{
if(c!=null){
this.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime= stepPerSecond-(System.currentTimeMillis()-startTime);
if(sleepTime>0)
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
else
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
is there someway that there will not be that delay?
maby when you click on a button it will stop the run() and redo it after it had updated the direction variable acording to the click.
Related
I need to understand on how to detect if user has touched the screen.
Expected Result:-Whenever user touches screen it should skip Splash Screen and move to main activity.
Problem:-Whenever user touches the screen Splash Screen is skipped but in the background sleep(10500) in try block keeps running and as it elapses the Main Activity starts again i.e. it opens two times.
What have I done so far:-I tried do while loop and gave a condition,if the condition is satisfied(of Touch) then break.But I don't seem to get the correct working condition.
Splash Screen Code:-
#Override
protected void onCreate(Bundle splashState) {
// TODO Auto-generated method stub
super.onCreate(splashState);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread() {
public void run() {
do
{
try {
//if(onTouchEvent(null))
// break;
sleep(10500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
startActivity(new Intent("com.first.MAINACTIVITY"));
}
}while(false);
}
};
timer.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
startActivity(new Intent("com.first.MAINACTIVITY"));
finish();
ourSong.release();
}
return super.onTouchEvent(event);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
ourSong.release();
}
If Statement is provided in the try block so if condition is satisfied it would break.But the condition is unknown to me.Need help with the condition.
Thanks.
private boolean isSplashRunning = true;
#Override
protected void onCreate(Bundle splashState) {
// TODO Auto-generated method stub
super.onCreate(splashState);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread() {
public void run() {
do
{
try {
//if(onTouchEvent(null))
// break;
sleep(10500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if(isSplashRunning)
startActivity(new Intent("com.first.MAINACTIVITY"));
}
}while(false);
}
};
timer.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isSplashRunning = false; //or in onPause
startActivity(new Intent("com.first.MAINACTIVITY"));
finish();
ourSong.release();
}
return super.onTouchEvent(event);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
isSplashRunning = false;
super.onPause();
finish();
ourSong.release();
}
I have a fragment that has an animation of textView fadein. The animation must start after some time delay say 2 seconds after fragment is loaded. I wrote a code for this. but the animation part is done and then the view is rendered. How can I load the fragment and after some time delay start my animation
My code is as below: Note: the class extends Fragment
Animation animFadein;
MenuClickHelper mClickHelper;
TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_main_menu,
container, false);
mClickHelper = new MenuClickHelper(rootView, getFragmentManager());
tv = (TextView) rootView.findViewById(R.id.tvPresentation);
animFadein = AnimationUtils.loadAnimation(getActivity()
.getApplicationContext(), R.anim.fade_in);
animFadein.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
if (animation == animFadein) {
Toast.makeText(getActivity().getApplicationContext(),
"Animation Stopped", Toast.LENGTH_SHORT).show();
}
}
});
try {
Thread.sleep(2000);
tv.startAnimation(animFadein);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rootView;
}
Thread.sleep(2000); dont block your main using the sleep method instead you can use the Handler class and use postdelay to delay the animation:
sample:
change this:
try {
Thread.sleep(2000);
tv.startAnimation(animFadein);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
to
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
tv.startAnimation(animFadein);
}
}, 2000); //will start animation in 2 seconds
You can achieve the same result with
android:startOffset="2000"
in your XML animation set file.
I am building a snake game app.
So fur it works, except for the what it shows.
For some reason it shows two snakes: the original snake that moves and can be controlled, and another begginging of a snake that doesn't do anything.
Before that I drew at every cycle in the loop a background color, so it would cover the last canvas, and there was no problem.
Then I decided to divide the snake from the background, so I have made the snake canvas's background transparent and that it would clear the canvas each time it wants to draw a new canvas (each loop-to move the snake).
Here is the code:
The run of the thread:
public void run() {
long stepPerSecond=1000/FPS;
long startTime;
long sleepTime;
while(isRunning){
Canvas c=null;
startTime=System.currentTimeMillis();
try{
c=this.getHolder().lockCanvas();
synchronized (this.getHolder()) {
this.onDraw(c);
}
}
catch(Exception e){
}
finally{
if(c!=null){
this.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime= stepPerSecond-(System.currentTimeMillis()-startTime);
if(sleepTime>0)
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The snake's ondraw (it uses the snake's class on draw):
public void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(color.transparent, Mode.CLEAR);
snake.onDraw(canvas);
}
The snake's class onDraw:
public void onDraw(Canvas canvas){
switch(dirTransfare(dir)){
case 1://RIGHT
xLoc=xLoc+PART_SIZE;
break;
case 2://UP
yLoc=yLoc-PART_SIZE;
break;
case -1://LEFT
xLoc=xLoc-PART_SIZE;
break;
case -2://DOWN
yLoc=yLoc+PART_SIZE;
break;
}//swich
int x=xLoc;
int y=yLoc;
for(Brick bp:SnakeBody){
bp.setR(0+x, 0+y, PART_SIZE+x, PART_SIZE+y);
canvas.drawRect(bp.getR(),bp.getP());
int xtemp=bp.getX();
int ytemp=bp.getY();
bp.setX(x);
bp.setY(y);
x=xtemp;
y=ytemp;
}//for each
}//onDraw
Here is the mainActivity, which summons the snakeVew and the backgorundView (snake is the game view and the background is the arena)
public class MainActivity extends Activity {
GameView gv;
ImageView left;
ImageView right;
ImageView up;
ImageView down;
Arena a;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*requestWindowFeature(Window.FEATURE_NO_TITLE);
v= new GameView1(this);
setContentView(v);
*/
gv= new GameView(this);
a=new Arena(this);
setContentView(R.layout.game_view);
LinearLayout surface = new LinearLayout(this);
LinearLayout backGround = new LinearLayout(this);
surface = (LinearLayout)findViewById(R.id.surface);
backGround = (LinearLayout)findViewById(R.id.background);
surface.addView(gv);
backGround.addView(a);
left=(ImageView) findViewById(R.id.left);
right=(ImageView) findViewById(R.id.right);
up=(ImageView) findViewById(R.id.up);
down=(ImageView) findViewById(R.id.down);
left.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(gv.snake.getDir()!="LEFT"&&gv.snake.getDir()!="RIGHT"){
gv.snake.Left();
}
}
});
right.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(gv.snake.getDir()!="RIGHT"&&gv.snake.getDir()!="LEFT"){
gv.snake.Right();
}
}
});
up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(gv.snake.getDir()!="UP"&&gv.snake.getDir()!="DOWN"){
gv.snake.Up();
}
}
});
down.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(gv.snake.getDir()!="DOWN"&&gv.snake.getDir()!="UP"){
gv.snake.Down();
}
}
});
}
The problem is not in the background (the arena class); I have alredy took it down and confirmed that (it worked the same away, with the bug, even without using the arena class).
Here is how it looks with the bug:
http://i.tinyuploads.com/q7oztz.jpg
(you can see two snakes - the long one is the real one)
The blue background is from class arena.
Any ideas? Tnx for help :D
I am new in Android programming, I used to program microcontroller, now I need a few assistance from you guys.
I want to call two function with delay after each function called repeatedly until the stop button pressed.
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//I want perform these series of function repeatedly
//until stop button pressed
while(true){
bluetoothDisconnect();
delay(3000);
bluetoothConnect();
delay(3000);
if(status == true){
break;
}
}
}
});
btnStop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
status = true;
}
});
Big Thanks,
You need to thread the code in btnStart otherwise it will lock the GUI. Sorry to not be more helpful, I write Android apps using Mono (C#), I am unsure how to do it in Java.
Try this:
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
status = false;
Thread t = new Thread(new Runnable() {
#Override
public void run() {
doLoop();
}
});
t.start();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
status = true;
}
});
where
private void doLoop(){
do {
bluetoothDisconnect();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
bluetoothConnect();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (!status);
}
and status is a global variable, FALSE by default (declared as boolean status = false)
This code works as a simple solution, however I'd suggest you to look at the AsyncTask class:
http://developer.android.com/guide/components/processes-and-threads.html
Create an handler to delay the task .
Create the runnable thread and run your handler like:
Handler mhandler=new Handler();
Runnable mRunnable=new Runnable() {
#Override
public void run() {
bluetoothDisconnect();
btnStart.performClick() ;
}
};
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//I want perform these series of function repeatedly
//until stop button pressed
while(true){
bluetoothconnect();
mhandler.postDelayed(mRunnable,3*1000);
if(status == true){
break;
}
}
}
});
I'm playing video via MediPlayer in my android application and have SeekBar displayed. Now I want this seeks bar to automatically update as the video progresses so it should automatically move from left to right. At the moment, (code below) the bar updates and this is done via running thread, that every second updates the progress of seekBar. The problem is it is not smooth and as seekBar is updated via its seekProgress() the video stops for split second and all is very jumpy. Now I would like it to have updated more often then every second as well as keep functionality that I already implemented to allow user to tap on the bar and change progress of the video.
I'm after something like Android MediaPLayer application have, seekBar is on transparent background and all is smooth and I have no idea how it is done.
No, currently as you see from the code below thread updates every second as it sleeps inside f run method. I've also tried to use handlers to update UI thread, effect was the same. I also extended SeekBar to its own class, had thread there and this was no good either, exactly same effect.
If anyone can explain to me how to solve this problem and how its done with other player appls that would be great.
public class FightPlayerActivity extends Activity implements Runnable, OnSeekBarChangeListener, SurfaceHolder.Callback, OnPreparedListener {
private MediaPlayer mp=null;
private SeekBar seekBar;
private Thread progressBarUpdater;
private String filePath;
private Handler handler=new Handler();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Toast.makeText(this,"Create ", 2000).show();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void onStop()
{
super.onStop();
mp.stop();
mp.reset();
mp.release();
}
public void run()
{
while(true)
{
try {
progressBarUpdater.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
seekBar.setProgress(mp.getCurrentPosition());
// handler does have same effect, so video stops for split second
//handler.postDelayed(this, 1000);
}
}
public void onStart()
{
super.onStart();
setContentView(R.layout.fight_player);
filePath=getIntent().getStringExtra("filename");
filePath=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+"/FightAll_BJJ_Scoring/"+filePath;
Toast.makeText(this,filePath, 2000).show();
// seek bar
seekBar=(SeekBar) findViewById(R.id.seek_bar);
seekBar.setOnSeekBarChangeListener(this);
try {
SurfaceView sv=(SurfaceView) findViewById(id.video_preview);
SurfaceHolder sh=sv.getHolder();
sh.addCallback(this);
sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stop(View view)
{
mp.seekTo(0);
mp.pause();
}
public void pause(View view)
{
mp.pause();
}
public void play(View view)
{
mp.start();
}
public void surfaceCreated(SurfaceHolder holder) {
try {
mp=new MediaPlayer();
mp.setDataSource(filePath);
mp.setDisplay(holder);
mp.setOnPreparedListener(this);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
//handler.removeCallbacks(this);
//handler.postDelayed(this, 1000);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void onPrepared(MediaPlayer mediaplayer) {
mp.start();
seekBar.setMax(mp.getDuration());
progressBarUpdater=new Thread(this);
progressBarUpdater.start();
//handler.postDelayed(this, 1000);
}
public void onProgressChanged(SeekBar sb,int progress,boolean fromUser)
{
//Toast.makeText(this, progress, 2000).show();
mp.seekTo(progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
onProgressChanged(seekBar,seekBar.getProgress(),true);
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
Your major problem is in your onProgressChanged() method.
You are seeking to the specified position every time the seekBar progress changes, even when it is done programmatically. Which means that every time you call seekBar.setProgress(mp.getCurrentPosition()), onProgressChanged() will be fired.
So we change it to the following:
public void onProgressChanged(SeekBar sb, int progress, boolean fromUser) {
if (fromUser) {
mp.seekTo(progress);
}
}
That way it will only be fired when the user moves the seekBar.
Moreover, according to this answer, it would be better to replace your while(true) loop with:
public void run() {
seekBar.setProgress(mp.getCurrentPosition());
if (mp.getCurrentPosition() < mp.getDuration()) {
seekBar.postDelayed(this, MILLISECONDS);
}
}