U3L28 Function Error

playerPolygoon.isTouching is not a function (line 37)

My student is receiving this error in his code but I’m not sure what the issue is. Any ideas?

Greetings, @csalinas .

I believe the problem lies in there that .isTouching is reserved for specifically, only sprites. “playerPolygoon” and “collectable” should be changed to sprites instead of drawings.

I hope this has helped!

~ Pluto

Thank you! I’ve let the student know and we’ll see how it goes!

So we looked at the code and the polygon drawing is preferred since the purpose of the game is to add a side to the polygon each time a line is collected.

Is there a way to do something similar for a variable rather than a drawing?

Greetings once again @csalinas,

I don’t really think so, unfortunately, but there is an alternative route that you could choose (which might not be that efficient).


The only way to do this would be to set different animations for each polygon that’s shown.
I’ve also experimented with this and you can’t tell the difference when “sides = 20-25”, so I suggest making 20-25 animations maximum and make the animation cap at the final polygon animation.

Example:

/*
Keep note that you have to name the shapes accordingly.
Example: shape3, shape4, shape5
*/

var sides = 2;
var playerPolygon = createSprite(200, 200);
var line = createSprite(200, 200);
line.setAnimation("line animation here or you could change the width and height of the sprite to make it into a line");

function draw() {
  if (playerPolygon.isTouching(line)) {
    sides += 1; //same as sides = sides + 1 | if you didn't know
  }
  if (sides >= 20) { //makes it stay at 20 if it's already 20
    sides = 20;
  }
  playerPolygon.setAnimation("shape"+sides)
  drawSprites();
}

If you want to make the sides still go up even after it’s maxed out at 20,

//The variable will still be changed, but the animation wont.
if (sides <= 20) {
  playerPolygon.setAnimation("shape"+sides)
} else {
  playerPolygon.setAnimation("shape20");
}

Sorry if this isn’t as efficient as you thought it’d be, but this was the best way I could think of.

~ Pluto

1 Like

I agree that this would be a decent way of solving the problem and it’s not that inefficient either!

Thanks for weighing in!

Mike

That’s what I was thinking too but we were hoping there was a way for the polygon to keep adding to its number of sides. But this works! Thank you all!