What ist the difference between ontouchend and ontouchleave? - c#

It seems like for the following question Google should give an answer but (for me) it doesn't.
What ist the difference between the events ontouchend and ontouchleave or ontouchstart and ontouchenter, respectively?
Many thanks
Philipp

you have to consider your widget as a collision box that react to touch on screen
ontouchend() is called when you lift off you finger off the screen, won't activate if you first slide ouside the colision box.
ontouchleave() is called if your finger touche the colision box werever it come from et leave eitheir by lift up or sliding your finger out of the colision box.
ontouchenter() is as soon your finger enter the colision box
ontouchstart() is only called is your finger directly touched the collision box, its make to avoid accidental activation by sliding from another place of the screen

Related

Unity drag and drop check right or left (2D)

I wanna build a card game in Unity.
I want to player swipe cards left or right but I wanna show some tricks about what's gonna happen if they swipe left or right so if they grab the card (2D Image) and go a bit left or right a text will appear on image depends on side.
I tried to use swipe and use X axis change but It's not working properly.
Here is a basic pictured information about what i wanna build:
I would advise using the drag handlers system in unity to track where the card started, and trigger different effects at different distances / directions from its origin.
You can detect the drag of the card by implementing IBeginDragHandler, IDragHandler, IEndDragHandler on a script on your card object.
Begin is called when the object starts dragging, Drag is called during each frame its moving, and End is called when the object is dropped.
When a drag begins, you can cache the position where the card started. Then in Drag handler each frame you can check the distance of your card from where it started. Once it has moved more than a certain distance, you can trigger whatever tricks or effects you want to show.
Depending on how your coordinate space is set up, you could for example know left from right during drag updates such as:
// Inside of your on drag method, check each frame for where the card is
// If it has moved far enough, trigger an effect
if (Vector3.Distance(dragStartPosition, transfom.position) > yourTriggerDistance) {
// if X is greater, the card moved right
if (transform.position.x > dragStartPosition.x) {
// Trigger Right swipe text
ShowSomeRightSwipeText();
}
// otherwise, X is smaller, so the card has moved left by the required distance
else {
// Trigger left swipe text
ShowSomeLeftSwipeText();
}
}
Also please remember that:
In order for these interfaces to work, you need to have a Physics 2D Raycaster attached to your camera in the scene.
You need to make sure you have an Event System in the scene as well.
The above two things are often asked issues here regarding detecting object dragging.

Clicking anywhere triggers the button

In my scene, I have this set-up:
Game Controller
Level Controller
Main Camera
Event System
Canvas
Player
PlayerCanvas (Render Mode is World Space)
Button
I set the PlayerCanvas into the same width and height of the button. And both of them are just small. I put Debug.Log to check everytime I press the button. But somehow, it triggers the button even if I click way off the screen. Can help to explain why is this happening. Thanks!
Note: I'm trying to add a button beside the Player so that even if the Player moves, the button will just follow.
I think your hierarchy is wrong.
Try something like
Game Controller
Level Controller
Main Camera
Player
Event System
Canvas (Screen Space - Overlay)
Button
Not sure what Player is, but I'm sure on my hierarchy Event System and Canvas are on the same level. And every button is son of Canvas. Not sure you can make multiple Cavas (you should not need it anyway).

NGUI OnDragOver() pass through when colliders overlap

we are using NGUI, some collider has to be overlapped, and we want the ONDragOver() event received by the collider behind, ie should pass through to the underneath collider, Here is a demonstration of the situation:
The bigger one is on top of the smaller ones, and they are in the same hierarchy so that I can drag to move all of them.
I have tested that when object dragged over
void OnDragOver(GameObject that)
{
Debug.Log("you are on drag over workpanel not the brick slot");
//Debug.Log(" I am being draged over by : " + that.name);
}
This log will show to indict that the bigger collider will receive the event.
But actually, I want the smaller one in it to receive the event :-(
I have figured this out, change the z position of the collider want to hide behind to a larger value, so the one with a smaller z will get the event first, this may help with people with similar issue.
This may cause the bigger collider not to get the event when hit on the smaller collider, but, whatever, I think this will be good enough for this situation, if someone has a better solution, let us know ;D
And you also should know that the panel meters too. If you have a button collider that became not clickable after colliders overlapped, try to arrange the button in a new panel and tweak the panel depth.

Detecting when a Picture Box touches another control

I made a game where the player controls a square, whenever it hits a wall it is supposed to die. The Square is a Picture Box and the walls are picture boxes too. You can move using W,A,S and D. I was thinking about doing something similar to:
if(Square.Top == Square.Top + Square.Height)
and then restart the game. Is there any better way doing it? Istead of having alot of IFs? Whenever a control touches another to do something?
Thanks alot !
Yes, you get the control's containing rectangle by Control.Bounds, and then use IntersectsWith with another rectangle.
if(Square.Bounds.IntersectsWith(Wall.Bounds))
{
// ...
}
Keep in mind that it won't trigger when your square touches a wall, only when it starts going on top of it, but I assume that's what you want.

C# XNA 4.0 Fullscreen Mouse Moves Into Second Monitor

The heading just about says it all. I derive from the XNA Game class, and set the IsMouseVisible = true. I have a dual screen setup with the secondary display being extended from the primary. I set my game to fullscreen mode (GraphicsDeviceManager.IsFullScreen = true). All works fine and fullscreen mode is initialized. The only problem I have is that the mouse does not stay bound within the fullscreen game. As soon as it reaches the edge of the fullscreen game, it seamlessly moves over onto the secondary monitor. Is this expected bahaviour?
I do a Mouse.GetState() to retrieve mouse info every frame on the same thread in which my game runs.
I have even tried setting Mouse.WindowHandle = GameWindowHandle before every call to GetState, but it does not change anything.
Any ideas? Or is this expected behaviour and I need to clamp the mouse to the fullscreen area myself?
Regards,
Riaan
Yes, exactly, clamp your mouse to the screen that you want it to stay on. This is similar to an approach that I (and many others) use when I implement sprite cursors for my games. I hide the system mouse, and clamp it to the center of the screen so that it doesn't show up on other monitors.
The only downside is that you have to un-clamp your mouse if you minimize the game, etc. Otherwise it stays stuck, which can be really annoying.

Categories