Hello Guest

Author Topic: Instantiate sprite & SetSprite()  (Read 3641 times)

DemiGoth

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 45
    • View Profile
Instantiate sprite & SetSprite()
« on: February 19, 2014, 08:14:10 am »
I have a problem with the game menu I'm creating.

To create a menu, I instantiate the menu sprites to the screen from the main game loop. Attached to each sprite I have the following code.

Code: [Select]
using UnityEngine;
using System.Collections;

public class OptionMenu : MonoBehaviour
{
public tk2dCamera myCamera;
static private tk2dSprite myObject;
byte MyMenuOption;

// Use this for initialization
void Start ()
{
Vector3 StartPos;

myObject=GetComponent<tk2dSprite>();
StartPos=myObject.transform.position;
if(StartPos.x==myCamera.nativeResolutionWidth/2 && StartPos.y==myCamera.nativeResolutionHeight-50)
{
// TetraGems logo
myObject.SetSprite(2);
MyMenuOption=1;
}
else if(StartPos.x==myCamera.nativeResolutionWidth/2 && StartPos.y==100)
{
// Start button
myObject.SetSprite(3);
MyMenuOption=2;
}
else if(StartPos.x==myCamera.nativeResolutionWidth-75 && StartPos.y==0)
{
// Exit button
myObject.SetSprite(6);
MyMenuOption=3;
}
else if(StartPos.x==125 && StartPos.y==myCamera.nativeResolutionHeight/2+100)
{
// Next Blocks button
myObject.SetSprite(8);
myObject.transform.Rotate(0f,0f,350);
MyMenuOption=4;
}
else if(StartPos.x==125 && StartPos.y==myCamera.nativeResolutionHeight/2-50)
{
// Next Gems button
myObject.SetSprite(10);
myObject.transform.Rotate(0f,0f,10);
MyMenuOption=5;
}
else if(StartPos.x==myCamera.nativeResolutionWidth-125 && StartPos.y==myCamera.nativeResolutionHeight/2+100)
{
// Music button
myObject.SetSprite(12);
myObject.transform.Rotate(0f,0f,350);
MyMenuOption=6;
}
else if(StartPos.x==myCamera.nativeResolutionWidth-125 && StartPos.y==myCamera.nativeResolutionHeight/2-50)
{
// Sound Effects button
myObject.SetSprite(14);
myObject.transform.Rotate(0f,0f,10);
MyMenuOption=7;
}
else if(StartPos.x==75 && StartPos.y==0)
{
// Credits button
myObject.SetSprite(15);
MyMenuOption=8;
}

if(MyMenuOption>2)
{
myObject.scale=new Vector3(1.5f,1.5f,1.5f);
}
else if (MyMenuOption==2)
{
myObject.scale=new Vector3(2f,2f,2f);
}
}

// Update is called once per frame
void Update ()
{
if(MyMenuOption>0 && MainGame.GameState>=20)
{
// Game no longer in menu mode - destroy object
DestroyImmediate(gameObject);
}
}

void OnMouseDown()
{
switch(MyMenuOption)
{
case 2: // Play
MainGame.GameState=20;
break;
case 3: // Exit
Application.Quit();
break;
case 4: // Next Blocks
myObject.SetSprite(9);
break;
case 5: // Next Gems
myObject.SetSprite(11);
break;
case 6: // Music
myObject.SetSprite(13);
break;
case 7: // Sound Effects
myObject.SetSprite(15);
break;
case 8:
break;
}
}
}

Each menu option ends up on their appropriate location. But when I click for MyMenuOption 4 trough 7, the sprite for MyMenuOption 8 (not yet coded :p) changes.

How can I let each sprite change their own instead of the last one I instantiated?

DemiGoth

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 45
    • View Profile
Re: Instantiate sprite & SetSprite()
« Reply #1 on: February 19, 2014, 08:19:34 am »
Okay, NVM... Silly me (once again). I should make the myObject public and drop the original sprite there. Instantiating the new ones will automatically get the myObject from the instantiated sprite  :-[