’15 -’16 General Discussion for Lesson 5.18

Hi @caroline

I think the easiest way to do this may be a google drawing actually. Its a different type of document in google drive that allows you to create a text box and then you can also create shapes. Then you can layer them to get the look you want.

Best,

Dani

One of my students wants to display a random image based on a keyword input by the user. Anyone have an easy way to do that?

Note flickr and pencilcode are blocked by my school right now. :frowning:

Got Pencilcode unblocked!

var sport = “Sail”
var sportUrl = “https://pencilcode.net/img/” + sport;
image(“image1”, sportUrl);
setPosition(“image1”, 200, 300, 100, 100);

Excited to hear this works. Very nicely done taking advantage of Pencil Code’s (admittedly awesome) random image feature.

I have two questions from this student project.

First how do we programmatically change the background color or background image of a button? We tried using setProperty but couldn’t get the syntax to work.

Second, there are a bunch of buttons (81) that are set up to allow you to change the numerical value. Obviously he can’t create an event handler for each button by hand. What is the best way to approach this? Programmatically create the 81 event handlers? Create one event handler for clicking anywhere on the screen and then try to figure out which button the mouse was on?

Thanks!

Hi @caroline!

Changing button backgrounds - I think it would be easier to use an image element instead of a button if you are looking for the background of your button to change. I’m not sure if there is an easy way to change the background of the button itself.

Button of Buttons - @baker created a demo of how you can use the event object to check for clicks on lots of buttons. The demo is here : https://studio.code.org/projects/applab/_rkLfFzSwVWYQ1tIQ17wGQ/edit . The trick is that the screen always captures click and then you can probe the event object to figure out which button was clicked. That conditional :

if(event.targetId==“screen1”){
return;
}

Allows you to ignore the clicks that were on the screen and not on the button.

Hope that helps!

Dani

1 Like