I'm new in AndEngine and there is too many hard things for me now..
I want to know where I touched when I touch anywhere, and give actions by those locations with x and y. Can anybody help me?
Go throught the AndEngine Examples. There are two methods. Either you register touch on an Entity, as shown for example in TouchAndDrag example:
final Sprite sprite = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager()) {
#Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
return true;
}
};
Or you use the touch listener on a Scene. You have to implement the IOnSceneTouchListener and then set it in your scene:
public class MyListener implements IOnSceneTouchListener {
#Override
public boolean onSceneTouchEvent(Scene pScene, final TouchEvent pSceneTouchEvent) {
if (pSceneTouchEvent.isActionDown()) {
//execute action.
}
return false;
}
}
}
...
scene.setOnSceneTouchListener(new MyListener ());
You can find most of the code needed in the examples already. See this tutorial on how to add them to Eclipse.
Related
Here's my idea: I wanted to have a scrollview where the user could both scroll the component in it, and click on it.
After hours of testing/search, I've finally managed to make this working with the following code.
My problem is in the comparison Math.Abs(eventData.delta.x) > 1.0f. I consider that if the mouse "moves more than 1.0f" then it's dragging, else I consider this as a click.
The value 1.0f works perfectly on all the devices with a mouse = it's easy not to move, and big screen (tablets), when you click. But on smartphones, ie my Galaxy S6 or the 3 other ones I've tried, it's very sensitive and almost impossible to make a click.
How could you programmatically handle this? Is there a DPI or something to take in account and based on this, multiply my 1.0f by the resolution?
public class BoardHandler : EventTrigger
{
private static GameObject _itemDragged;
private static bool _isDragging;
private static ScrollRect _scrollRect;
public override void OnPointerClick(PointerEventData data)
{
if (_isDragging) {
return;
}
Debug.Log("Click");
}
public override void OnBeginDrag(PointerEventData eventData)
{
if (Math.Abs(eventData.delta.x) > 1.0f ||
Math.Abs(eventData.delta.y) > 1.0f) {
_scrollRect.OnBeginDrag(eventData);
_isDragging = true;
}
}
public override void OnDrag(PointerEventData eventData)
{
if (_isDragging) {
_scrollRect.OnDrag(eventData);
}
}
public override void OnEndDrag(PointerEventData eventData)
{
if (!_isDragging) {
return;
}
_scrollRect.OnEndDrag(eventData);
_isDragging = false;
}
private void Start()
{
_scrollRect = GetComponentInParent<ScrollRect>();
}
}
I've used TouchScript in the past to cover multiple devices within one project, like from 9 screens 6k screen array to tablets. They have a set of utilities to handle multiple resolution and dpi.
Check out the the UpdateResolution method in TouchManagerInstance.
hth.
Here's the method I used to move a sprite in X-axis
public void moveX(float constanttime,float fromX,int toX,final Sprite s){
MoveXModifier mod2=new MoveXModifier(constanttime, fromX, toX);
s.registerEntityModifier(mod2);
}
I'm calling this method like this when I want to start moving the sprite.
moveX(5, car1.getX(), -100, car1);
(car1 is the sprite I want to move)
I want to stop moving this sprite when the user touches the sprite(car1).
Can someone help me with this?
Actually you can pause entity modifiers (and all updates regarding specific entities individually). The following code allows you to pause an entity's updates based on a boolean value manipulated via touch events (not registered to the scene in the snippet). The following code snippet will cause modifiers on the entity to pause until isActionUp() is called. You can effectively pause entity updates (modifiers) by overriding the onManagedUpdate() method of an entity and passing a value of 0, which tells the entity 0 seconds have passed and to not update the entity modifiers.
boolean isTouched = false;
final Rectangle rectangle = new Rectangle(0, 0, 0, 0, mResourceManager.getEngine().getVertexBufferObjectManager()){
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionDown()){
isTouched = true;
}
if(pSceneTouchEvent.isActionUp()){
isTouched = false;
}
return super
.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
}
#Override
protected void onManagedUpdate(float pSecondsElapsed) {
if(isTouched){
super.onManagedUpdate(0);
} else {
super.onManagedUpdate(pSecondsElapsed);
}
}
};
I want to make player jumps following this tutorial but I can't. Please take a look into my code and help me to fix it. Here is my codes:
mPhysicsWorld = new PhysicsWorld(new Vector2(0,
SensorManager.GRAVITY_EARTH),false);
sapo = new Sapo(100,100,mVertexBufferObjectManager,mCamera,mPhysicsWorld) {
#Override
public void onDie() {}
};
attachChild(sapo);
sapo.jump();
public abstract class Sapo extends AnimatedSprite {
private Body mBody;
public Sapo(float pX, float pY, VertexBufferObjectManager vbo, Camera camera, PhysicsWorld physicsWorld)
{
super(pX, pY, ResourceManager.getInstance().mSapoTiledTextureRegion,vbo);
createPhysics(camera, physicsWorld);
}
private void createPhysics(final Camera camera, PhysicsWorld physicsWorld)
{
mBody = PhysicsFactory.createBoxBody(physicsWorld, this, BodyType.DynamicBody, PhysicsFactory.createFixtureDef(1f,1f,1f));
}
public abstract void onDie();
public void jump() {
mBody.setLinearVelocity(new Vector2(mBody.getLinearVelocity().x,-100));
}
}
I found the problem. I forgot to register the physic handler, like this:
myScence.registerUpdateHandler(mPhysicsWorld);
I'm trying to make a sprite so when you touch it and drag your finger, the sprite follows your movement. I've tried to follow the AndEngine examples, but they are part of GLES1. I'm using GLES2.
Within my code, the onAreaTouched is being called, but it is not continuously being called to update the sprite with my finger.
Thanks in advance.
public class RectangleFactory extends Sprite {
public float randomNumber;
public RectangleFactory(float pX, float pY, ITextureRegion pTextureRegion,
VertexBufferObjectManager pVertexBufferObjectManager) {
super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
// TODO Auto-generated constructor stub
Random random = new Random();
randomNumber = (float) random.nextInt(BallShakeActivity.CAMERA_WIDTH);
};
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y)
{
Log.d("Mark", "circles are touched");
this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
if(pSceneTouchEvent.isActionMove()){
Log.d("Mark", "finger is moving");
}
return true;
};
#Override
protected void onManagedUpdate(final float pSecondsElapsed){
if(this.mY > 0f){
}
else{
Random random = new Random();
randomNumber = (float) random.nextInt(BallShakeActivity.CAMERA_WIDTH);
this.setPosition(randomNumber, 800f);
}
super.onManagedUpdate(pSecondsElapsed);
}
}
You can find some examples, including a touch-drag example, for AndEngine GLES2 in the AndEngineExamples repository on Nicolas' Github page.
https://github.com/nicolasgramlich/AndEngineExamples
The examples seem to be mostly for the GLES2 branch but there may also be some examples for the GLES2-AnchorCenter branch as well.
Some of the code works with GLES1/master branch but you're mostly on your own for examples for that branch.
I'm trying to make a simple glassware app that displays the accelerometer data on the screen. The code I am posting works on my Android phone, but when I use it on google glass, only the Y axis seems to work correctly (looking up and down).
When I say the Y axis, it's actually the third array entry from the SensorEvent event.values[] array (instead of the second array entry - see code).
All TextViews DO display a float value similar to how they appear on my android phone, but they just don't change by much (around .1 to .2) when moving my head.
public class SensorActivity extends Activity implements SensorEventListener {
SensorManager mSensorManager;
mAccel;
TextView tvx, tvy, tvz;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main);
tvx = (TextView) findViewById(R.id.tvx2);
tvy = (TextView) findViewById(R.id.tvy2);
tvz = (TextView) findViewById(R.id.tvz2);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccel = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAccel, SensorManager.SENSOR_DELAY_NORMAL);
}
#Override
protected void onPause(){
super.onPause();
mSensorManager.unregisterListener(this);
}
#Override
protected void onResume(){
super.onResume();
mSensorManager.registerListener(this, mAccel, SensorManager.SENSOR_DELAY_NORMAL);
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
#Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
tvx.setText(Float.toString(x));
tvy.setText(Float.toString(y));
tvz.setText(Float.toString(z));
}
}
The Glass Documentation pretty much points to the Android documentation, so I am assuming they should be the same.
Any insight would be greatly appreciated.
**** NEW UPDATE ****
Now, all of a sudden. The Y-axis is reading the Y-direction (and the Z-axis is too). Main problem with that is that the Y-axis reading starts at value "9" and goes to "0" in either direction (no negative direction) so determining where the user is looking is an issue. I am aware I could call the Z-axis the Y-axis, but that would just be masking a different issue.
It turns out that the X-axis is registering head tilt and NOT head turn (which is not in accordance with the documentation). I haven't found any documentation pertaining to calibrating the accelerometer (if that's even possible).