OnAreaTouched stacking Andengine - touch

First I will show you image to explain my problem
Description of image:
I have 2 sprites RED and white .I attached
Red Sprite with Scene and
White Sprite with Red Sprite
I have moved and scaled it Red Sprite and white sprite like in 2nd image
My Problem
I have registered touch listener.But I want to register touch of white sprite when full image is scaled (after 2nd image case)
But when i register touch on fully scale it doesn't trigger touch .
To trigger touch i have to register both sprites touch before image 1
I have also seen similar problem here .
My code
> `titleScene = new Scene();
titleScene.setBackground(new Background(0, 1, 0));
A_Background_Sprite = new Sprite(0, 0, A_Background_TextureRegion, activity.getVertexBufferObjectManager());
titleScene.attachChild(A_Background_Sprite);
RED_Sprite = new Sprite(50, 50, RED_TextureRegion, activity.getVertexBufferObjectManager()){
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
if(!RED_Sprite.isScaled())
{
Log.i("Apple_Background_Sprite", "setOnClickListener");
AppleX=(int) RED_Sprite.getX();
AppleY= (int) RED_Sprite.getY();
RED_Sprite.registerEntityModifier(new SequenceEntityModifier(new MoveModifier(1f, RED_Sprite.getX(), camera.getCenterX(), RED_Sprite.getY(),camera.getCenterY()),new ScaleModifier(1f, 1f, 2f, new IEntityModifierListener() {
public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
// TODO Auto-generated method stub
white_Sprite.setVisible(true);
titleScene.registerTouchArea(white_Sprite);
}
public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
// TODO Auto-generated method stub
}
},EaseLinear.getInstance())));
}
else
{
// Spelling_Background_Sprite.registerEntityModifier(new MoveModifier(4f, camera.getWidth(), 20, 400, 400));
}
return true;
// here you can use the code
}
};
RED_Sprite.setSize(150f, 150f);
titleScene.setTouchAreaBindingOnActionDownEnabled(true);
white_Sprite.setVisible(false);
titleScene.attachChild(RED_Sprite);
RED_Sprite.attachChild(white_Sprite);
titleScene.registerTouchArea(RED_Sprite);
PLease Take a look at
RED_Sprite.registerEntityModifier(new SequenceEntityModifier(new MoveModifier(1f, RED_Sprite.getX(), camera.getCenterX(), RED_Sprite.getY(),camera.getCenterY()),new ScaleModifier(1f, 1f, 2f, new IEntityModifierListener() {
public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
// TODO Auto-generated method stub
white_Sprite.setVisible(true);
titleScene.registerTouchArea(white_Sprite);
}

When you have overlying objects, and you want to trigger touch events on all of them, be sure you don't return true at the end of the onTouch callback function, otherwise the touch listener stops processing the remaining touch events. See this tutorial for more info http://andengineguides.wordpress.com/2011/09/14/getting-started-touch-events/

Related

How to destroy instantiated button upon collision exit - Unity 3D

I am a beginner in Unity and I am currently making a simple game. I have a problem where the instantiated Button is not destroyed.
The process of the script is to instantiate a button and destroy it upon collision exit. But when I move out of the object, the object stays and it is not destroyed. i don't know if there is something wrong with the Destroy code of line.
Here is the script for the collision:
using UnityEngine;
using UnityEngine.UI;
public class InteractButtonPosition : MonoBehaviour
{
public Button UI;
private Button uiUse;
private Vector3 offset = new Vector3(0, 0.5f, 0);
// Start is called before the first frame update
void Start()
{
//uiUse = Instantiate(UI, FindObjectOfType<Canvas>().transform).GetComponent<Button>();
}
// Update is called once per frame
void Update()
{
//uiUse.transform.position = Camera.main.WorldToScreenPoint(this.transform.position + offset);
}
private void OnCollisionEnter(Collision collisionInfo)
{
if(collisionInfo.collider.name == "Player")
{
uiUse = Instantiate(UI, FindObjectOfType<Canvas>().transform).GetComponent<Button>();
uiUse.gameObject.SetActive(true);
uiUse.transform.position = Camera.main.WorldToScreenPoint(this.transform.position + offset);
}
}
private void OnCollisionExit(Collision collisionInfo)
{
if(collisionInfo.collider.name == "Player")
{
Destroy(uiUse);
}
}
}
The code does exactly what you have written, which is destroying uiUse which is a Button component. If you go and inspect your game object in the scene, you will see, that indeed, the button component has been destroyed.
What you want to do is to destroy the GameObject the button component is attached to, like: Destroy(uiUse.gameObject)

unity add object on touched place on screen(rotation)

hello guys i have a code that clone an object on the touched area on game and i have a button that change the rotation of the object (the y rotation by 25 deg),i want when i touch the screen that the clone objects will also change by the current rotation of the object. thank you guys
private Rigidbody myrigidbody;
void Start()
{
myrigidbody = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.touchCount > 0)
{
var touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
var pos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10f));
myrigidbody.MovePosition(new Vector3(pos.x, pos.y, 0f));
}
}
}
Gonna follow up on TEEBQNE here,
you need a way to reference all cubes if you want all of them to rotate. If you are just testing right now, you can tag all cube objects with a certain name like "cube" and use FindGameObjectsWithTag() function to iterate through all of the object types.
So you can change your function from this
public void firstAngle() {
cube.transform.Rotate(0f, 22.5f, 0.0f);
}
To this
public void firstAngle() {
foreach (GameObject obj in FindGameObjectsWithTag("cube")){
obj.transform.Rotate(0f, 22.5f, 0.0f);
}
}
Of course if you want better conventions as suggested in the comments, you can just replace the FindGameObjectsWithTag with a reference to all the cubes in the scene. This can either be an array, list, etc

How to check that object not hidden by another object?

I make game with isometric view. When player comes into the house the roof of it hides and player can interact with NPCs, items, etc. But now it can interact with it even when roof is visible. How to detect that item is hidden by house roof or wall or another object?
void Update() {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit[] hits = Physics.RaycastAll(ray, Mathf.Infinity);
if (Input.GetMouseButtonDown(0)) {
foreach (RaycastHit hit in hits) {
if (hit.collider.tag != "NPC") {
continue;
}
//Interact ...
}
}
}
You can simply check the distance between the hit wall/roof and NPC, from the ray-cast origin (camera). Like so:
private Camera cameraRef;
private void Awake() {
// P.S: Cache the 'Camera.main', calls to it can be expensive.
cameraRef = Camera.main;
}
void Update() {
if (Input.GetMouseButtonDown(0)) {
Ray ray = cameraRef.ScreenPointToRay(Input.mousePosition);
RaycastHit[] hits = Physics.RaycastAll(ray, Mathf.Infinity);
foreach (RaycastHit hit in hits) {
if (hit.collider.tag != "NPC") {
continue;
} else if (RaycastHitRoofOrWallFirst(hits, hit.collider.gameObject)) {
// This NPC is hidden behind a roof/wall.
continue;
}
// Interaction...
}
}
}
/// <summary>
/// Check if a target object is being hidden behind a roof/wall.
/// </summary>
/// <param name="hits">The hits that the raycast gotten.</param>
/// <param name="targetObject">The gameobject to check against.</param>
/// <returns>Return true if the target object is hidden, false if not.</returns>
private bool RaycastHitRoofOrWallFirst(RaycastHit[] hits, GameObject targetObject) {
foreach (RaycastHit hit in hits) {
if (hit.collider.CompareTag("roof") || hit.collider.CompareTag("wall")) {
float distanceFromCameraToObstacle = Vector3.Distance(cameraRef.transform.position, hit.collider.transform.position);
float distanceFromCameraToNPC = Vector3.Distance(cameraRef.transform.position, targetObject.transform.position);
// Check if the NPC is closer to the camera (raycast origin)
// compared to the roof or wall.
if (distanceFromCameraToObstacle < distanceFromCameraToNPC) {
// The roof/wall is closer to the camera (raycast origin)
// compared to the NPC, hence the NPC is blocked by the roof/wall
return true;
}
}
}
return false;
}
Here is a small visual diagram of what it should check for:
Or just use simple raycast...
If possible depending on the context, instead of using Physics.RaycastAll, you can use Physics.Raycast.
It returns the first object that the ray-cast hits.
Adding to this answer an alternative could maybe also be using OnBecameVisible
OnBecameVisible is called when the object became visible by any Camera.
This message is sent to all scripts attached to the Renderer.
and OnBecameInvisible
OnBecameInvisible is called when the Renderer is no longer visible by any Camera.
This message is sent to all scripts attached to the Renderer.
OnBecameVisible and OnBecameInvisible are useful to avoid computations that are only necessary when the object is visible.
For activating and deactivating the according NPC's colliders so the Raycast anyway will only work on visible objects in the first place.
Like on the NPCs have a script
public class InteractableController : MonoBehaviour
{
// you can also reference them via the Inspector
public Collider[] colliders;
private void Awake()
{
// pass in true to also get inactive components
if(colliders.Length = 0) colliders = GetComponentsInChildren<Collider>(true);
}
private void OnBecameInvisible()
{
foreach(var collider in colliders)
{
collider.enabled = false;
}
}
private void OnBecameVisible()
{
foreach(var collider in colliders)
{
collider.enabled = true;
}
}
}
However
Note that object is considered visible when it needs to be rendered in the Scene. It might not be actually visible by any camera, but still need to be rendered for shadows for example. Also, when running in the editor, the Scene view cameras will also cause this function to be called.

How to add collision physics in Unity Mapbox World Scale?

In World Scale AR, I want to move to an object I placed using SpawnOnMap. When I touch that object, I want that object to be erased. I made the Player have a script called PlayerController. I made a tag for the object so that when it touches, it should destroy or set it active. When I walk towards the object, nothing happens. I've tried OnTriggerEnter and OnCollisionEnter already. Is there some kind of method I'm missing that Mapbox provides?
public class PelletCollector : MonoBehaviour
{
public int count = 0;
// Start is called before the first frame update
void Start()
{
}
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Pellet")
{
count++;
Debug.Log("Collected Pellets: " + count);
other.gameObject.SetActive(false);
//Destroy(other.gameObject);
}
}
}

How to stop the move Modifier in AndEngine?

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);
}
}
};