Note: The following can be used to stop an object from leaving canvas
Getting too big? / Going too far right? - Positive values
Step 1: Create a speed variable in initialize(). The number should be the same as whatever you are adding/subtracting in your update line.
If you have in repeatingForever():
Then in initialize(), you should add:
Step 2: In repeatingForever(), change your "update" line so that it uses the speed variable you created.
becomes...
Step 3: In repeatingForever(), add an if-statement and set q1speed to 0.
q1speed = 0
}
- Always use ">" to stop the shape from getting "too big."
- The number to "stop" at shouldbe bigger than the one in initialize().
Here's how the code looks like now:
q1 = 50
q1speed = 3
repeatingForever():
if (q1 > 300) {
q1speed = 0
}
q1 = q1 + q1speed
cc.fillStyle = "#000"
cc.fillRect(50, 100, q1, 50)
Getting too small? / Going too far left? - Negative values
Step 1: Create a speed variable in initialize(). The number should be the same as whatever you are adding/subtracting in your update line.
If you have in repeatingForever():
Then in initialize(), you should add:
Step 2: In repeatingForever(), change your "update" line so that it uses the speed variable you created.
becomes...
Step 3: In repeatingForever(), add an if-statement and set q1speed to 0.
q1speed = 0
}
- Always use " < "to stop the shape from getting "too small. "
- The number to "stop " at should be smaller than the one in initialize().
Here's how the code looks like now:
q1 = 50
q1speed = -3
repeatingForever():
if (q1 < 40) {
q1speed = 0
}
q1 = q1 + q1speed
cc.fillStyle = "black "
cc.fillRect(50, 100, q1, 50)