JavaScript Introducing JavaScript Finishing the Game Adding New Graphics

Faye Ma
Faye Ma
1,028 Points

i don't understand what 'platform' in ('platform', platform_1.png) is, is that a variable?? no image is called platform.

i don't understand what 'platform' in ('platform', platform_1.png) is, is that a variable?? no image is called platform. images are called platform_1.png and platform_2.png. I don't understand how the parameter is defined. Why there are two values? Why not one? Why not three?

2 Answers

Michael Hulet
MOD
Michael Hulet
iOS Development with Swift Treehouse Moderator 46,358 Points

"platform" and "platform_1.png" as you see them are not variables per se, but string literals that you're using as parameters to the image method. image is a method (AKA a function) on the Loader class of the Phaser library (game.load is an instance of the Loader class). This method takes 3 parameters:

  1. A key to get/set this image in the cache. Loading images takes a lot of work, so Phaser loads the image and automatically holds onto it so it doesn't have to load it again when you ask for it next time. In order for it to do this, it has to have a unique way for it/you to reference it in the future, so it makes you define a string as a key to use as the first parameter of this method, just like in a normal Object. In the future, you can reference this image with the key "platform" (or whatever other string you specify)
  2. The path to the image file you want to load. You don't have to supply this parameter like you do the key. If you don't, it'll automatically look for a file named "key_you_supplied_before.png", where key_you_supplied_before is the string you provided as the first parameter to this method. If you do supply a value for this parameter like we do in the video, it'll look for a file at the path you specify, which is platform_1.png in this case
  3. A boolean (either true or false) to specify if it should overwrite any files that already exist in the cache with the key you specified. The default value for this parameter is false, so if you don't specify a 3rd parameter (like in the video), Phaser will assume you mean false for this parameter. Say that we already loaded platform_1.png using the key "platform". We're done using platform_1.png now, and now we want to load platform_2.png and also reference it using the key "platform". If we just specify the key "platform" and the file path "platform_2.png", Phaser will see that a file already exists in the cache for the key "platform" and give us back that. However, that file is platform_1.png, which we don't want anymore. Specifying true for this method will tell Phaser that we know it already has a file in its cache with this key, but we want it to get rid of that file and load the one we just told it about

If you specify more parameters to this method for some reason, they will be ignored. You can find the reference for this method in the Phaser documentation.

Faye Ma
Faye Ma
1,028 Points

Thank you so much!! that's very helpful!

Josh Cha
Josh Cha
1,778 Points

I'm a beginner as well and just started the program. Lets work hard! :)