- Home /
Question is off-topic or not relevant
Can someone convert this into javascript? :)
I need help, I tried to convert this into javascript as my whole project is in javscript. I got help from someone else on how to do a swiping script and it worked perfectly. However, I need to call variables from this script from another, I'll post the c sharp script first and then my failed attempt second. The second one has a few things added to it.
using UnityEngine;
using System.Collections;
public class Swipe : MonoBehaviour {
void Start ()
{
}
Vector2 StartPos;
int SwipeID = -1;
float minMovement = 20.0f;
public GameObject gridline;
Vector3 temp;
void Update ()
{
temp = gridline.transform.position;
foreach (var T in Input.touches) {
var P = T.position;
if (T.phase == TouchPhase.Began && SwipeID == -1) {
SwipeID = T.fingerId;
StartPos = P;
} else if (T.fingerId == SwipeID) {
var delta = P - StartPos;
if (T.phase == TouchPhase.Moved && delta.magnitude > minMovement) {
SwipeID = -1;
if (Mathf.Abs (delta.x) > Mathf.Abs (delta.y)) {
if (delta.x > 0) {
//swipe right
if(temp.x <= 60.28 && temp.x >= 56.28)
{
if(temp.x != 60.28)
{
temp.x = temp.x + 1;
gridline.transform.position = temp;
}
}
} else {
//swipe left
if(temp.x <= 60.28 && temp.x >= 56.28)
{
if(temp.x != 56.28)
{
temp.x = temp.x - 1;
gridline.transform.position = temp;
}
}
}
}
else {
if (delta.y > 0) {
Debug.Log ("Swipe Up Found");
} else {
Debug.Log ("Swipe Down Found");
}
}
} else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
SwipeID = -1;
}
}
}
}
//=====================================javascript=================================//
wipeID : int = -1;
var minMovement : float = 20.0f;
public var gridline : GameObject;
var temp : Vector3;
var swiping : boolean = false;
var whichdot : int = 1;
var dot1 : GameObject;
var dot2 : GameObject;
var dot3 : GameObject;
var dot4 : GameObject;
function Update ()
{
(whichdot == 1)
{
dot1.GetComponent(GUITexture).enabled = true;
dot2.GetComponent(GUITexture).enabled = false;
dot3.GetComponent(GUITexture).enabled = false;
dot4.GetComponent(GUITexture).enabled = false;
}
if(whichdot == 2)
{
dot2.GetComponent(GUITexture).enabled = true;
dot1.GetComponent(GUITexture).enabled = false;
dot3.GetComponent(GUITexture).enabled = false;
dot4.GetComponent(GUITexture).enabled = false;
}
if(whichdot == 3)
{
dot3.GetComponent(GUITexture).enabled = true;
dot2.GetComponent(GUITexture).enabled = false;
dot1.GetComponent(GUITexture).enabled = false;
dot4.GetComponent(GUITexture).enabled = false;
}
if(whichdot == 4)
{
dot4.GetComponent(GUITexture).enabled = true;
dot2.GetComponent(GUITexture).enabled = false;
dot3.GetComponent(GUITexture).enabled = false;
dot1.GetComponent(GUITexture).enabled = false;
}
temp = gridline.transform.position;
for (var T in Input.touches) {
var P = T.position;
if (T.phase == TouchPhase.Began && SwipeID == -1) {
SwipeID = T.fingerId;
StartPos = P;
} else if (T.fingerId == SwipeID) {
var delta = P - StartPos;
if (T.phase == TouchPhase.Moved && delta.magnitude > minMovement) {
SwipeID = -1;
if (Mathf.Abs (delta.x) > Mathf.Abs (delta.y)) {
if (delta.x > 0) {
//swipe right
if(temp.x <= 60.28 && temp.x >= 57.28)
{
swiping = true;
temp.x = temp.x + 1;
gridline.transform.position = temp;
whichdot = whichdot -1;
waitalittle();
}
} else {
//swipe left
if(temp.x <= 60.28 && temp.x >= 57.28)
{
swiping = true;
temp.x = temp.x - 1;
gridline.transform.position = temp;
whichdot = whichdot +1;
waitalittle();
}
}
}
else {
if (delta.y > 0) {
Debug.Log ("Swipe Up Found");
} else {
Debug.Log ("Swipe Down Found");
}
}
} else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
SwipeID = -1;
}
}
}
function waitalittle()
{
yield WaitForSeconds (1);
swiping = false;
}
}
whats wrong the the javascript? do you get compilation errors or other oddities?
No compilition errors, just when I compile and run it on my android, it no longer works.
This isn't really the place; "Please do work for me" isn't the purpose of this site. It's for asking specific development questions.
That is why I put my attempt below??? so people could help me solve the problem...
If you went on StackOverflow and asked someone to convert a script to a different language, it would also be closed. This site is basically StackOverflow for Unity, so please use it in the same way. I'd suggest posting in the forums for this sort of thing.
Follow this Question
Related Questions
How can I change this C# code into Javascript? 0 Answers
Need help converting javascript to c# script 2 Answers
Help converting C# to Javascript. 1 Answer
How to make this line work in C#? 1 Answer
Javascript int to float 1 Answer