Lesson 12 Why does this work?

I have a student who is probably more experienced at programming than I am. This is not a problem for me, but I cannot understand why his solution to the rotating pan problem is working. Can someone help me to understand? His program is below.

I don’t see a counter loop.

var pan = createSprite(200, 200);
pan.setAnimation(“pan”);

function draw() {
background(“palegreen”);
pan.rotation += 1;
drawSprites();

The draw loop runs the code inside of it at every frame, so it is basically a loop. The pan.rotation += 1 line of code adds to the already existing rotation variable the sprite has.

Thank you! I’ve never seen += instead of a counter loop before.

These two expressions are exactly the same … += is a shortcut

pan.rotation += 1;
pan.rotation=pan.rotation+1;

Good luck!

Mr. Wood

1 Like