Last active
August 29, 2015 14:10
-
-
Save bitchwhocodes/8750741951f5ee1d9709 to your computer and use it in GitHub Desktop.
Unity3d JsonObject WWW to call SparkCore API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Net; | |
using System.IO; | |
public class Plane : MonoBehaviour | |
{ | |
public Vector2 jumpForce = new Vector2 (0, 300); | |
private WWW www; | |
// Use this for initialization | |
void Start () | |
{ | |
InvokeRepeating ("GetSparkData", 0.2f, 0.2f); | |
} | |
void GetSparkData () | |
{ | |
www = new WWW ("https://api.spark.io/v1/devices/{YOUR_DEVICE_ID}/pressure?access_token={YOUR_ACCESS_TOKEN}); | |
// Wait for download to complete | |
StartCoroutine (WaitForRequest (www)); | |
} | |
IEnumerator WaitForRequest (WWW www) | |
{ | |
yield return www; | |
// check for errors | |
if (www.error == null) { | |
JSONObject jo = new JSONObject (www.text); | |
string result = jo ["result"].ToString(); | |
int value = int.Parse (result); | |
if (value < 20) { | |
doJump (); | |
} | |
} | |
} | |
void doJump () | |
{ | |
rigidbody2D.velocity = Vector2.zero; | |
rigidbody2D.AddForce (jumpForce); | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
} | |
void OnCollisionEnter2D (Collision2D other) | |
{ | |
Debug.Log ("collision enter"); | |
Die (); | |
} | |
void Die () | |
{ | |
// we are restarting this game by reloading it | |
Application.LoadLevel (Application.loadedLevel); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment