cocos2dx Multi touch and touch - android

everyone! I have trouble with combination in one scene multi touch and one touch with. So let me describe my problem step by step.
I use cocos2dx( for android ), So I have scene, on this scene I add layer Pan Zoom Layer for scroll and zoom. next, I add new layer(Game Layer) on PanZoomLayer. I need catch click on Game Layer(on this layer located my game things) I try to do so, using pattern observer,I have done PanZoom Layer by "Subject" my Game Layer - "Observer", and in PanZoomLayer Have created enum EVENTS, and struct touchEvent
enum EVENTS{
EVENT_ONCE_CLICK,
EVENT_MOVE,
ANOTHER,
};
struct touchEvent {
EVENTS eventName;
};
and created in class PanZoomLayer field m_events :
std::stack<EVENTS> m_events;
and on onTouchesBegan add such code
if (_touches.size() == 1) {
m_events.push(EVENTS::EVENT_ONCE_CLICK);
}
else {
m_events.push(EVENTS::ANOTHER);
}
in onTouchesMoved add such code in branch if _touches.size() == 1:
Vec2 curTouchPosition = Director::getInstance()->convertToGL(touch->getLocationInView());
Vec2 prevTouchPosition = Director::getInstance()->convertToGL(touch->getPreviousLocationInView());
Vec2 deltaPosition = curTouchPosition - prevTouchPosition;
float pos = curTouchPosition.distance(prevTouchPosition);
if (fabs(pos) < 0.5f) {
m_events.push(EVENTS::EVENT_ONCE_CLICK);
}
else {
m_events.push(EVENTS::ANOTHER);
}
an onTouchesEnded, add such code :
if (m_events.empty())
return;
EVENTS ev = m_events.top();
while (!m_events.empty()) {
m_events.pop();
}
if (ev == EVENTS::EVENT_ONCE_CLICK) {
// MessageBox("once click", "title");
Touch *touch = (Touch *)_touches.at(0);
Vec2 position = touch->getLocation();
notifyClick(position);
}
where notifyClick :
std::shared_ptr<CSceneSession> sessionShared = m_session.lock();
if (sessionShared)
sessionShared->clickOnScene(point);
So I enter in game cycle : game scene -> transition on menu scene -> game scene...... in start it great work, but after some time ( on difference devices - different time) game begin strange behavior : buggy,
Could you help me please? Thanks for any idea and suggestions.

I Solved my problem in the following way: I remove scheduleUpdateForTarget in method onEnter and remove unscheduleAllForTarget in method onExit and remove method update.
Thank you for your attention!

Related

fl_chart touch response issue in LineChart

I have displayed a Line chart using fl_chart ^0.10.1 (Tested on Flutter ver 52.2.1 & 53.0.1).
I am updating the FlSpots from a csv file.
I am trying to get the index or Spot object if I touch somewhere in the plot.
But I am getting an empty list from the touchResponse if I touch (both tap or long-press) somewhere.
But I am getting the value in the plot itself. I am assuming it's there because of the built-in touch handler.
The code I am trying it:
lineTouchData: LineTouchData(
touchCallback: (LineTouchResponse touchResponse) {
if (touchResponse.touchInput is FlPanEnd ||
touchResponse.touchInput is FlLongPressEnd) {
var spots = touchResponse.lineBarSpots;
setState(() {
var lengthList = spots.length;
print(
'touched spot---> ${spots.toString()},,,len--->$lengthList');
});
} else {
print('wait');
}
}),
My end goal is to use and pass the touched index to open a new screen/widget.
touchCallback: (LineTouchResponse? lineTouch) {
final value = lineTouch.lineBarSpots![0].x;
// set state...
});
It's in the demo by fl_chart in github.
https://github.com/imaNNeoFighT/fl_chart/blob/master/example/lib/line_chart/samples/line_chart_sample3.dart

Unity and map services

I want to use the world map in Unity and have been looking at the API of various map services. I need to show a screenshot (a static map with markers) on the screen, and move to the full map view to navigate it after clicking on it.
MapBox managed to display the map with the selected coordinates and add test markers, but that's all I can do with a query like this: https://api.mapbox.com/styles/v1/mapbox/streets-v11/static/url-https%3A%2F%2Fwww.mapbox.com%2Fimg%2Frocket.png(-76.9,38.9)/-76.9,38.9,15/1000x1000?access_token=pk.eyJ1IjoiZGVuZGVhZCIsImEiOiJja2F1dha4egixnnfhmnvtc2u0y3bua2ntin0.GGOyhgN_fEqtPpPc5n6OLg because this request returns a jpg image.
They also have a plugin for Unity, but it's only used in 3d projects and does not allow me to configure the display in 2d.
In MapBox, the mapping I need is implemented using JavaScript for Web and Java for Android. On Android I can do what I need. I can connect to the API on Android, but will I be able to use it in Unity later?
It's the same with Google maps.
Actually, the question is, did someone work with map services in Unity? And how can this be implemented correctly?
I don't know if this is still relevant, but I used Mapbox in Unity (the Mapbox plugin) to create a AR Soundscape by "registering" GameObjects to coordinates and moving them in real-time when the map is moved.
Your problem sounds an awful lot like the one I solved with that.
Basically you provide the Lat/Lon values for your objects and convert them to Unity world space coordinates using the AbstractMap.GeoToWorldPosition() function.
I used a raycast to actually pull that off in-engine, which is quite convenient.
//Edit:
Unity is quite capable of handling 2D projects. You just have to configure it properly and build your project around it.
The following is the class that I use to handle all positioning-related calculations. Maybe it's of some help to you.
namespace TehMightyPotato.Positioning
{
[Serializable]
public class GeoPosition
{
[Tooltip(
"Update frequency of position polling. Update every n-th frame. 1 is every frame, 60 is every 60th frame.")]
[Range(1, 60)]
public int positionUpdateFrequency = 1;
[Tooltip("Should the object have a specified altitude?")]
public bool useYOffset = false;
[Tooltip("If useMeterConversion is activated the yOffsets unit is meters, otherwise its unity units.")]
public bool useMeterConversion = false;
[Tooltip("The actual y position of the object in m or unity units depending on useMeterConversion.")]
public float yOffset = 0;
[Tooltip("X is LAT, Y is LON")]public Vector2d geoVector;
[HideInInspector] public float worldRelativeScale;
// Apply the result of this function to your gameobjects transform.position on every frame to keep them on this position.
public Vector3 GetUnityWorldSpaceCoordinates(AbstractMap map)
{
UpdateWorldRelativeScale(map);
var worldSpaceCoordinates = map.GeoToWorldPosition(geoVector, false);
if (useYOffset)
{
worldSpaceCoordinates.y = yOffset;
}
return worldSpaceCoordinates;
}
public void UpdateWorldRelativeScale(AbstractMap map)
{
worldRelativeScale = map.WorldRelativeScale;
}
public void SetGeoVectorFromRaycast(Vector3 position, AbstractMap map, LayerMask layerMask)
{
var ray = new Ray(position, Vector3.down);
if (Physics.Raycast(ray, out var hitInfo, Mathf.Infinity, layerMask))
{
geoVector = map.WorldToGeoPosition(hitInfo.point);
}
else
{
throw new NullReferenceException("Raycast did not hit the map. Did you turn on map preview?");
}
}
public void SetYOffsetFromRaycast(AbstractMap map, Vector3 position, LayerMask layerMask)
{
UpdateWorldRelativeScale(map);
// using raycast because of possible y-non-zero maps/ terrain etc.
var ray = new Ray(position, Vector3.down);
if (Physics.Raycast(ray, out var hitInfo, Mathf.Infinity, layerMask))
{
var worldSpaceDistance = Vector3.Distance(position, hitInfo.point);
if (useMeterConversion)
{
yOffset = worldSpaceDistance * worldRelativeScale;
}
else
{
yOffset = worldSpaceDistance;
}
}
else
{
throw new NullReferenceException("Could not find map below. Is map preview turned on?");
}
}
}
}

Xamarin.Forms Android Custom Animation not working even though it works on UWP. Ideas?

I have a custom animation that expands a Cell inside a TableView. I also refresh the tableView size before/after the animation as appropriate. This works on UWP, and I don't have a Mac yet, so I can't test it on IOS.
This doesn't work at all on Android. It does not refresh the tableRoot to update the size of it, nor does it seem to play the expanding animations.
I say the second part, because when I lock the tableview to the full size it doesn't play.
The animation itself is pretty straight forward:
private void AnimateLinearRowAppear(VisualElement visual, double targetHeight = 40, TableRoot _tableRoot, bool visible)
{
visual.IsVisible = visible;
TableRootRefresher(_tableRoot);
Animation _animation;
_animation = new Animation(
(d) => visual.HeightRequest = d, 0, targetHeight);
_animation.Commit(visual, "The Animation", 16, 250, Easing.Linear, (target, b) =>
{
_animation = null;
});
}
Any one have any ideas as to why it doesn't work on Android?
Unless I'm mistaken this is for cross platform development.
Do I have to write an interface and call each of my animations in a platform specific way?
Oh, this is the latest version of Xamarin 2.3.4.270.
Thanks for any help in advance!
I'm calling the animation from an event such as a specific picker selection or switch.
Switch.toggled += (sender, e) =>
{
AnimationChooser(celltochange,switch.toggle, tableRootCellIsPartOf);
}
AnimationChooser decides if we're playing the appearing or disappearing animation and is working correctly.
private void AnimationChooser(Cell celltoChange, bool stateOfSwitch, TableRoot _tableRoot)
{
var visual = (celltoChange as ViewCell).View
if(visual.IsVisible && stateOfSwitch)
{ AnimateLinearRowDissappear(visual, 40 ,_tableRoot, stateOfSwitch);}
else AnimateLinearRowAppear(visual,40,_tableRoot,stateOfSwitch);
}
TableRootRefresher(TableRoot _tableRoot)
{
Var tempRoot = _tableRoot;
_tableRoot = null;
_tableRoot = tempRoot:
}
The XAML:
<TableView>
<TableRoot x:Name="root">
<TableSection>
<SwitchCell x:Name="switch"/>
<EntryCell x:Name="toAppear" HieghtRequest="0" IsVisible="False"/>
</TableSection>
</TableRoot>
</TableView>
Here is a sample project.
I have used the below codesnippet animation working fine for me.
//animate changed the opacity from 1 to 0.
//XFtabbody is my UI elements
var animation = new Animation(v => XFTabBody.Opacity = v, 1, 0);
Action<double, bool> finished = animate;
animation.Commit(XFTabBody, "aa", 0, 100, Easing.Linear, finished);
void animate(double d, bool b)
{
XFTabBody.Children.Clear();
}

Get touch Location and tap on monogame

I'm writing a game in monogame for Android for my school project but I can't get touch location to do one simple tap action, I search but I can't find anything useful to do that. My game is a pipe puzzle game that I want when player touch a pipe it rotate but as I say I can't do this. this is my code for keyboard input.
KeyboardState newState = Keyboard.GetState();
if (oldState.IsKeyUp(Keys.Space) && newState.IsKeyDown(Keys.Space))
{
angle1 += (float)Math.PI / 2.0f;
}
oldState = newState;
You'll want to add a check for the following:
TouchCollection touchCollection = TouchPanel.GetState();
if (touchCollection.Count > 0)
{
//Only Fire Select Once it's been released
if (touchCollection[0].State == TouchLocationState.Moved || touchCollection[0].State == TouchLocationState.Pressed)
{
Console.WriteLine(touchCollection[0].Position);
{
}
You can then do what ever you want with the Position variable.

Is calling a script in unityscript slow?

My enemy script is linked to a prefab and being instantiated by my main script.
It kills enemies in a random order (I am jumping on them and some are not dying, not what I want).
(what I am trying to achieve is an enemy to die when I jump on its head and play a death animation.
So from this enemy script I call the other script jump <-- which is linked to my player script and get the jump Boolean value. Could the processing of jump be to slow? I need help
I tried everything)
it works but only on certain enemies any ideas why? Thanks community.
Can anyone help me find a better method?
Could someone help me maybe find if the Players y => an amount to change jump var on the enemy
Just had a perfect run, whats wrong with this its working then not then it is partly working
If I add audio, it doesn't work.
#pragma strict
var enemy : GameObject;
var speed : float = 1.0;
var enemanim : Animator;
var isdying : boolean = false;
private var other : main;
var playerhit: boolean = false;
function Start () {
other = GameObject.FindWithTag("Player").GetComponent("main");
this.transform.position.x = 8.325;
this.transform.position.y = -1.3;
enemanim = GetComponent(Animator);
enemanim.SetFloat("isdead",0);
}
function OnCollisionEnter2D(coll: Collision2D) {
if(coll.gameObject.CompareTag("distroy")){
Destroy(enemy.gameObject);
}
if(coll.gameObject.CompareTag("Player")){
playerhit=true;
}
}
function Update () {
if(other.jumped === true && playerhit==true){ *****the jumped i need
enemanim.SetFloat("isdead",1);
}
}
function FixedUpdate(){
this.transform.Translate(Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0));
this.rigidbody2D.velocity = Vector2(-5,0);
}
if(other.jumped === true && playerhit==true)
Is wrong.
It should be:
if(other.jumped == true && playerhit==true)
All 3 languages used by Unity, C#, UnityScript, and Boo, are compiled into the same IL byte code at the end. However, there are cases where UnityScript has some overhead as Unity does things in the background. One of these is that it does wrapping of access to members of built-in struct-properties like transform.position.
I prefer C#, I think it is better.

Categories

Resources