Skip to content

Instantly share code, notes, and snippets.

@RafaelOliveira
Last active July 28, 2016 19:40
Show Gist options
  • Save RafaelOliveira/6127f76a83c8869c9856 to your computer and use it in GitHub Desktop.
Save RafaelOliveira/6127f76a83c8869c9856 to your computer and use it in GitHub Desktop.
A button that call a function when clicked
class Button
{
public var x:Int;
public var y:Int;
public var width:Int;
public var height:Int;
public var onClick:Void->Void;
public function new(x:Int, y:Int, width:Int, height:Int)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public function checkMouseDown(x:Int, y:Int):Bool
{
if (x >= this.x && x <= this.x + this.width && y >= this.y && y <= this.y + this.height)
{
if (this.onClick != null)
onClick();
return true;
}
return false;
}
}
class Project
{
var stateButton:Button;
var state:String;
public function new()
{
stateButton = new Button(10, 10, 100, 50);
stateButton.onClick = changeState;
}
public function changeState()
{
state = 'menu';
}
public function onMouseDown(button:Int, x:Int, y:Int)
{
if (button == 0)
stateButton.checkMouseDown(x, y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment