I am designing a simple Android game and I am having problem with collusion.
There is Sprite object and another static sprite object when they collide beginContact(Contact..) function never gets called.
When does this get beginContact function get called? Is there any nice example that u know of?
Thanks for the help.
Much appreciated.
I am not sure if you can use beginContact on sprites, as far as I know it is tied to the box2D extension. Anyway, I use the following snippet:
public boolean setListener(PhysicsWorld mGamePhysicsWorld) {
mGamePhysicsWorld.setContactListener(new ContactListener() {
#Override
public void beginContact(final Contact contact) {
// do stuff
}
public void endContact(Contact contact) {
// do some other stuff
});
return true;
}
Related
In Android, how do I take an action whenever a variable changes?
So I want to implement a listener for an object I created. What I want it to do is execute a block of code when its value changes from false to true.
As I am following this thread, I can't understand where the person wants us to implement the last block of code containing the logic for the listener.
Could someone, hopefully, guide me in the right direction?
(This question is being asked here as I don't have enough rep. points)
That last bit of example code triggers the listener, so it basically needs to be run whenever the "event" occurs. In this case the "event" is whenever (wherever in the code) the value of the variable changes.
If you have a setter and that is the only place the value changes, that is where you'd put it. If you are changing the value in multiple places throughout your code, I would make a new private method (call it signalChanged), put your code there, and then call it immediately after the variable assignment in the cases you want the listener to fire.
Here's an example (some code borrowed from linked answer, haven't checked that it compiles).
public class MyObj
{
public MyObj(int value)
{
setValue(value);
}
private int myValue;
public int getValue() { return myValue; }
public void setValue( int value )
{
if (value != myValue)
{
myValue = value;
signalChanged();
}
}
public interface VariableChangeListener
{
public void onVariableChanged(Object... variableThatHasChanged);
}
private VariableChangeListener variableChangeListener;
public void setVariableChangeListener(VariableChangeListener variableChangeListener)
{
this.variableChangeListener = variableChangeListener;
}
private void signalChanged()
{
if (variableChangeListener != null)
variableChangeListener.onVariableChanged(myValue);
}
}
you have to create a callback interface
here is a good about custom listener tutorial
here is a sample
public class MyObj {
VariableChanger onVariableChanged ;
public void setOnVariableChanged(VariableChanger onVariableChanged) {
this.onVariableChanged = onVariableChanged;
}
void log(){
boolean changed = false;
onVariableChanged.onVariableChanged();
//this will call it
}
interface VariableChanger{
void onVariableChanged();
}
}
class logic {
MyObj mo = new MyObj();
void main(){
mo.setOnVariableChanged(new MyObj.VariableChanger() {
#Override
public void onVariableChanged() {
//do your action
}
});
}
}
In Android, like any language, most developper uses logic comparisons to check values (if, else, switch, =, !=, >, <, etc) or Event (signal)
What kind of listener do you want to implement?
I'm trying to use locks in Android to detect when the activity is active or not, the flow of my app is below,
I'm using threads running by service and I have one activity.
The thread will check if the Activity is open or not,
1- If it is open/active then it will NOT update the DB.
2- If the activity/active isn't open then it will update the DB.
Is the below code correct,
On the activity side I'm doing this,
public void onCreate(Bundle savedInstanceState) {
GlobalVars.setActivityActive()
}
On the thread side I'm doing this,
if (GlobalVars.isActivityActive())
do nothing
else
update DB
And the global vars class is this
public class GlobalVars extends Application {
private boolean ActivityActive;
public boolean isActivityActive() {
synchronized (this) {
return ActivityActive;
}
}
public void setActivityActive(boolean ActivityActive) {
synchronized (this) {
this.ActivityActive = ActivityActive;
}
}
}
My question is this, is using synchronized in the get correct? or should I just do this,
public boolean isActivityActive() {
return ActivityActive;
}
Update
This is how I ended up using it based on the answers/comments
private volatile boolean activityActive;// this would be enough to be thread safe
Yeah, that will work.
It's crazy, though. If you are gonna synchronize on "this" you could make it simpler by just synchronizing the methods. ... so what is that ReentrantLock for?
... and you could make the whole thing even simpler by just using an AtomicBoolean.
I have been coding an Android app that has a lot of code dedicated to it. As you can imagine, there's lots of case-driven code in there. Because most of Android callback functionality is based on integers and ItemIDs and requestCodes, there is a lot of functionality built into switch statements or if-then-else constructs.
What are the best practices for organizing/refactoring this code in a better way? What have you found that works to reduce the amount of code and clarifies it at the same time? Is a huge amount of small classes going to hurt Android performance?
Thanks in advance.
A large number of classes is not going to affect the performance of the application. Some good practices in Android, however, include placing values like integers, item IDs, and request codes into a Resources xml file.
You will also see a lot of Callback classes as inner interfaces of the Object they relate to:
public class MyObject
{
private Callback callback;
private Object returnObject;
public void setCallback(Callback callback)
{
this.callback = callback;
}
public void doSomething()
{
//do something - could be an anync task or other that assigns returnObject
callback.invoke(returnObject);
}
public interface Callback
{
public void invoke(Object obj);
}
}
Then you can use this as follows:
MyObject obj = new MyObject();
obj.setCallback(new MyObject.Callback() {
#Override
public void invoke(Object obj) {
Log.i("Callback", obj.toString());
}
});
obj.doSomething();
I'm using a class that extends GenericPool to handle getting and recycling of sprites. What I'd like to do is have a method in that class to recycle after a certain duration. Something like:
public void recyleIn(Sprite sprite, float durationSeconds) {}
And there's a handy/dandy DelayModifier that seems like the proper way to implement it. So we'd have something like the following:
public void recyleIn(MySprite mySprite, float durationSeconds) {
mySprite.registerEntityModifier(
new DelayModifier(
durationSeconds,
new IEntityModifierListener() {
#Override
public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
}
#Override
public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
recycle(mySprite);
}
}
));
}
Now here's the problem: I can't call the recycle() method or do the recycling right there unless the Sprite is "final". Ordinarily that wouldn't be a problem, I'd just make a "final" deepcopy and use that. But in this case, making a copy defeats the original purpose of recycling in a pool. (E.g., if I'm just going to make a copy anyway, why bother to recycle and use pooling in the first place?)
Any ideas on the proper approach/model for this sort of thing? Thanks in advance.
UPDATE:
Umm.. I just realized I could just use the IEntity parameter in the onModifierFinished() method. (I'm used to ignoring those parameters for some reason.) So I could do something like this within the onModifierFinished():
recycle((MySprite) pItem);
I'd still be curious on anyone's thoughts on the best approach, but I think I may have just missed the obvious here.
I'm going to go ahead and answer my own question to help those in this situation. The short answer is that it's proper to use the IEntity parameter rather than an object outside it. The second issue I ran into when doing this is that I wanted to detach the IEntity from the scene in this method but, in AndEngine, it's important that when an IEntity is detached from the scene it must happen in the UpdateThread--otherwise you have a chance of errors as the scene is drawing while something is being removed from it. The end result looks like this:
#Override
public void onModifierFinished(IModifier<IEntity> pModifier, final IEntity pItem) {
pool.getContext().runOnUpdateThread(new Runnable() {
#Override
public void run() {
pItem.detachSelf();
}
});
}
One other small but important point is the "final" modifier on the Ientity parameter--necessary so that it can be called within the run() thread.
I know a good amount of java but this is my first time programming with the android sdk. I need to get the rotation of the phone in real time and display it on the screen. I was wondering what sensor method to use, as I heard that getOrientation was processor intensive and may not work in real time. Secondly, I was wondering which class I'd right this program in, I don't quite understand android class hierarchy yet. Thirdly, how would I make the numbers change on the screen in real time?
Thanks for the help!
I was wondering what sensor method to use, as I heard that getOrientation was processor intensive and may not work in real time.
You'll want to have a look at the OrientationEventListener object.
Secondly, I was wondering which class I'd right this program in, I don't quite understand android class hierarchy yet.
To get you started, you could build all this code into an Activity. Unlike a traditional Java program there is no main() entry point method and you won't user the constructors of application component classes to instantiate them. Lifecycle callback methods like onCreate() and onDestroy() are where you will want to do initialization and teardown of instance information. This guide may help you in how to construct your application to use a single Activity.
Thirdly, how would I make the numbers change on the screen in real time? Thanks for the help!
The OrientationEventListener includes a callback method for each change, simply use this callback to update a view in your UI.
Here is a simple example pulling it all together:
public class OrientationActivity extends Activity {
private OrientationEventListener mListener;
private TextView mTextView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextView = new TextView(this);
setContentView(mTextView);
mListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) {
#Override
public void onOrientationChanged(int orientation) {
mTextView.setText(String.valueOf(orientation);
}
};
}
#Override
public void onResume() {
super.onResume();
mListener.enable();
}
#Override
public void onPause() {
super.onPause();
mListener.disable();
}
}