var countDownObj:GameObject; // カウントダウンテキスト
var mainCamera:GameObject; // カメラ
var gameStatus:int; // ゲームステータス 0:wait 1:play
var clickStartObject:GameObject; // クリックし始めたオブジェクト
var prevObject:GameObject; // 前のオブジェクト
var removeObjectList:Array; // 削除するオブジェクトのリスト
var lineObject:GameObject; // LineRendererオブジェクト
if(Input.GetMouseButton(0) && clickStartObject == null){
removeObjectList = new Array();
var hit:RaycastHit2D =Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hit.collider != null) {
if(hit.collider.gameObject.name.IndexOf("Chara") != -1){
clickStartObject = hit.collider.gameObject;
prevObject = clickStartObject;
removeObjectList.push(prevObject); // 削除候補のリストに追加
var charaTexture:tk2dSprite = prevObject.GetComponent(tk2dSprite);
charaTexture.color.a = 0.5;
}else if(clickStartObject && Input.GetMouseButtonUp(0)){
Destroy(lineObject); // linerendererオブジェクト削除
if(removeObjectList.length >= 3){
var dropCount:int = removeObjectList.length;
for(var i:int = 0; i < removeObjectList.length; i++){
Destroy(removeObjectList[i]);
removeObjectList = new Array();
// 3個以上つながっていない場合は色を通常に戻す
for(i = 0; i < removeObjectList.length; i++){
var obj:GameObject = removeObjectList[i];
obj.GetComponent(tk2dSprite).color.a = 1;
prevObject = clickStartObject = null;
}else if(clickStartObject){
hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hit.collider != null) {
if(hit.collider.gameObject.name == prevObject.name){
if(prevObject != hit.collider.gameObject){
var dist:float = Vector2.Distance(prevObject.transform.position, hit.collider.gameObject.transform.position);
// 前のオブジェクトとの距離が一定範囲以下の場合つながっているとする
// 既に追加されたオブジェクトではないかチェック
for(i = 0; i < removeObjectList.length; i++){
obj = removeObjectList[i];
if(obj == hit.collider.gameObject){
// 既に追加されていないオブジェクトであれば追加する
prevObject = hit.collider.gameObject;
charaTexture = prevObject.GetComponent(tk2dSprite);
charaTexture.color.a = 0.5;
removeObjectList.push(prevObject);
// 既に追加されているオブジェクトの場合、そこまで線を戻す
for(i = idExistNo + 1; i < removeObjectList.length; i++){
obj = removeObjectList[i];
obj.GetComponent(tk2dSprite).color.a = 1;
removeObjectList.splice(idExistNo + 1, removeObjectList.length - idExistNo - 1);
prevObject = removeObjectList[removeObjectList.length - 1];
lineObject = Instantiate(Resources.Load("Prefabs/LineRenderer"));
var lineRenderer:LineRenderer = lineObject.GetComponent(LineRenderer);
lineRenderer.SetVertexCount(removeObjectList.length);
lineRenderer.SetWidth(0.1,0.1);
for(i = 0; i < removeObjectList.length; i++){
obj = removeObjectList[i];
lineRenderer.SetPosition(i, obj.transform.position);
countDownObj.SetActive(false);
// キャラクターをcountの数だけ上から落とす
private function dropChara(count:int){
for(var i:int = 0; i < count; i++){
var chara:GameObject = Instantiate(Resources.Load("Prefabs/Chara"));
chara.transform.position.x = Random.Range(-2.0, 2.0);
yield WaitForSeconds(0.05);
var charaTexture:tk2dSprite = chara.GetComponent(tk2dSprite);
var spriteId:int = Random.Range(0,5);
chara.name = "Chara" + spriteId;
charaTexture.spriteId = spriteId;
private function countDown(){
var countDownText:tk2dTextMesh = countDownObj.GetComponent(tk2dTextMesh);
countDownObj.SetActive(true);
countDownText.text = 3 + "";
iTween.ScaleTo(countDownObj, iTween.Hash("x", 1.2,"y", 1.2, "delay", 0, "time", 0.3, "easeType", "easeInOutQuad"));
iTween.ScaleTo(countDownObj, iTween.Hash("x", 0.8,"y", 0.8, "delay", 0.3, "time", 0.3, "easeType", "easeInOutQuad"));
iTween.ScaleTo(countDownObj, iTween.Hash("x", 1.0,"y", 1.0, "delay", 0.3, "time", 0.3, "easeType", "easeInOutQuad"));
countDownText.text = 2 + "";
iTween.ScaleTo(countDownObj, iTween.Hash("x", 1.2,"y", 1.2, "delay", 0, "time", 0.3, "easeType", "easeInOutQuad"));
iTween.ScaleTo(countDownObj, iTween.Hash("x", 0.8,"y", 0.8, "delay", 0.3, "time", 0.3, "easeType", "easeInOutQuad"));
iTween.ScaleTo(countDownObj, iTween.Hash("x", 1.0,"y", 1.0, "delay", 0.3, "time", 0.3, "easeType", "easeInOutQuad"));
countDownText.text = 1 + "";
iTween.ScaleTo(countDownObj, iTween.Hash("x", 1.2,"y", 1.2, "delay", 0, "time", 0.3, "easeType", "easeInOutQuad"));
iTween.ScaleTo(countDownObj, iTween.Hash("x", 0.8,"y", 0.8, "delay", 0.3, "time", 0.3, "easeType", "easeInOutQuad"));
iTween.ScaleTo(countDownObj, iTween.Hash("x", 1.0,"y", 1.0, "delay", 0.3, "time", 0.3, "easeType", "easeInOutQuad"));
countDownText.text = "Start";
iTween.ScaleTo(countDownObj, iTween.Hash("x", 1.4,"y", 1.4, "delay", 0, "time", 0.5, "easeType", "easeInOutQuad"));
iTween.ScaleTo(countDownObj, iTween.Hash("x", 0,"y", 0, "delay", 0.5, "time", 0.5, "easeType", "easeInOutQuad"));
countDownObj.SetActive(false);