Warning! This page is still in the works.


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():

q1 = q1 + 3

Then in initialize(), you should add:

q1speed = 3

Step 2: In repeatingForever(), change your "update" line so that it uses the speed variable you created.

q1 = q1 + 3

becomes...

q1 = q1 + q1speed

Step 3: In repeatingForever(), add an if-statement and set q1speed to 0.

if (q1 > 300) {
q1speed = 0
}


Here's how the code looks like now:

initialize():
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():

q1 = q1 - 3

Then in initialize(), you should add:

q1speed = -3

Step 2: In repeatingForever(), change your "update" line so that it uses the speed variable you created.

q1 = q1 - 3

becomes...

q1 = q1 + q1speed

Step 3: In repeatingForever(), add an if-statement and set q1speed to 0.

if (q1 < 0) {
q1speed = 0
}


Here's how the code looks like now:

initialize():
q1 = 50
q1speed = -3

repeatingForever():
if (q1 < 40) {
q1speed = 0
}
q1 = q1 + q1speed
cc.fillStyle = "black "
cc.fillRect(50, 100, q1, 50)