-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathButton.java
More file actions
128 lines (113 loc) · 4.04 KB
/
Copy pathButton.java
File metadata and controls
128 lines (113 loc) · 4.04 KB
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
119
120
121
122
123
124
125
126
127
128
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Buttons are images that change appearance when the cursor hovers over them
* and may do things when clicked. To use them, check if the Button has been clicked and
* do whatever function needed.
*
* @author Angela Wang, Stephanie Xia
* @version April 2025
*/
public class Button extends Actor
{
private GreenfootImage image, hoverImage;
private GreenfootSound[] clicks;
private String text;
private Font font = new Font(40);
private Cursor cursor;
private int curIndex;
/**
* Button constructor - pass user cursor and direction of button (for image setting purposes).
* We are aware that the L/R issue could have been solved by flipping the image, but
* were too short on time to fix.
*
* @param cursor User's cursor, as created by the World the Button is in
* @param isLeft Boolean: if true, button is left-facing (used to determine which button image)
*/
public Button(Cursor cursor, boolean isLeft){
if (isLeft == true)
{
image = new GreenfootImage ("SettingsArrowL.png");
setImage(image);
hoverImage = new GreenfootImage("SettingsArrowHoverL.png");
image.scale(80, 100);
hoverImage.scale(80,100);
}
else
{
image = new GreenfootImage ("SettingsArrowR.png");
setImage(image);
hoverImage = new GreenfootImage("SettingsArrowHoverR.png");
image.scale(80, 100);
hoverImage.scale(80, 100);
}
this.cursor = cursor;
}
public Button(Cursor cursor, boolean isShopWorld, int price){
if (price == 10)
{
image = new GreenfootImage ("UpgradeButton10.png");
setImage(image);
hoverImage = new GreenfootImage("UpgradeButtonPD10.png");
image.scale(230,76);
hoverImage.scale(230,76);
}
else if(price == 20)
{
image = new GreenfootImage ("UpgradeButton20.png");
setImage(image);
hoverImage = new GreenfootImage("UpgradeButtonPD20.png");
image.scale(230,76);
hoverImage.scale(230,76);
}
else
{
image = new GreenfootImage ("BuyButton15.png");
setImage(image);
hoverImage = new GreenfootImage("BuyButtonPD15.png");
image.scale(190,60);
hoverImage.scale(190,60);
}
this.cursor = cursor;
//array of click sounds --> can click quickly
clicks = new GreenfootSound[5];
for (int i = 0; i < 5; i++){
clicks[i] = new GreenfootSound("click.wav");
}
curIndex = 0;
}
public Button(Cursor cursor, String imageBase){
image = new GreenfootImage(imageBase + ".png");
hoverImage = new GreenfootImage(imageBase + "_hover.png");
this.cursor = cursor;
clicks = new GreenfootSound[5];
for (int i = 0; i < 5; i++){
clicks[i] = new GreenfootSound("click.wav");
}
curIndex = 0;
}
/**
* Act checks for if the cursor is hovering above the button and changes the image if yes.
* If the button is clicked, a click sound is played.
* @return void
*/
public void act()
{
//if the cursor is hovering over the button, change the image
if (cursor.getHoveredActors().contains(this)){
setImage(hoverImage);
// not working?!?!?
// if button is clicked, play sound
// if (Greenfoot.mousePressed(this)){
// curIndex++;
// //if current index exceeds maximum possible index, reset to 0
// if (curIndex > clicks.length - 1){
// curIndex = 0;
// }
// clicks[curIndex].play();
// }
} else {
//if not hovered over, normal image
setImage(image);
}
}
}