This site requires JavaScript, please enable it in your browser!
Greenfoot back
SaxOps1
SaxOps1 wrote ...

2015/7/13

SetImage with variables

SaxOps1 SaxOps1

2015/7/13

#
Hey, I was wondering about setting the image with variables, because I know i can do things like this:
1
setImage(x + ".png");
But i was wondering if you can do the same, except with other kinds of variables, maybe a boolean, like this:
1
setImage(isTrue + ".png");
Thanks!
davmac davmac

2015/7/13

#
Well, why not try it and see? I think you'll find that a boolean added to a string will yield a string (in the example above either "true.png" or "false.png") so as long as such an image exists, then it will work, assuming that's what you intended. Note that there's nothing special about 'setImage' in this regard. You can use variables in the parameter value expressions for any method call.
SaxOps1 SaxOps1

2015/7/14

#
Ok, thanks! I am going to try and use this for walls to 'connect' to each other! e.g.
1
setImage(up + down + left + right + ".png");
davmac davmac

2015/7/14

#
If those variables are not strings, you may need to prepend an empty string so that the compiler understands that you want to convert them to a string and not add them together. Eg:
1
setImage("" + up + down + left + right + ".png");
SaxOps1 SaxOps1

2015/7/15

#
Thanks!
You need to login to post a reply.