Width/Height Sprite Help

var sprite = createSprite(200, 300, 100, 50);

Why does this not change my sprites width and height?

Why do I have to use
sprite.height=50;
sprite.width=100;

Thank you! :slight_smile:

Hi, @melodi_kunn,

Can you share a link to a project we could look at to help you debug?

I made a very simple project with your code and it seems to be accepting the height and width.

Let us know and we’d be happy to help!

Mike

https://studio.code.org/projects/gamelab/K73JWykVMqTT6zXzu81nODr94QZLfDVwClMvMcn6HK8

This is just a simple example. Do you think it has anything to do with the level they are on? Unit 3, level 9? Like for instance, certain code is not accessible until it has been introduced by the program?

Thank you!
Melodi

Now that I looked at your example, I did see a few things … I remixed it and here is my remixed version…

I’m not sure why your 1st car size doesn’t change when you add the width and height component, but normally, you will declare a sprite without a width and height and it will use the width and height of the sprite itself (from the animation tab). Then, although you can use height and width to adjust the size, you will usually use the scale block (see my example) to adjust the entire sprite as a percentage of its original size (such as my .75 (75%)). You could also set the scale to be 1.5 (150%) of the original size, etc.

Also, in your example, both of your sprites had the same name (car). This could have been part of the problem because when you name two sprites with the same name and then you try to change attributes, the program will only remember the last one you named, so the first one may behave differently than you would expect. So, again, notice in my example that I named the 2nd one car2.

Although this may not definitively answer your original question, I hope it at least shows you a different way to approach it …

Let us know if you still have questions and we are happy to look at it.

Mike

Hi,

I am having the same issue. My students aren’t able to change the width and height of sprites inside of the createSprite parameters. I haven’t taught sprite.scale so I am not ready for them to use this. Is this a bug?

Hi @sjlindsay,

Do you have a link to this student’s project? If I recall correctly from the post from a few months ago, there was a bug that we were able to identify & help that teacher resolve.

Thanks!
–Michael K.

After discussing this with others, we determined that it isn’t a bug, but that those sizing parameters will only apply if the sprite doesn’t have an animation added (ie. the gray box). Once you add an animation, the animation size takes over and is used, so a scale block will be required to change the size.

I have run into this in my own classroom as well. I usually handle it this way.

“We will learn how to resize sprites in the next day or two, but if you want to try exploring it sooner, you can switch to text mode and type the following code: sprite.scale = 0.5; (for a 50% size).”

That’s pretty much all I tell them. Kids who are adventurous will give it a shot and the others are usually ok to wait until they get the block.

Hope this helps!

Mike

2 Likes

We had this same issue come up today. Our other CS teacher and I kept circling around trying to figure out WHY the width and height parameters didn’t apply to our student’s sprites. The explanation from @mwood is what we’re going with.

Here are a couple suggestions for improvements with the curriculum and documentation that would have helped us tremendously:

  1. Lesson 7, level 10 asks students to create their own sprites. Inevitably, most student’s animations/sprites end up huge so they want to resize them. They haven’t learned how to resize sprites yet and don’t have access to blocks that will do it correctly. Some of them will think to try changing the width and height parameters and then get frustrated and confused when it doesn’t work. Their only option is to resize the animation which isn’t ideal. Some sort of help text for teachers would have been great or move this kind of a challenge to a lesson after they have learned about scale.
  2. Include the information about this weird quirk in the documentation (Code.org Tool Documentation). Somewhere it should say that the height and width only apply if there isn’t an animation added.

Thanks!

3 Likes

@mreeves,

Great suggestions. You may want to pass them on to support@code.org which will document it for possible action.

Good luck!

Mike

You don’t have a sprite you’re using the width and height parameters in… But if you were using them while declaring the “guy” then that’s normal behavior. When you’re setting the animation of a sprite, the sprite will reset it’s width and height and set it to the size of the animation it’s assigned to.