yashsingh1/xdaforums huggingface dataset:
[Java for Android App Development] Wampirate:
characterGraphic=BitmapFactory.decodeResource(getResources(),R.drawable.sprite); // this is character sprite loading from drawable folder in res
charsrc = new Rect(62,62,124,124);
chardst = new Rect(100,charY-46,193,charY+45); // these are character source and destrination rectangles, my code is kinda messy but you can see my character source rectangle is 62x62 which is about 1,5 bigger than 41x41, and I see one full stand
// theres the whole painting method
protected void onDraw(Canvas canvas)
{
//canvas.drawCircle(circleX, circleY, radius, redPaint);
if (bLost)
{
redPaint.setTextSize(72);
redPaint.setColor(Color.rgb(0,0,0));
canvas.drawText("You died. Touch screen to return.", 100, 240, redPaint);
}
else
{
canvas.drawBitmap(backgroundGraphic, bgsrc,bgdst,null);
if (!onGround()) canvas.drawBitmap(characterGraphic,charjsrc,chardst,null);
else canvas.drawBitmap(characterGraphic, charsrc, chardst, null);
redPaint.setStrokeWidth(1);
for (int i=0;i<listaL.size();i++){
canvas.drawRect(listaL.get(i).x,listaL.get(i).y,listaL.get(i).x+30,listaL.get(i).y+30,redPaint);
}
}

}

Heres the link to the properties of sprite.png so you dont think I'm crazy.

Where it says 123 it is the height and 410 is the width.
[Java for Android App Development] sak-venom1997:
You woudnt be restricted to only using OpenGl via NDK you can happily use any c/c++ libs

Not to mention the performance boost you get

Anyway its your call do what you.feel you're comfortable with
[Java for Android App Development] nikwen:
Thanks. Will check it later. ;)
[Java for Android App Development] Wampirate:
I didn't know about it. That's interesting, could you tell me a bit more about it? Where do I start ? :D

[MENTION=5101348]nikwen[/MENTION] Thanks :)
[Java for Android App Development] sak-venom1997:
Sure

Ill fetch some links for you

EDIT :

http://stackoverflow.com/a/4920992/2408192

This should get you started
[Java for Android App Development] Wampirate:
Another question emerged ;D

// its a function in my GameView class extending View
public void gameUpdate()
{
postInvalidate();
}

public class GameLoopThread extends Thread{

private boolean running;
public void setRunning()
{
running=!running;
}
[MENTION=439709]override[/MENTION]
public void run() {
while (running)
{
GameView.gameUpdate();
}
}
}

And beneath the method from GameView class is my game loop thread class, how do I make the drawing initiate, because GameView.gameUpdate(); doesnt work, says I cant make a static reference to a non-static method, and when I change gameUpdate method to static then there is a problem with postInvalidate(); which cant be in a static method?
[Java for Android App Development] sak-venom1997:
Replace GameView.gameUpdate();

With gameUpdate()

---------- Post added at 01:48 PM ---------- Previous post was at 01:45 PM ----------

Sry if GameThread is an inner class use GameView.this.gameUpdate()
[Java for Android App Development] Wampirate:
What if it's not an inner class? Does it have to be? I never know when I can make another class file and use methods/variables from another class file

Because its not inner in my case, and alone gameUpdate(); doesnt work either.
[Java for Android App Development] sak-venom1997:
If you intend to call it from GameView class use ..

---------- Post added at 01:58 PM ---------- Previous post was at 01:54 PM ----------

Add this field to GameThread

private GameView mGameView;

Add this constructor

public GameThread(GameView view)

{

mGameView = view;

}

Now you can call

mGameView.your public method(args)
[Java for Android App Development] Wampirate:
Thanks a lot ! Not sure how this works but it does :D
[Java for Android App Development] sak-venom1997:
But remember to instantiate the Thread using this constructor only
[Java for Android App Development] Wampirate:
Btw how do I instantiate it?

GameThread gThread = new GameThread(???);

My GameView instance is created in GameActivity class
[Java for Android App Development] nikwen:
That way:

GameThread gThread = new GameThread(myGameView); //the parameter is your GameView

If you want to start it:

gThread.start(); //NOT run()

---------- Post added at 01:34 PM ---------- Previous post was at 01:31 PM ----------

I would not use the NDK.

Learning the Java part should be easier than learning how to do it with the NDK as that is much more complicated.

Coding should take less time using Java, too.

So I suggest developing in Java. ;)

---------- Post added at 01:48 PM ---------- Previous post was at 01:34 PM ----------

Which is the exact name of the folder you put the image into?

Did you put different versions into the different directories like drawable-mdpi and drawable-hdpi?
[Java for Android App Development] Wampirate:
I did GameThread gThread = new GameThread(this); and it worked.

About the image you can see the image is taken from drawable folder, I created the folder in res and I have only one size for now, the game is specifically made for my phone so that I can learn android but that shouldn't matter . Something is wrong here. What exactly is with this pixel density? This is kinda weird.

Why would I need to take 62x62 from bitmap where there are 41x41 stands...
[Java for Android App Development] sak-venom1997:
42x42

Means the image is 42 pixels wide

and 42 pixels in height
[Java for Android App Development] Wampirate:
I know what it means, what else could it mean :D?

This method I use:

canvas.drawBitmap(bitmapName,sourceRect,destRect,null);

There's the problem. What is wrong in this method? How do you draw a part of a picture?

Another question ;d This is my GameActivity class, I can't get the pause and resume to work, when I lock and wake during game it starts from the beginning. Any suggestions? ;D I wanted simply to get logs from LogCat when I lock and wake my phone so that I see it works but I don't see any.

public class GameActivity extends Activity{

[MENTION=439709]override[/MENTION]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
GameView gv = new GameView(this);
//gv.setKeepScreenOn(true);
setContentView(gv);
Log.d("cycle","started");

}
[MENTION=439709]override[/MENTION]
protected void onResume()
{
super.onResume();
Log.d("cycle","resumed");
}
[MENTION=439709]override[/MENTION]
protected void onPause()
{
super.onPause();
Log.d("cycle","paused");
if (isFinishing()) Log.d("cycle","finishing");;
}
}
[Java for Android App Development] nikwen:
Hey, it is a problem with the BitmapFactory.decodeResource() method. Check this on stackoverflow for a fix: http://stackoverflow.com/questions/7929280/android-bitmap-getpixel-depends-on-density

---------- Post added at 09:13 PM ---------- Previous post was at 09:11 PM ----------

You might want to prevent that the screen turns off: http://stackoverflow.com/questions/8442079/keep-the-screen-awake-throughout-my-activity
[Java for Android App Development] Wampirate:
Thanks I will. EDIT: Thanks a lot actually it all makes sense now!! :D

I know how to keep screen on :) But its about like when you get a call to pause app and then resume when you come back to it.

I got the onResume() and onPause() to work, I'm not sure however how to invoke setRunnning() method from my GameThread class in them so that I stop the game onPause and resume onResume.

public class GameActivity extends Activity{

private GameThread mGameThread;
public GameActivity(GameThread thread)
{
mGameThread = thread;
}
[MENTION=439709]override[/MENTION]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
log("started");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
GameView gv = new GameView(this);
//gv.setKeepScreenOn(true);
setContentView(gv);

}
[MENTION=439709]override[/MENTION]
protected void onResume()
{
super.onResume();
log("resumed");
mGameThread.setRunning();
}
[MENTION=439709]override[/MENTION]
protected void onPause()
{
super.onPause();
log("paused");
mGameThread.setRunning();
if (isFinishing()) log("finishing");
}
private void log(String text)
{
Log.d("cycle",text);
}
}

package com.agpfd.game;

import java.util.Random;

public class GameThread extends Thread{
private GameView mGameView;

Random rand = new Random();;
Obstacles schowek;
/*private int newTime;
private int oldTime=(int) System.currentTimeMillis();*/
private boolean running;
public GameThread(GameView view)
{
mGameView = view;
}
public boolean setRunning()
{
running=!running;
return true;
}
[MENTION=439709]override[/MENTION]
public void run() {
while (running)
{
/*newTime = (int) System.currentTimeMillis();
int deltaTime = newTime - oldTime;
if (deltaTime>=17)*/

mGameView.gameUpdate(mGameView.gThread);
mGameView.drawUpdate();
//oldTime=newTime;
try {
GameThread.sleep(23);
} catch (InterruptedException e) {
e.printStackTrace();
}

}
}
}

I tried doing it analogically like sak-venom1997 told me to do in GameThread class, making a constructor and so on, but I don't think it's for Activities, now it won't even start on my phone;D
[Java for Android App Development] nikwen:
Yeah, the thread will finish if you call the setRunning method for the second time because the while (running) loop will not longer be executed.

A workaround could be this:

package com.agpfd.game;

import java.util.Random;

public class GameThread extends Thread{
private GameView mGameView;

Random rand = new Random();;
Obstacles schowek;
/*private int newTime;
private int oldTime=(int) System.currentTimeMillis();*/
private boolean running;
public GameThread(GameView view)
{
mGameView = view;
}
public boolean setRunning()
{
running=!running;
return true;
}

[MENTION=439709]override[/MENTION]
public void run() {
[COLOR="Red"]while (true)
{
if (interrupted()) {
break;
}
if (running) {[/COLOR]
/*newTime = (int) System.currentTimeMillis();
int deltaTime = newTime - oldTime;
if (deltaTime>=17)*/

mGameView.gameUpdate(mGameView.gThread);
mGameView.drawUpdate();
//oldTime=newTime;
try {
GameThread.sleep(23);
} catch (InterruptedException e) {
e.printStackTrace();
}
[COLOR="Red"] } else {
try {
GameThread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}[/COLOR]

}
}
}

Override the onDestroy() method of the Activity:

[MENTION=439709]override[/MENTION]
public void onDestroy()
{
super.onDestroy();
mGameThread.interrupt();
}

---------- Post added at 09:32 PM ---------- Previous post was at 09:31 PM ----------

Great. :)
[Java for Android App Development] Wampirate:
setRunning() changes boolean running to the opposite value, that is if it's true it will be set to false, and if false to true. I can call it how many times I want, can't I? When I call in onPause() it will change to false and the thread will do nothing, then onResume() is should start the thread again.

The problem is that

private GameThread mGameThread;

public GameActivity(GameThread thread)

{

mGameThread = thread;

}

this can't be in an Activity class and it doesn't work, so there is no calling mGameThread.setRunning(), I need another way to somehow call this method

If you get me ;D I just cant call this method in my activity class because it doesnt exist.
[Java for Android App Development] nikwen:
For the onResume:

In your implementation it will not. Why? Let me explain that:

What happens if you set running to false? The while loop will finish. It will not be executed. You could create a new thread and start that or you could use the method I provided in the previous post. However, creating a new one would be better (especially for the battery life).

Ah, sorry. I did not get that. You are right. :o

I would create the thread in the onResume method of your Activity. You would not even need the setRunning thing. Just call mGameThread.interrupt() in the onPause method after adding this to the run() method:

if (interrupted()) {
break;
}
[Java for Android App Development] Wampirate:
But how can I call mGameThread.interrupt() this, when there is no mGameThread or whatever, that is the problem.

I don't know how to refer to methods of my GameThread class in GameActivity class. :)

public class GameActivity extends Activity{

[MENTION=439709]override[/MENTION]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
log("started");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
GameView gv = new GameView(this);
//gv.setKeepScreenOn(true);
setContentView(gv);

}
[MENTION=439709]override[/MENTION]
protected void onResume()
{
super.onResume();
log("resumed");
}
[MENTION=439709]override[/MENTION]
protected void onPause()
{
super.onPause();
log("paused");
if (isFinishing()) log("finishing");
}
private void log(String text)
{
Log.d("cycle",text);
}
}

This is the GameActivity class. In here I want to stop or resume my thread which is instantialized in my GameView class, using setRunning() method of that GameThread class. :D
[Java for Android App Development] nikwen:
Ok. I will do it:

public class GameActivity extends Activity{

private GameThread mGameThread;

[MENTION=439709]override[/MENTION]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
log("started");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//gv.setKeepScreenOn(true);
setContentView(gv);

}
[MENTION=439709]override[/MENTION]
protected void onResume()
{
super.onResume();
log("resumed");
mGameThread = new GameThread();
}
[MENTION=439709]override[/MENTION]
protected void onPause()
{
super.onPause();
mGameThread.interrupt();
log("paused");
if (isFinishing()) log("finishing");
}
private void log(String text)
{
Log.d("cycle",text);
}
}

package com.agpfd.game;

import java.util.Random;

public class GameThread extends Thread{
private GameView mGameView;

Random rand = new Random();;
Obstacles schowek;
/*private int newTime;
private int oldTime=(int) System.currentTimeMillis();*/

[MENTION=439709]override[/MENTION]
public void run() {
while (true)
{
if (interrupted()) {
break;
}
/*newTime = (int) System.currentTimeMillis();
int deltaTime = newTime - oldTime;
if (deltaTime>=17)*/

mGameView.gameUpdate(mGameView.gThread);
mGameView.drawUpdate();
//oldTime=newTime;
try {
GameThread.sleep(23);
} catch (InterruptedException e) {
e.printStackTrace();
interrupt();
}

}
}
}

Does that do the trick?
[Java for Android App Development] Wampirate:
That will make a new Thread on each resume? Will it pause the game and when I create a new one return to that last state before pause? Perhaps that might work, I'll try
[Java for Android App Development] nikwen:
No, it will not return to the last state.
[Java for Android App Development] Wampirate:
You sure? I actually think it might, since it doesn't create a new instance of my GameView class, and the Thread is only to update everything in GameView class.

So when theres no thread, theres no updating and then we create a new one and it updates again.

If it doesn't work then I need something else, since I would like to be able to pause and resume my game, like when you get called and the game is paused then you can return to it.

Because right now even without stopping thread, when I get a call during play or lock the screen and then come back it starts from the beginning
[Java for Android App Development] nikwen:
Yeah, it depends on where you save the game state. If you save it in the Activty or any other class, it will continue the old state.

If you save it in the Thread class, then it will have a new state, again.

You can also combine this solution with the one I provided some minutes ago:

[MENTION=439709]override[/MENTION]
public void run() {
while (true)
{
if (interrupted()) {
break;
}
if (running) {
/*newTime = (int) System.currentTimeMillis();
int deltaTime = newTime - oldTime;
if (deltaTime>=17)*/

mGameView.gameUpdate(mGameView.gThread);
mGameView.drawUpdate();
//oldTime=newTime;
try {
GameThread.sleep(23);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
try {
GameThread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}
}

However, I would not recommend that because the thread will still be running when it is paused.

It will do one operation per second. That might be very battery-consuming.

---------- Post added at 10:19 PM ---------- Previous post was at 10:16 PM ----------

You could also save your game state in another class called GameState:

class GameState {
//TODO: save the game state here (using fields)
}

Then in the constructor of the thread:

GameState mGameState;

public GameThread (GameState state) {
super();
mGameState = state;
}

This would work with the method creating a new Thread in every onResume(). (That one: http://xdaforums.com/showthread.php?p=43230230#post43230230 Just modify it according to the second method in this post.)

That is what I'd suggest.
[Java for Android App Development] Wampirate:
Tommorow I'll look into what you wrote and try to understand, but for now I will rest ;D

Thanks for helping me, the game is really playable now, I even managed to get it to work on different phones ;)

Now I only need to make this onPause and onResume and so on to work and also to save the game and save scores to sd card and I could release it already ^^
[Java for Android App Development] Wampirate:
If I create a new GameThread in GameActivity class then I cant start it from my GameView class. I mean, about what you said. If I create that thread in onResume(), how do I start and set it running in GameView later?

I tried making a public GameView gv in activity, then I can access gv.gThread.setRunning() , at least eclipse doesn't show any errors but I don't think it works. Why not?

public class GameActivity extends Activity{

public GameView gv;
[MENTION=439709]override[/MENTION]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
log("started");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
gv = new GameView(this);
//gv.setKeepScreenOn(true);
setContentView(gv);
}
[MENTION=439709]override[/MENTION]
protected void onResume()
{
super.onResume();
log("resumed");
}
[MENTION=439709]override[/MENTION]
protected void onPause()
{
super.onPause();
gv.gThread.setRunning();
log("paused");
if (isFinishing()) log("finishing");
}
private void log(String text)
{
Log.d("cycle",text);
}
}

I've done some more logs with that code beneath. I noticed that when I press play in LogCat there is started and resumed shown, then when I lock the screen while playing, it shows paused, stopped and destroyed and immediately after started and resumed, even though it is still locked. Anyone has any idea why is it like that? Moreover, when I unlock it again pauses, stops and destroys and then starts and resumes.

public class GameActivity extends Activity{

public GameView gv;
[MENTION=439709]override[/MENTION]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
log("started");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
gv = new GameView(this);
//gv.setKeepScreenOn(true);
setContentView(gv);
}
[MENTION=439709]override[/MENTION]
protected void onResume()
{
super.onResume();
log("resumed");
}
[MENTION=439709]override[/MENTION]
protected void onPause()
{
super.onPause();
gv.gThread.setRunning();
log("paused");
if (isFinishing()) log("finishing");
}
private void log(String text)
{
Log.d("cycle",text);
}
[MENTION=439709]override[/MENTION]
protected void onDestroy() {
log("destroyed");
super.onDestroy();
}
[MENTION=439709]override[/MENTION]
protected void onRestart() {
log("restarted");
super.onRestart();
}
[MENTION=439709]override[/MENTION]
protected void onRestoreInstanceState(Bundle savedInstanceState) {

super.onRestoreInstanceState(savedInstanceState);
}
[MENTION=439709]override[/MENTION]
protected void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);
}
[MENTION=439709]override[/MENTION]
protected void onStop() {
log("stopped");
super.onStop();
}

}
[Java for Android App Development] nikwen:
I did not say that you should create the Thread in the GameView but in the GameActivity.

The methods I posted above should work then.
[Java for Android App Development] Wampirate:
I understood that, but once I create a thread I should call a start method, and additionally for my thread a setRunning() method so that it actually does something. EDIT: I could get rid of that running boolean but still I have to start the thread, I can't start it in GameActivity, can I?

And since the thread would be created in GameActivity class, I would have no access to it from my GameView class, where I would normally call those methods.
[Java for Android App Development] nikwen:
You can start it in the GameActivity.

You can access them from your GameView. Make the methods public and you can access them. ;)

Just use the second method I provided here: http://xdaforums.com/showthread.php?p=43230610#post43230610 That would be the best behaviour in my view.

(I modified it to make it easier to understand. ;))
[Java for Android App Development] Wampirate:
Is there a way to make double buffering using view class, not surfaceview, in order to get rid of the flickering and make the image smoother?
[Java for Android App Development] nikwen:
I don't know. Sorry. ;)
[Java for Android App Development] Wampirate:
Ehh ;/ Gonna study that SurfaceView - I tried to start learning AndEngine but that's a bit too difficult and there aren't many tutorials
[Java for Android App Development] out of ideas:
Andengine is pretty crazy to me too. I can find a good amount of tutorials, but some are for the older versions, and some are for GLES2, and some for GLES 2 anchor center branch, etc...

It gets pretty crazy then I just go back to apps instead of games.

The way they changed the old BaseGameActivity to the new SimpleBaseGameActivity, while putitng in a new BaseGameActivity really throws me for a loop.

this guy has a pretty good tut you might want to check out.

http://www.matim-dev.com/full-game-tutorial---part-1.html

and the andengine forums might help a bit.

http://www.andengine.org/forums/
[Java for Android App Development] Wampirate:
Will try thanks :). For now, I need help with movement.

There is an object in my game, which has x and y coordinates. X is the same throughout the whole game. Only y is changing. I want player to swipe down and up to control the object, you understand? The problem is when I simply write

...
case MotionEvent.ACTION_MOVE:
player.y=event.getY();

there is a glitch, because for example when the object is on top of the screen and you start swiping from bottom to top, it will "teleport" to bottom and move along with your finger. How do I make it so that when you swipe for example 100 pixels from bottom to top, then the ship moves up for the same amount of pixels, regardless of where the swipe took place?
[Java for Android App Development] nikwen:
Save the y coordinates of the finger and the player when the user touches the screen for the first time (should be something like ACTION_DOWN).

Then calculate the movement that way:

...
case MotionEvent.ACTION_MOVE:
player.y=savedPlayerY + (event.getY() - savedFingerY);
[Java for Android App Development] Wampirate:
It does the same as simply player.y=event.getY();

The "teleporting" still happens.
[Java for Android App Development] nikwen:
It will do the same if you do not save the values correctly.

Do this:

Add this to the class:

int savedPlayerY, savedFingerY;

In the touch method:

case MotionEvent.ACTION_DOWN:
savedPlayerY = player.y;
savedFingerY = event.getY();
break; //Do not forget the break!
case MotionEvent.ACTION_MOVE:
player.y=savedPlayerY + (event.getY() - savedFingerY); //the code posted above
break; //Do not forget the break!

Do not forget the break!