What does my Processing error, "Debugger halted" mean? - java

I'm watching The Coding Train's Processing Tutorials. In 5.4, he makes this line of code that is supposed to have an ellipse then move when clicked, but stop when clicked again. But for me it just says Debugger Halted. Here is my code:
float x = 100;
boolean going = false;
void setup() {
size(400, 300);
}
void draw() {
background(0);
fill(255);
ellipse(x, 150, 24, 24);
if (going) {
x = x + 2;
}
}
void mousePressed() {
going = true;
}

My guess is you're running in debug mode with a break point set.
You probably want to disable debug mode by clicking the butterfly button in the upper-right corner, or you want to find the break point and remove it by clicking the line number with a diamond icon.

Related

How to rotate this arm upwards?

I am learning Java with the program named Processing.
However, I cannot understand why my code do not work appropriately.
I made a arm with this problem which should be rotated to upside or downside when I hold the mouse button, but it just works when I hold the left button on my mouse, and it does not work when I hold the right button.
The problem is I do not know the problem of my code as follows.
float angle=0;
float angleDirection=1;
float speed=0.005;
void setup(){
size(800,600);
}
void draw(){
background(255,255,255);
stroke(20,20,255);
translate(400,300);
rotate(angle);
strokeWeight(18);
line(0,0,140,0);
pushMatrix();
translate(140,0);
rotate(angle*2.0);
strokeWeight(14);
line(0,0,100,0);
translate(100,0);
rotate(angle*2.5);
strokeWeight(10);
line(0,0,60,0);
popMatrix();
rotate(-angle*2.0);
strokeWeight(18);
line(0,0,-140,0);
translate(-140,0);
rotate(-angle*2.0);
strokeWeight(14);
line(0,0,-100,0);
translate(-100,0);
rotate(-angle*2.5);
strokeWeight(10);
line(0,0,-60,0);
if(mousePressed){
if(mouseButton==LEFT){
angle=angle+speed*angleDirection;
if((angle>QUARTER_PI)||(angle<0)){
angle=QUARTER_PI;
}
if(mouseButton==RIGHT){
angle=angle+speed*angleDirection;
if((angle>QUARTER_PI)||(angle<0)){
angleDirection=-angleDirection;
angle=QUARTER_PI;
}
}
}
}
}
You're almost there, but accidentally made a logical error: the right button condition is nested inside the left button condition. Since the mouseButton can only be either left or right (but not both), the right button condition will never trigger. Simply move it outside of the left button condition:
if (mousePressed) {
if (mouseButton==LEFT) {
angle=angle+speed*angleDirection;
if ((angle>QUARTER_PI)||(angle<0)) {
angle=QUARTER_PI;
}
}
if (mouseButton==RIGHT) {
angle=angle+speed*angleDirection;
if ((angle>QUARTER_PI)||(angle<0)) {
angleDirection=-angleDirection;
angle=QUARTER_PI;
}
}
}
(Edit > Auto Format (Ctrl+T / CMD + T) will make it easier to spot {} issues)
Have fun learning!

Processing: How to make Sphere that rotates when mouseClicked?

In my program, I want a sphere to represent a globe. I want the user to press down the mouse1 button and rotate the sphere. When the user is not pressing the mouse1 down, the sphere should just be staying still regardless of how the mouse is moving. In simple terms, I want a sphere to rotate only when the mouse is pressed. Here is my code for the sphere rotating.
float lockXRotation;
float lockYRotation;
void setup() {
size(1000,700,P3D); //reference
}
void draw() {
mouseAction();
fill(200, 0, 160);
if(mousePressed == false){
rotateX(lockXRotation);
rotateY(lockYRotation);
}
sphere(100);
}
void mouseAction () {
translate(500, 350, 0); //reference starts here'
if(mousePressed){
rotateX(mouseY * -0.01);
rotateY(mouseX * -0.01); //ends here
lockXRotation = 0;//what should i put so it will stay still?
lockYRotation = 0;
}
}
I have tried mouseX * -0.01 for lockXRotation but it offsets by the distance of the x to the origin. Is there a function to find the rotation an object is in?
Okay, so you want the sphere's location to lock when the user presses the mouse.
I took a look at your code and ran it on Processing, I believe that your lockXRotation is not doing what it is suppose to.
If you look at lines 12 - 15, where it says if (mousePressed == false), then it should rotateX(lockXRotation), those lines don't belong in the draw function, but if you put them in the mouseAction function, that might solve the problem.

Platforms in a class not moving when called in

For a university course I'm making a game with a friend. The general idea is that we have some platforms moving from right to left and each time one goes offscreen it is generated at a random x and y position on the right (within some limits). There will be a little sprite that jumps from platform to platform.
We have reached a problem we're not sure how to solve. We have all the right code and everything but the platforms just won't move. They should move to the left at a constant speed of -4 pixels per frame (rectVelocity).
We cannot get them to move, though; they are static on the screen at the position each one is initially called in at.
This is the code as condensed as I can make it:
Platforms [] mainPlats;
void setup() {
size(750, 400);
mainPlats = new Platforms[3];
}
void draw() {
level();
}
void level() {
//This is the code for the first platform
mainPlats[0] = new Platforms(200, 200, 100, 15); //These values need to be set inside the class so that
//they aren't constantly overwriting the movement variables in the class
mainPlats[0].displayPlat();
mainPlats[0].platTransition();
//This is the code for the second platform
mainPlats[1] = new Platforms(420, 300, 100, 15);
mainPlats[1].displayPlat();
mainPlats[1].platTransition();
//This is the code for the third platform
mainPlats[2] = new Platforms(570, 350, 100, 15);
mainPlats[2].displayPlat();
mainPlats[2].platTransition();
}
class Platforms {
PImage platform;
int rectX, rectY, rectWidth, rectHeight;
int rectVelocity = 4;
Platforms(int x, int y, int w, int h) {
rectX = x;
rectY = y;
// rectX = (int(random(600, 800))); //Tried to randomise start position, failed hilariously
//rectY = (int(random(150, 350)));
rectWidth = w;
rectHeight = h;
}
void displayPlat() {
platform = loadImage ("images/tiles.png");
//imageMode(CENTER);
image(platform, rectX, rectY, 100, 15); //rectangle platforms replaced with images
}
void platMove() {
rectX -= rectVelocity;
}
void platTransition() {
if (rectX < -200) {
rectX = (int(random(700, 1000)));
rectY = (int(random(150, 350)));
}
}
}
From the draw() function, you call your level() function, which initializes your Platform array every single frame.
This means that you create new Platforms at their starting positions every frame. You never see the platforms move, because as soon as you do move them, you replace them with new platforms at the starting positions again.
So step one is to move their initialization out of the level() function and only call them once, at the beginning of your sketch- the setup() function would be one place you could put them.
Your other problem is that you never actually call the platMove() function. So step two is to make sure you call that function.
A solution might look something like this:
Platforms [] mainPlats;
void setup() {
size(750, 400);
mainPlats = new Platforms[3];
mainPlats[0] = new Platforms(200, 200, 100, 15);
mainPlats[1] = new Platforms(420, 300, 100, 15);
mainPlats[2] = new Platforms(570, 350, 100, 15);
}
void draw() {
level();
}
void level() {
mainPlats[0].displayPlat();
mainPlats[0].platMove();
mainPlats[0].platTransition();
mainPlats[1].displayPlat();
mainPlats[1].platMove();
mainPlats[1].platTransition();
mainPlats[2].displayPlat();
mainPlats[2].platMove();
mainPlats[2].platTransition();
}
Also note that you shouldn't load the image every single frame, either. You should only load it once, at startup. You also might want to use a for loop to iterate over your Platforms instead of referring to every single index. But these don't really affect your problem.
You've got rectX as positive values (>0) when you construct the platforms, but you are checking for rectX < -200 when you call platTransition, which is why it never does anything.

Stop box from moving if it hits another box

I'm trying to code a GUI that allows the user to move a box around. There is also another box on the screen, called "block" that the box isn't allowed to intersect. I have box moving fine, but I don't know how to stop the two from intersecting and keep the drawing of the box on the screen. Right now, the box disappear when it hits the block (I know why it's doing this, I just don't know how to fix it).
X and Y are the movement detected by the left and right mouse keys.
box = new Rectangle2D.Double(0 + X, 0 + Y, 200, 50);
block = new Rectangle2D.Double(300, 300, 50, 50);
if (box.intersects(block)) {
hit = true;
} else {
hit = false;
}
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.red);
g2.draw(block);
if (model.hit()) {
g2.setPaint(Color.black);
g2.drawString("WHOA THERE", 20, 50);
} else {
g2.setPaint(Color.blue);
g2.fill(box);
I'm a little unclear on what you're asking. If you're asking how to draw the box when it hits the block, the problem is your if/else statement. In the if case, it skips the drawing block. Remove the else to draw the box even if it hits.
If you're asking how to prevent the two boxes from intersecting, that's easy. If you detect they intersect, move the moving box so it's just outside the block. So if the block is at x = 10, set the moving box (model?) so it's at x = 11. HTH

Java Mouse Input Per Second

I am coding a Java game that uses mouse actions. I have a point msc in my main class - it gets changed whenever I press the mouse and gets set to (0, 0) whenever I release the mouse. For the buttons, they check if msc is inside their rectangle, and if so, call click.
One of the buttons is supposed to toggle a boolean; when I click on it though, it switches true and false very fast because msc gets updated every time paintComponent is called.
Here is the code for the button click method:
if (button.contains(Screen.msc)) {
beenClicked = true;
this.width = this.width - 2;
this.height = this.height - 2;
this.x = this.x + 1;
this.y = this.y + 1;
g.setColor(currColor);
textColor = Color.YELLOW;
}
but that is not where the problem is I think. Here is the code that changes msc:
public void mouseReleased(MouseEvent e) {
Screen.msc = new Point(0, 0);
}
public void mousePressed(MouseEvent e) {
Screen.msc = new Point((e.getX()) - ((Frame.size.width - Screen.myWidth) / 2), e.getY() - ((Frame.size.height - (Screen.myHeight)) - (Frame.size.width - Screen.myWidth) / 2));
}
and the code for what happens when the specific toggle button is clicked:
if (toggleToolTips.clicked()) {
if (Screen.canDrawTooltip) {
Screen.canDrawTooltip = false;
} else if(!Screen.canDrawTooltip){
Screen.canDrawTooltip = true;
}
}
The problem is every time I go and click the button, the boolean switches back and forth real fast. When I hold it, it just rapidly and continually switches. I would like to make it so that I click once and it switches once.
Your setting the boolean beenClicked but are not checking it anywhere. I would suggest trying
if (!beenClicked && toggleToolTips.clicked())
{
Screen.canDrawTooltip = !Screen.canDrawTooltip);
}
And setting beenClicked back to false somewhere such as mouseReleased.

Categories