/* Script for creating a simple Button This script is designed to change Level on mouse click. Place the script on the button object When selecting colour for the buttons ensure that you also set the A value to 1.0 as it defaults to 0 so will be invisible. You can change the actions within the clickedstate function to modify the script to do other things. */ class Button : ZilchComponent { //Variable for level to change to if you use this button for level changing. [Property] var Level1 : Level = null; //The Reactive component is required for this component [Dependency] var Reactive : Reactive = null; //The default sprite when the button is not hovered over or selected [Property] var DefaultSprite : SpriteSource = null; //Color for the Default Sprite [Property] var DefaultColor : Real4 = Real4(0,0,0,0); //The sprite to use when hovering over the button with mouse [Property] var HoveredSprite : SpriteSource = null; //Color for the Hovered Sprite [Property] var HoveredColor : Real4 = Real4(0,0,0,0); //The sprite source to use when the button is clicked [Property] var ClickedSprite : SpriteSource = null; //Reference to restart sprite text object [Property] var ButtonTextPath : CogPath = null; function Initialize(init : CogInitializer) { //Connecting to all the mouse events to use in the UI Zero.Connect(this.Owner, Events.MouseEnter, this.OnMouseEnter); Zero.Connect(this.Owner, Events.MouseExit, this.OnMouseExit); Zero.Connect(this.Owner, Events.MouseDown, this.OnMouseDown); } //Responds to the mouse entering the boundaries of the button function OnMouseEnter(event : ViewportMouseEvent) { this.HoveredState(); } //Responds to the mouse exiting the boundaries of the button function OnMouseExit(event : ViewportMouseEvent) { this.DefaultState(); } //Responds to the clicking of a button on the mouse function OnMouseDown(event : ViewportMouseEvent) { //This will load the level chosen as the property at the top of the script var space = this.ClickedState(this.Level1); } //Returns button to its default sprite source function DefaultState() { this.Owner.Sprite.SpriteSource = this.DefaultSprite; this.Owner.Sprite.Color = this.DefaultColor; } //Changes button's sprite source to the value of HoveredSprite function HoveredState() { this.Owner.Sprite.SpriteSource = this.HoveredSprite; this.Owner.Sprite.Color = this.HoveredColor; } //Changes button's sprite source to the value of ClickedSprite, //restarts the game, and sets visibility properties function ClickedState(level : Level) : Space { //Enter all actions in this function: Level Change, Item Selection, Game Start etc. //This line creates a new space for the new level var space = this.GameSession.CreateSpace(Archetype.Find("Space")); //This loads a new level space.LoadLevel(this.Level1); //If you choose to pass variables from one level to another //Like health, points etc. this is where you would do it. //This destroys the old space to clean up the game this.Space.Destroy(); //This returns the new space with all new variables created. return space; //Set button and text visibility to false, //Only use these lines if you want the button to disappear //and you will stay on the same level: /* this.Owner.Sprite.Visible = false; this.ButtonTextPath.Cog.SpriteText.Visible = false; */ } }