-
Notifications
You must be signed in to change notification settings - Fork 21
/
SpriteButton.js
118 lines (96 loc) · 3.18 KB
/
SpriteButton.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var Sburb = (function(Sburb){
///////////////////////////////////////////
//SpriteButton class
///////////////////////////////////////////
//constructor
Sburb.SpriteButton = function(name,x,y,width,height,sheet,action){
Sburb.Sprite.call(this,name,x,y,width,height);
this.pressed = false;
this.mousePressed = false;
this.clicked = false;
this.action?action:null;
for(var i=0;i<(sheet.width/this.width)*(sheet.height/this.height);i++){
this.addAnimation(new Sburb.Animation("state"+i,sheet,0,0,width,height,i,1,1000));
}
this.startAnimation("state0");
}
Sburb.SpriteButton.prototype = new Sburb.Sprite();
Sburb.SpriteButton.prototype.update = function(){
Sburb.Sprite.prototype.update.call(this);
this.updateMouse();
}
//update button in relation to mouse state
Sburb.SpriteButton.prototype.updateMouse = function(){
var x = Sburb.Mouse.x;
var y = Sburb.Mouse.y;
var mouseDown = Sburb.Mouse.down;
this.clicked = false;
if(this.hitsPoint(x-this.width/2,y-this.height/2)){
Sburb.Stage.style.cursor = "pointer";
}
if(mouseDown){
if(!this.mousePressed){
this.mousePressed = true;
if(this.hitsPoint(x-this.width/2,y-this.height/2)){
this.pressed = true;
}
}
}else{
if(this.pressed){
if(this.hitsPoint(x-this.width/2,y-this.height/2)){
this.clicked = true;
var nextState = "state"+(parseInt(this.animation.name.substring(5,this.animation.name.length))+1);
if(this.animations[nextState]){
this.startAnimation(nextState);
}else{
this.startAnimation("state0");
}
}
}
this.pressed = false;
this.mousePressed = false;
}
if(this.clicked && this.action){
Sburb.performAction(this.action);
}
}
Sburb.SpriteButton.prototype.setState = function(state){
this.startAnimation("state"+state);
}
//serialize this SpriteButton to XML
Sburb.SpriteButton.prototype.serialize = function(output){
output = output.concat("\n<spritebutton name='"+this.name+
(this.x?"' x='"+this.x:"")+
(this.y?"' y='"+this.y:"")+
"' width='"+this.width+
"' height='"+this.height+
"' sheet='"+this.animation.sheet.name+
"' >");
if(this.action){
output = this.action.serialize(output);
}
output = output.concat("</spritebutton>");
return output;
}
///////////////////////////////////////////////
//Related Utility Functions
///////////////////////////////////////////////
//Parse a SpriteButton from XML
Sburb.parseSpriteButton = function(button){
var attributes = button.attributes;
var sheet = Sburb.assets[attributes.getNamedItem("sheet").value];
var newButton = new Sburb.SpriteButton(attributes.getNamedItem("name").value,
attributes.getNamedItem("x")?parseInt(attributes.getNamedItem("x").value):0,
attributes.getNamedItem("y")?parseInt(attributes.getNamedItem("y").value):0,
attributes.getNamedItem("width")?parseInt(attributes.getNamedItem("width").value):sheet.width,
attributes.getNamedItem("width")?parseInt(attributes.getNamedItem("height").value):sheet.height,
sheet);
var curAction = button.getElementsByTagName("action");
if(curAction.length>0){
var newAction = Sburb.parseAction(curAction[0]);
newButton.action = newAction;
}
return newButton;
}
return Sburb;
})(Sburb || {});