1

I need to make a box that stays red after my mouse enters the box. I have the code that works, but once my mouse leaves the box, it returns to white. Help? I edited my code to show you guys more. Sorry, this is my first time using this site so I am kind of a noob.

override func update() {
        // ****** IMPORTANT - PLEASE NOTE ******
        // Variables declared here, inside the update function,
        // only remember values for one call (execution) to the update function.
        // When update is called again to redraw the view, a variable
        // declared inside the function will not remember its previous value.

        background(gray: 0.5)

        // *************************************************
        // Insert your drawing code here, below this comment
    rect(x: 200, y: 175, width: 75, height: 75)
    rect(x: 305, y: 175, width: 75, height: 75)
    rect(x: 410, y: 175, width: 75, height: 75)
    rect(x: 515, y: 175, width: 75, height: 75)

    var boxA = false

    if tin.mouseX >= 200.0 && tin.mouseX <= 275.0 && tin.mouseY >= 175.0 && tin.mouseY <= 250.0 {
              boxA = true
           }

    if boxA == true {
        fillColor(red: 1, green: 0, blue: 0, alpha: 1)
        rect(x: 200, y: 175, width: 75, height: 75)
    }

gzaya
  • 13
  • 4

2 Answers2

0

It's not clear what function you're writing this snippet in. So, to make it simple, add a property observer to your boolean boxA:

var boxA = false {
    willSet {
        if newValue {  // Set red color only when the boxA is true
            fillColor(red: 1, green: 0, blue: 0, alpha: 1)
            rect(x: 200, y: 175, width: 75, height: 75)
        }
    }
}

You can read more on the property observers here: What is the purpose of willSet and didSet in Swift?

George
  • 25,988
  • 10
  • 79
  • 133
Gaurav Chandarana
  • 738
  • 10
  • 10
  • Thanks @George_E – Gaurav Chandarana Feb 04 '20 at 20:00
  • @George_E I added more of my code, does that change anything? Im a beginner coder so I am not understanding your reply. If i use the code you provided I would erase my if statement regarding tin.mouse, and the code wouldn't work – gzaya Feb 04 '20 at 20:42
  • @gzaya Just to let you know - this isn’t my code. I only made an edit. – George Feb 04 '20 at 20:53
0

Presumably update is being called as part of some drawing loop. The comment at the start suggests what your problem is

// Variables declared here, inside the update function,
// only remember values for one call (execution) to the update function.
// When update is called again to redraw the view, a variable
// declared inside the function will not remember its previous value.

So you draw your rectangle:

rect(x: 200, y: 175, width: 75, height: 75)

which is presumably with a white background. Then you have your test for being inside the rectangle:

var boxA = false
if tin.mouseX >= 200.0 ... {
  boxA = true
}

and finally you fill the rectangle with red if boxA is true.

But as soon as update finishes, the state of boxA is gone. Next time through update, it's again set to true if and only if the mouse is inside the rectangle, so if you move it outside, it'll go back to false.

To fix it, you have to move boxA to some place where it won't get reset. For example, if update is a method in some class, then you could make boxA an instance variable of the class. E.g.,

class View {
  var boxA = false  // Here the state won't be lost after update finishes

  override func update() {
    ...
    // xxx var boxA = false xxx Don't put the declaration here
    if ... {
      boxA = true
    }
    ...
  }
}

bg2b
  • 1,939
  • 2
  • 11
  • 18