Explore MIT App Inventor - Basic http://dev-explore.appinventor.mit.edu/tutorial-difficulty/basic en PaintPot (Part 2) for App Inventor 2 http://dev-explore.appinventor.mit.edu/ai2/paintpot-part2 <div class="field field-name-body field-type-text-with-summary field-label-hidden view-mode-rss"><div class="field-items"><div class="field-item even" property="content:encoded"><p>This tutorial has two parts: <a href="/ai2/paintpot-part1">Part 1</a> | <a href="/ai2/paintpot-part2">Part 2</a> </p> <p>Part 2 extends <a href="/ai2/paintpot-part1">Part 1</a> of the tutorial to create both large and small dots, as a demonstration of how to use <em>global variables</em>.</p> <h4 class="ai-header">Starting</h4> <p>Make sure you've completed the <a href="/ai2/setup">Set Up</a> process and you have your completed project from <a href="/ai2/paintpot-part1">PaintPot Part 1</a> loaded.</p> <p>Start where you left off at the end of Part 1, with the project open in App Inventor. Use the <strong>Save As</strong> button to make a copy of <span style="color:green;">PaintPot</span> so you can work on the new version without affecting the original version. Name the copy "PaintPotV2" (with no spaces). After saving a copy, you should see <span style="color:green;">PaintPotV2</span> in the Designer.</p> <h4 class="ai-header">Creating variables</h4> <p>The size of the dots drawn on the canvas is determined in the <span class="basicblock">when DrawingCanvas.Touched</span> event handler where <span class="callblock-ai2">call Drawing.DrawCircle</span> is called with <strong style="color:green;">r</strong>, the radius of the circle, equal to 5. To change the thickness, all we need to do is use different values for <strong style="color:green;">r</strong>. Use <strong style="color:green;">r</strong> = 2 for small dots and <strong style="color:green;">r</strong> = 8 for large dots.</p> <p>Start by creating names for these values:</p> <ol><li>Open the Blocks Editor if it isn't already open and connect the phone. Your phone should show the buttons and the canvas you built previously.</li> <li>In the Blocks Editor, in the Built-In column, open the Variables drawer. Drag out a <span class="variableblock">initialize global name to</span> block. Change the text that reads "name" to read "small". A yellow warning exclamation mark might appear on the block. If you mouse over this you'll see a warning message explaining that the block has an empty socket.</li> <li>You need to fill in the socket with a number block that specifies the value for "small" -- use 2 as the value. To create the number block, type the number 2. A menu will appear, showing you all the possible blocks that include "2" in their name. Click on the first one, which is the <span class="mathblock">number 2</span> itself, and a number block with the value 2 should appear. Plug that in to the <span class="variable block">initialize global small to</span> block. The yellow warning mark will disappear, because the empty socket has been filled. (The second value listed in the menu is the math block <span class="mathblock">atan2</span> which you won't use here.)</li> </ol><p>Here are the steps in the sequence:</p> <ol><li><img src="/sites/all/files/ai2tutorials/paintPot2/var1.png" /></li> <li><img src="/sites/all/files/ai2tutorials/paintPot2/var2.png" /></li> <li><img src="/sites/all/files/ai2tutorials/paintPot2/var3.png" /></li> <li><img src="/sites/all/files/ai2tutorials/paintPot2/var4.png" /></li> <li><img src="/sites/all/files/ai2tutorials/paintPot2/var5.png" /></li> </ol><p>You've now defined a global variable named <span class="variableblock">small</span> whose value is the number 2.<br /> Similar to <span class="setblock">small</span>, define a global variable <span class="variableblock">big</span>, whose value is 8.<br /> Finally, define a global variable <span class="variableblock">dotsize</span> and give it an initial value of 2.</p> <pre class="ai-box">You might wonder whether it would be better programming style to make the initial value of <span style="color:black;">dotsize</span> be the value of <span style="color:black;">small</span> rather than 2. That would be true, except for a subtle programming point: Doing that would be relying on the assumption that <span style="color:black;">small</span> will already have a value at the point in time when <span style="color:black;">dotsize</span> is assigned its value. In App Inventor, you can't make assumptions about the order in which different <span class="setblock" style="color:black;">def</span> blocks will be processed. In general, of course, you really <em>would</em> like to specify the order in which variables are assigned. You can do this by assigning all values when the application is initialized, using the Screen initialize event. The <a href="http://appinventor.mit.edu/explore/content/quizme.html">Quiz Me</a> tutorial gives an example of initialization.</pre><h4 class="ai-header">Using variables</h4> <p>Now go back to the touch event handler you set up in Part 1 and change the <span class="callblock-ai2">call to DrawCircle</span> block so that it uses the value of <span style="color:green;">dotsize</span> rather than always using 5.</p> <p>In the Blocks Editor, switch to the My Blocks column, and open the Variables drawer. Pull out a get block and click on the dropdown. You should see three new variables in the dropdown: small, big, and dotsize</p> <p>These blocks were automatically created and put in the dropdown of <span class="variableblock">get</span> and <span class="variableblock">set</span> variable blocks, similarly to the way that x and y were created in the dropdown when you defined the <span class="eventblock">when DrawingCanvas.Touched</span> event handler in the part 1 of this tutorial. "Global" means "global variable", in contrast to the event-handler arguments, which are considered "local variables". The difference is that the argument values are accessible only within the body of the event handler, while global variables are accessible throughout the entire program.</p> <ul><li>Go to the <span class="eventblock">when MyCanvas.Touched</span> event handler and replace the number 5 block in <span class="callblock-ai2">call DrawCircle</span> with the <span class="variableblock">get dotsize</span> block from the Variables drawer.</li> </ul><h4 class="ai-header">Changing the values of variables</h4> <p>Now set up a way to change dotsize to be small (2) or big (8). Do this with buttons.</p> <ol><li>In the Designer, drag a <strong>HorizontalArrangement</strong> component into the Viewer pane below the <span style="color:green;">DrawingCanvas</span> component. Name the component "BottomButtons".</li> <li>Drag the existing <span style="color:green;">ButtonWipe</span> into <span style="color:green;">BottomButtons</span>.</li> <li>Drag two more button components from the Palette into <span style="color:green;">BottomButtons</span>, placing them next to <span style="color:green;">ButtonWipe</span>.</li> <li>Name the buttons "ButtonBig" and "ButtonSmall", and set their <strong style="color:green;">Text</strong> to read "Big dots" and "Small dots", respectively.</li> <li>In the Blocks Editor under My Blocks, create a <span class="eventblock">when ... Clicked</span> event handler for <span style="color:green;">ButtonSmall</span> that changes <span style="color:green;">dotsize</span> to be the value of <span style="color:green;">small</span>. To change <span style="color:green;">dotsize</span> use the <span class="variableblock">set global dotsize to</span> block from the MyDefinitions drawer and plug in the <span class="variableblock">global small</span> block.</li> <li>Make a similar event handler for <span style="color:green;">ButtonBig</span>.</li> </ol><p>The two click event handlers should look like this:</p> <p><img src="/sites/all/files/ai2tutorials/paintPot2/PaintPotDotsClick.png" /></p> <p>You're done! You can draw in <span style="color:green;">PaintPot</span> and use the new buttons to draw either big dots or small dots. Notice that dragging your finger still produces a thin line. That's because the changes we just made don't affect how <span class="callblock-ai2">DrawLine</span> is called.</p> <p>Here's the finished program in the Designer:</p> <p><img src="/sites/all/files/ai2tutorials/paintPot2/PaintPotAllDesigner.png" /></p> <p>and in the Blocks Editor:</p> <p><img src="/sites/all/files/ai2tutorials/paintPot2/PaintPotAllBlocks.png" /></p> <pre class="ai-box">A bug for you to work on: The program you just built has a slight bug. If you start drawing before pressing any of the paint buttons, the paint color will be black; however, after you choose a color, there's no way to get back to black. Think about how you could fix that.</pre><h4 class="ai-header">Review</h4> <p>You create global variables by using <span class="variableblock">def</span> blocks from the Variables drawer.</p> <p>For each global variable you define, App Inventor automatically supplies a <span class="variableblock">global</span> block that gives the value of the variable, and a <span class="variableblock">set global ... to</span> block for changing the value of the variable. These blocks can be found in the Variables drawer.</p> <h4 class="ai-header">Scan the Sample App to your Phone</h4> <p>Scan the following barcode onto your phone to install and run the sample app. </p><p><img src="/sites/all/files/ai2tutorials/paintPot2/PaintPot2Barcode.png" /></p> <p>Or <a href="/sites/all/files/ai2tutorials/paintPot2/PaintPot2.apk">download the apk</a></p> <h4 class="ai-header">Download Source Code</h4> <p>If you'd like to work with this sample in App Inventor, download the <a href="/sites/all/files/ai2tutorials/paintPot2/PaintPot2.aia">source code</a> to your computer, then open App Inventor, go to the My Projects page, and choose <b>More Actions | Upload Source</b>.</p> <pre class="ai-box">Done with <span style="color:black;">PaintPot</span>? Return to the other tutorials <a href="/ai2/tutorials">here</a>.</pre></div></div></div><section class="field field-name-field-version field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Version:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-version/app-inventor-2" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">App Inventor 2</a> </li> </ul> </section> <section class="field field-name-field-tutorial-difficulty field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Difficulty:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-difficulty/basic" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Basic</a> </li> </ul> </section> <section class="field field-name-field-tutorial-type field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Type:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-type/drawing-canvas" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Drawing Canvas</a> </li> </ul> </section> Tue, 16 Jul 2013 14:33:50 +0000 joanie 364 at http://dev-explore.appinventor.mit.edu http://dev-explore.appinventor.mit.edu/ai2/paintpot-part2#comments PicCall for App Inventor 2 http://dev-explore.appinventor.mit.edu/ai2/piccall <div class="field field-name-body field-type-text-with-summary field-label-hidden view-mode-rss"><div class="field-items"><div class="field-item even" property="content:encoded"><p><span style="color:green;">PicCall</span> shows you how you can use App Inventor to make apps that do actual things, like calling friends.</p> <p>This tutorial assumes that you have completed the <a href="/ai2/setup">Set Up</a> process.</p> <h4 class="ai-header">Before starting</h4> <p>To run <span style="color:green;">PicCall</span>, your phone must be set up and activated for making phone calls. If it isn't you can still build <span style="color:green;">PicCall</span> for practice, but the phone won't actually make the calls.</p> <pre class="ai-warning">Warning: <span style="color:black;">PicCall</span> does not work on all Android phones in the current implementation of App Inventor: you'll get an error notice on some phones when you try to pick a phone number. Also, you won't see all your contacts — only those created from Gmail. These limitations will be removed in the future.</pre><p>In this tutorial, unlike <span style="color:green;">HelloPurr</span>, you'll give names to components, rather than just using the default names that App Inventor provides (like "Button1"). Using meaningful names is good programming practice: it helps you keep your programs straight in your own mind, and it helps others understand your programs.</p> <p>Your phone should also contain a few contacts with pictures. You can use the Contacts app to save pictures for your contacts. You can also click on Contacts in your Gmail account on your computer and add pictures there.</p> <p>Make sure your computer and your phone are set up to use App Inventor. Start a new project in the Designer window. Name it "PicCall" and change the screen <strong style="color:green;">Title</strong> to <span style="color:green;">PicCall</span>. Open the Blocks Editor, click <strong>Connect to Companion</strong>, and check that the phone has started the App Inventor app.</p> <h4 class="ai-header">Getting started</h4> <p>Start out just like in <span style="color:green;">HelloPurr</span> by placing a button on the screen. Make the button 150 pixels wide and 150 pixels high. Set button's <strong style="color:green;">Image</strong> to a picture. You may as well use the picture of the kitty if it's handy -- you'll be changing the picture soon. Set the <strong style="color:green;">Text</strong> of the button to "Press to Call", although you'll be changing that soon as well.</p> <p>Change the name of the Button component to "TopButton" (you'll make one called "BottomButton" later in the tutorial). To change a component's <strong style="color:green;">Name</strong>, click the <strong>Rename</strong> button in the Components panel and enter the new name.</p> <pre class="ai-box">In this tutorial, unlike <span style="color:black;">HelloPurr</span>, you'll give names to components, rather than just using the default names that App Inventor provides (like "Button1"). Using meaningful names is good programming practice: it helps you keep your programs straight in your own mind, and it helps others understand your programs.Don't confuse the <strong style="color:black;">Name</strong> of a component with the <strong style="color:black;">Text</strong> of a component. The <strong style="color:black;">Text</strong> is what appears on the screen. The <strong style="color:black;">Name</strong> is the name your program uses to refer to the component. You'll see the name in the Components structure list in the Designer and on the drawers in the Blocks editor.</pre><h4 class="ai-header">Making phone calls</h4> <p>In <span style="color:green;">HelloPurr</span>, you made the phone play a sound when the button was clicked. <span style="color:green;">PicCall</span> in almost the same, except that instead of playing a sound, the phone makes a call.</p> <p>App Inventor's <strong>PhoneCall</strong> component makes phone calls. You can find PhoneCall in the Social section of the Palette. Open that section and drag out a PhoneCall into the viewer. It will go into the Non-visible components area. Name it "TopCall". The PhoneCall's <strong style="color:green;">PhoneNumber</strong> property determines the number to call. Set that to some 10-digit phone number you'd like to call. Here's how the Designer should look:</p> <p><img src="/sites/all/files/ai2tutorials/picCall/PicCallDesigner.png" /></p> <p>Now switch to the Blocks Editor and pull out the <span class="eventblock">when TopButton.Click do</span> block. In the <span class="eventblock">do</span> slot, place a <span class="callblock">call TopCall.MakePhoneCall</span> block from the TopCall drawer, so that the event handler looks like this:</p> <p><img src="/sites/all/files/ai2tutorials/picCall/PicCallWhenClick.png" /></p> <p>Go ahead and test what you have so far on the phone: Press the button and make the call. You could package this up as an app right now. It would be a pretty limited app, always calling the same fixed number, some people might find that useful.</p> <h4 class="ai-header">Phone contact information</h4> <p>In addition to making phone calls, App Inventor apps can also get information from the phone's contact list. You do this with the <strong>PhoneNumberPicker</strong> component.</p> <p>Pull out a PhoneNumberPicker component from the Social section of the Palette, place it under <span style="color:green;">TopButton</span>. A PhoneNumberPicker is a kind of button: when you press it, it brings up your phone contacts list and lets you pick someone. Change the name of the PhoneNumberPicker to "TopPick", and change its <strong style="color:green;">Text</strong> to "Press to pick a number to call". Try it by pressing the picker on your phone: you should see your contacts come up, and you can pick one. Nothing will happen after you pick, because you haven't yet told the components to do anything. You'll do that next.</p> <h4 class="ai-header">Using the picker</h4> <p>Switch to the Blocks window and open the drawer for <span style="color:green;">TopPick</span>. Drag out the <span class="eventblock">when TopPick.AfterPicking do</span> block. This lets you define an event handler that says what to do after you've picked a number from your contacts.</p> <p>Now open the <span style="color:green;">TopCall</span> drawer and drag out <span class="setblock">set TopCall.PhoneNumber to</span> and fit it into the slot in the <span class="eventblock">when TopPick.AfterPicking do</span> block. Now drag out <span class="argblock">TopPick.PhoneNumber</span> from the <span style="color:green;">TopPick</span> drawer and plug it into the empty socket. Here's how your event handler should look:</p> <p><img src="/sites/all/files/ai2tutorials/picCall/PicCallAfterPicking.png" /></p> <p>Try it on the phone: Press the picker, choose a contact and a phone number. Then press the phone call button to make the call. Add a command to the event handler to set <span style="color:green;">TopButton</span>'s <strong style="color:green;">Text</strong> property to <span class="argblock">TopPick.PhoneNumber:</span></p> <p><img src="/sites/all/files/ai2tutorials/picCall/PicCallAfterPicking2.png" /></p> <h4 class="ai-header">Pictures</h4> <p>If you have a picture stored with your contacts, you can make the button show that along with the phone number, rather than always using the picture of the kitty. To do this, add an command to the event handler, to set the <strong style="color:green;">Image</strong> property of <span style="color:green;">TopButton</span> to be the the <strong style="color:green;">Picture</strong> property of <span style="color:green;">TopPick</span>:</p> <p><img src="/sites/all/files/ai2tutorials/picCall/PicCallAfterPicking3.png" /></p> <pre class="ai-box">PhoneNumberPicker has two properties that are easy to confuse: <strong style="color:black;">Picture</strong> and <strong style="color:black;">Image</strong>. <strong style="color:black;">Picture</strong> is the picture associated with the contact that's picked. <strong style="color:black;">Image</strong> is the image of the PhoneNumberPicker component as it appears in the Designer and on the phone.</pre><h4 class="ai-header">Enhancements</h4> <p>Here are some variations for you to try:</p> <ul><li>Add a second button, <span style="color:green;">BottomButton</span>, and a second PhoneNumberPicker, so that your app gives you the choice of two numbers.</li> <li>Add a label with instructions on how to use the app.</li> <li>Show the name of the person being called in addition to the phone number. Use an extra label to show the additional information.</li> </ul><h4 class="ai-header">Using PicCall</h4> <p>You can package <span style="color:green;">PicCall</span> and download it to the phone so you can use it when you're not connected to the computer. But there's a big limitation: Every time you restart <span style="color:green;">PicCall</span>, it starts fresh and does not remember what you picked last time. Later, we'll see how to use the <strong>TinyDB</strong> component to create apps that can remember information from one time to the next. Such information is called <em>persistent data</em>.</p> <h4 class="ai-header">Review</h4> <p>Here are the key ideas covered in this tutorial:</p> <ul><li>You can name components by means of the <strong>Rename</strong> button.</li> <li>App Inventor has components that can use information stored on the phone. The <strong>PhoneNumberPicker</strong> can get phone numbers and pictures for your contacts, and <strong>PhoneCall</strong> can make calls.</li> </ul><pre class="ai-box" style="background-image: url('/sites/all/files/ai2tutorials/picCall/android-warning-30x30.gif"><strong>Tip</strong>: The list that comes up when you run the phone number picker does not show the pictures associated with your contacts, on some Android systems. Even though the image isn't shown, the "Picture" property will still return a picture that will show up when you run the app, provided that the phone has a picture for that contact.</pre><p>Scan the following barcode onto your phone to install and run the sample app. </p><p><img src="/sites/all/files/ai2tutorials/picCall/PicCallBarcode.png" /></p> <h4 class="ai-header">Download Source Code</h4> <p>If you'd like to work with this sample in App Inventor, download the <a href="/sites/all/files/ai2tutorials/picCall/PicCall.aia">source code</a> to your computer, then open App Inventor, go to the My Projects page, and choose <b>More Actions | Upload Source</b>.</p> <pre class="ai-box">Done with <span style="color:black;">PicCall</span>? Return to the other tutorials <a href="/ai2/tutorials">here</a>.</pre></div></div></div><section class="field field-name-field-version field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Version:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-version/app-inventor-2" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">App Inventor 2</a> </li> </ul> </section> <section class="field field-name-field-tutorial-difficulty field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Difficulty:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-difficulty/basic" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Basic</a> </li> </ul> </section> Wed, 03 Jul 2013 14:50:54 +0000 aaron 358 at http://dev-explore.appinventor.mit.edu http://dev-explore.appinventor.mit.edu/ai2/piccall#comments PaintPot (Part 1) for App Inventor 2 http://dev-explore.appinventor.mit.edu/ai2/paintpot-part1 <div class="field field-name-body field-type-text-with-summary field-label-hidden view-mode-rss"><div class="field-items"><div class="field-item even" property="content:encoded"><p>This tutorial has two parts: <a href="/ai2/paintpot-part1">Part 1</a> | <a href="/ai2/paintpot-part2">Part 2</a> </p> <p>This tutorial introduces the <strong>Canvas</strong> component for creating simple two-dimensional graphics. You'll build an app that lets you draw on the phone screen in different colors.</p> <pre class="ai-box"><strong>Historical note</strong>: PaintPot was one of the first programs developed to demonstrate the potential of personal computers, as far back as the 1970s.</pre><p><a href="http://cs.usfca.edu/~wolber/appinventor/bookSplits/ch2PaintPot.pdf">Download Book Chapter PDF</a></p> <h4>What you're building</h4> <p><img style="float:right;margin:5px;" src="/sites/all/files/ai2tutorials/paintPot1/PaintPotPart1PhoneImage.png" /></p> <p>With the <span style="font-style:italic;">PaintPot</span> app, you can:</p> <ul><li>Dip your finger into a virtual paint pot to draw in that color.</li> <li>Drag your finger along the screen to draw a line.</li> <li>Tap the screen to make dots.</li> <li>Use the button at the bottom to wipe the screen clean.</li> <li>Include an image as a drawing background.</li> </ul><p><span style="font-style:italic;"> <p>This tutorial assumes that you have completed the <a href="/ai2/hellopurr">HelloPurr</a> tutorial. This tutorial introduces the following App Inventor concepts:</p> <ul><li>The <strong>Canvas</strong> component for drawing.</li> <li>Controlling screen layout with <strong>Arrangement</strong> components.</li> <li>Event handlers that take arguments.</li> <li>Variables.</li> </ul><h4 class="ai-header">Before starting</h4> <p>Make sure your computer and your phone are set up to use App Inventor. Start a new project in the Designer window, and name it <span style="font-style:italic;">"PaintPot"</span>. Open the Blocks Editor, click <strong>Connect to Phone</strong>, and make sure the phone has started the App Inventor app.</p> <h4 class="ai-header">Screen title</h4> <p>To get started, go to the Properties panel on the right of the Designer and change the screen <strong>Title</strong> to "PaintPot". You should see this change on phone, with the new title showing in the title bar.</p> <p>There are three names in App Inventor, and it's easy to confuse them:</p> <ol><li>The name you choose for your project as you work on it (in this case, <span style="font-style:italic;">PaintPot</span>). This will also be the name of the application if you package it for the phone.</li> <li>The name "Screen1", which is the name of the <strong>Screen</strong> component. You'll see it listed in the Components panel in the Designer. You can't change the name of the first Screen component in the current version of App Inventor but you can create additional screens with names of anything you should choose.</li> <li>The <strong>Title</strong> property of the screen, which is what you'll see in the phone's title bar. Title is a property of the <strong>Screen</strong> component. The Title starts out being "Screen1", which is what you used in <span style="font-style:italic;">HelloPurr</span>. However, you can change it, as you're doing for <span style="font-style:italic;">PaintPot</span>. To reiterate, the name and the title of Screen1 are initially the same, but you can change the title if you want.</li> </ol><h4 class="ai-header">Set up the Components</h4> <p>You'll use these components to make <span style="font-style:italic;">PaintPot</span>:</p> <ul><li>Three <strong>Buttons</strong> for selecting <strong style="color:red">red</strong>, <strong style="color:blue">blue</strong>, or <strong style="color:green">green</strong> paint, and another button for wiping the drawing.</li> <li>A <strong>Canvas</strong>, the drawing surface. This canvas has a <strong>BackgroundImage</strong>, which is <a href="/sites/all/files/ai2tutorials/helloPurr/kitty.png">this kitty</a> from the <span style="font-style:italic;">HelloPurr</span> tutorial. You can also draw on a blank canvas. That's just a canvas without a background image.</li> <li>There's also a component you don't see: you use a <strong>HorizontalArrangement</strong> to make the three color buttons line up.</li> </ul><p>That makes five components in all. Let's get them and build the app.</p> <h4 class="ai-header">Color Buttons</h4> <ul><li>Drag a <strong>Button</strong> component onto the Viewer and change the button's <strong>Text</strong> attribute to "Red" and make its <strong>BackgroundColor</strong> red.</li> <li>Click on <span style="font-style:italic;">Button1</span> in the components list in the Viewer to highlight it (it might already be highlighted) and use the <strong>Rename...</strong> button to change its name from "Button1" to "ButtonRed".</li> <li>Similarly, make two more buttons for blue and green, named "ButtonBlue" and "ButtonGreen", placing them vertically under the red button.</li> </ul><p>Here's how this should look in the designer, with the button names appearing in the list of project components. In this project, you're changing the names of the components rather than leaving them as the default names as you did with <span style="font-style:italic;">HelloPurr</span>. Using meaningful names makes your projects more readable to yourself and others.</p> <p><img src="/sites/all/files/ai2tutorials/paintPot1/PaintPotThreeButtonsVertically.png" /></p> <p>You should also see the three buttons on the phone screen.</p> <h4 class="ai-header">Layout with Screen Arrangement</h4> <p>You should now have three buttons, one above the other. The next step is to make them line up horizontally. You do this using a <strong>HorizontalArrangement</strong> component.</p> <ol><li>From the Palette's Screen Arrangement category, drag out a <strong>HorizontalArrangement</strong> component and place it under the buttons. Change the name of this component from "HorizontalArrangement1" to "ThreeButtons".</li> <li>In the Properties panel, change the <strong>Width</strong> of <span style="font-style:italic;">ThreeButtons</span> to "Fill Parent..." so that it fills the entire width of the screen.</li> <li>Move the three buttons side by side into the <strong>HorizontalArrangement</strong> component. <strong>Hint</strong>: You'll see a blue vertical line that shows where the piece you're dragging will go.</li> </ol><p>If you look in the list of project components, you'll see the three buttons indented under the <span style="font-style:italic;">ThreeButtons</span> to show that they are now its subcomponents. Notice that all the components are indented under <span style="font-style:italic;">Screen1</span>.</p> <p><img src="/sites/all/files/ai2tutorials/paintPot1/PaintPotThreeButtonsHoriz.png" /></p> <p>You should also see your three buttons line up in a row on the phone screen, although things might not look exactly as on the Designer. For example, the Arrangement's outline shows in the Designer but not on the phone.</p> <p>In general, you use Screen Arrangement to create simple vertical or horizontal layouts. You can create more complex layouts by nesting Screen Arrangement components. There is also a <strong>TableArrangement</strong> component (not covered in this tutorial).</p> <h4 class="ai-header">Canvas and wipe button</h4> <p>The final two components are the canvas and the wipe button.</p> <ol><li>From the Palette's Basic category drag a <strong>Canvas</strong> component onto the Viewer. Change its name to "DrawingCanvas". Set its <strong>Width</strong> to "Fill Parent" and set its <strong>Height</strong> to 300 pixels.</li> <li>Add a Background Image to the <strong>Canvas</strong>. Click on the field containing "None..." next to <strong>BackgroundImage</strong> in the canvas's Properties panel. You can use the same kitty.png file, if you still have it on your desktop from an earlier tutorial. Or you can use another image.<br /><pre class="ai-box">You can use any image you like, but you'll get the best results if the size of the image (in pixels) is close to the size at which you'll be showing it on the phone. Also, large images will take a long time to load, and might exceed the memory capacity of the phone allocates for applications.</pre></li> <li>From the Palette, drag the final button onto the screen, placing it under the canvas. Change its <strong>id</strong> to "ButtonWipe" and change its <strong>Text</strong> attribute to "Wipe".</li> </ol><p>You've now completed the steps to set the appearance of your app. Here's how this should look in the Designer. Next, you'll define how the components behave.</p> <p><img src="/sites/all/files/ai2tutorials/paintPot1/PaintPotDesigner.png" /></p> <h4 class="ai-header">Add behaviors to the components</h4> <p>Click the Blocks button to switch to the Blocks Editor. First you will set up the buttons that change the paint color. Later you will add blocks to decide what happens when someone touches or drags the screen.</p> <h4 class="ai-header">Add button event handlers</h4> <p>In the Blocks Editor:</p> <ol><li>Open the drawer for <span style="font-style:italic;">ButtonRed</span> and drag out the <span class="basicblock">when ButtonRed.Click </span> block.</li> <li>Open the <span style="font-style:italic;">DrawingCanvas</span> drawer. Drag out the <span class="setblock">set DrawingCanvas.PaintColor to </span> block (remember that the set block of components is a <a href="/concepts/dropdowns">dropdown</a> so PaintColor is a selection in the dropdown) and place it in the <span class="basicblock">do</span> section of <span class="basicblock">when ButtonRed.Click </span>.<br /><img src="/sites/all/files/ai2tutorials/paintPot1/canvasdropdown.gif" /></li> <li>Open the Colors drawer and drag out the block for the color Red and put it into <span class="componentsetblock">set DrawingCanvas.PaintColor to </span>. (Clicking on a color block after it's been placed will display a table of colors that you can select from.)</li> <li>Repeat steps 2-4 for the blue and green buttons.</li> <li>The final button to set up is the Wipe button. Make a click event handler for <span style="font-style:italic;">ButtonWipe</span> by dragging <span class="eventblock">when ButtonWipe.Click </span> from the <span style="font-style:italic;">ButtonWipe</span> drawer. From the <span style="font-style:italic;">DrawingCanvas</span> drawer, drag <span class="callblock">call DrawingCanvas.Clear </span> and place it in the <span class="eventblock">do</span> area of the <span class="eventblock">when ButtonWipe.Click</span> block.</li> </ol><p>The blocks for the buttons should look like this:</p> <p><img src="/sites/all/files/ai2tutorials/paintPot1/buttonsclick.png" /></p> <h4 class="ai-header">Add Touch-event Handlers</h4> <p>Now for the next step: drawing on the <strong>Canvas</strong>. You'll arrange things so that when you touch the canvas, you get a dot at the spot where you touch. If you drag your finger slowly along the canvas, it draws a line.</p> <ul><li>In the Blocks Editor, open the drawer for the canvas and drag the <span class="eventblock">when DrawingCanvas.Touched</span> block to the workspace. As soon as you drag the block out, you may notice three argument names located at the top of the block <strong>x</strong>, <strong>y</strong>, and <strong>touchedSprite</strong>. These arguments are also known as local variables and can get accessed by using the <span class="getblock">get</span> or <span class="setblock">set</span> block found in the Variables drawer.</li> </ul><p>You've already seen button click events. Clicks are simple, because there's nothing to know about the click other than that it happened. Other event handlers such as <span class="eventblock">when ... Touched</span> need information about the event. In App Inventor, this information is expressed as the value of arguments associated with the event handler. For the <span class="eventblock">when ... Touched</span> event, the first two arguments stand for the x and y coordinates of where the touch happened. We'll save <strong>touchedSprite</strong> for a later tutorial.</p> <ul><li>For this touch event, make the canvas draw a small circle at the point with coordinates (x, y). Drag out a <span class="callblock">call DrawingCanvas.DrawCircle</span> command from the canvas drawer and place it in the <span class="eventblock">do</span> section of <span class="eventblock">when DrawingCanvas.Touched</span>.</li> </ul><p>On the right side of the <span class="callblock">call DrawingCanvas.DrawCircle</span> block are three sockets where you must specify values for the x and y coordinates where the circle should be drawn, and <strong>r</strong>, which is the radius of the circle. For <strong>x</strong> and <strong>y</strong>, you'll use values of the arguments that were supplied to the Touched handler:</p> <ol><li>Open the Variables drawer. Find the <span class="getblock">get</span> block.</li> <li>Drag out two <span class="getblock">get</span> blocks and plug them into the corresponding sockets in the <span class="eventblock">when DrawingCanvas.Touched</span> block. Click on the dropdown to select <strong>x</strong> and <strong>y</strong> in the corresponding block.</li> <li>You'll also need to specify the radius of the circle to draw. Five (pixels) is a good value for this app. Click in a blank area of the screen and type the number 5 followed by return to create a number block with a value of 5. Typing on the blank area of the screen is called <a href="/concepts/typeblocking">typeblocking</a> and is a useful shortcut to know. This can be done for any block, not just numbers. Plug the block for 5 into the radius slot.</li> </ol><p>Here's how the touch event handler should look:</p> <p><img src="/sites/all/files/ai2tutorials/paintPot1/PaintPotTouchHandler.png" /></p> <p>Try out what you have so far on the phone. Touch a color button. Now touch the canvas, and your finger should leave a spot at each place you touch. Touching the Wipe button should clear your drawing.</p> <h4 class="ai-header">Add Drag Events</h4> <p>Finally, add the drag event handler. Here's the difference between a touch and a drag:</p> <ul><li>A touch is when you place your finger on the canvas and lift it without moving it.</li> <li>A drag is when you place your finger on the canvas and move your finger while keeping it in contact.</li> </ul><p>When you drag your finger across the screen, it appears to draw a giant, curved line where you moved your finger. What you're actually doing is drawing hundreds of tiny straight lines: each time you move your finger, even a little bit, you extend the line from your finger's immediate last position to its new position.</p> <p>A drag event comes with 6 arguments. These are three pairs of x and y coordinates that show:</p> <ul><li>The position of your finger back where the drag started.</li> <li>The current position of your finger.</li> <li>The immediately previous position of your finger.</li> </ul><p>There's also a sprite, which we'll ignore for this tutorial.</p> <p>Now make dragging draw a line between the previous position and the current position by creating a drag handler:</p> <ol><li>From the <span style="font-style:italic;">DrawingCanvas</span> drawer, drag the <span class="eventblock">when DrawingCanvas.Dragged</span> block to the workspace.</li> <li>Also from the <span style="font-style:italic;">DrawingCanvas</span> drawer, drag the <span class="callblock-ai2">call DrawingCanvas.DrawLine</span> block into the <span class="eventblock">do</span> slot of the <span class="eventblock">when DrawingCanvas.Dragged</span> block.</li> <li>Drag a <span class="getblock">get</span> block to the open slots in <span class="eventblock">when DrawingCanvas.Dragged</span> and select the corresponding value: <strong>x1</strong> and <strong>y1</strong> should be <strong>prevX</strong> and <strong>prevY</strong>; <strong>x2</strong> and <strong>y2</strong> should be <strong>currentX</strong> and <strong>currentY</strong>.</li> </ol><p>Here's the result:</p> <p><img src="/sites/all/files/ai2tutorials/paintPot1/PaintPotDragHandler.png" /></p> <p>Test your work by trying it on the phone: drag your finger around on the screen to draw lines and curves. Touch the screen to make spots. Use the Wipe button to clear the screen.</p> <p>In <a href="/ai2/paintpot-part2">PaintPot Part 2</a>, you'll see how to use global variables to create dots of different sizes.</p> <h4 class="ai-header">Review</h4> <p>Here are some of the ideas covered in this tutorial:</p> <ul><li>You can use Screen Arrangement components to specify screen layouts other than just placing components one under the other.</li> <li>The <strong>Canvas</strong> component lets you draw on it. It can also sense touches and drags.</li> <li>Some event handlers are called with information about the event, such as the coordinates of where the screen was touched. This information is represented by arguments. When you select an event handler that has arguments, App Inventor creates <span class="argblock">value</span> blocks for these and places them in the My Definitions drawer.</li> </ul><h4 class="ai-header">Scan the Sample App to your Phone</h4> <p>Scan the following barcode onto your phone to install and run the sample app. </p><p><img src="/sites/all/files/tutorials/paintPot1/PaintPotBarcode.png" /></p> <p>Or <a href="/sites/all/files/ai2tutorials/paintPot1/PaintPot.apk">download the apk</a></p> <h4 class="ai-header">Download Source Code</h4> <p>If you'd like to work with this sample in App Inventor, download the <a href="/sites/all/files/ai2tutorials/paintPot1/PaintPot.aia">source code</a> to your computer, then open App Inventor, go to the My Projects page, and choose <b>More Actions | Upload Source</b>.</p> </span></p></div></div></div><section class="field field-name-field-version field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Version:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-version/app-inventor-2" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">App Inventor 2</a> </li> </ul> </section> <section class="field field-name-field-tutorial-difficulty field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Difficulty:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-difficulty/basic" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Basic</a> </li> </ul> </section> <section class="field field-name-field-tutorial-type field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Type:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-type/drawing-canvas" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Drawing Canvas</a> </li> </ul> </section> Wed, 03 Jul 2013 14:47:23 +0000 joanie 357 at http://dev-explore.appinventor.mit.edu http://dev-explore.appinventor.mit.edu/ai2/paintpot-part1#comments MoleMash for App Inventor 2 http://dev-explore.appinventor.mit.edu/ai2/molemash <div class="field field-name-body field-type-text-with-summary field-label-hidden view-mode-rss"><div class="field-items"><div class="field-item even" property="content:encoded"><p><img style="float:right;" src="/sites/all/files/ai2tutorials/moleMash/MoleOnEmulator.png" /></p> <p>In the game <span style="color:green;">MoleMash</span>, a mole pops up at random positions on a playing field, and the player scores points by hitting the mole before it jumps away. This tutorial shows how to build <span style="color:green;">MoleMash</span> as an example of a simple game that uses animation.</p> <p>The tutorial assumes that you have completed the <a href="http://explore.appinventor.mit.edu/ai2/hellopurr">HelloPurr</a> and <a href="/ai2/paintpot-part1">PaintPot</a> tutorials.</p> <p><a href="http://cs.usfca.edu/~wolber/appinventor/bookSplits/ch3MoleMash.pdf">Download Book Chapter (PDF)</a></p> <h4 class="ai-header">Getting Started</h4> <p>Connect to the App Inventor web site and start a new project. Name it "MoleMash", and also set the screen's <strong style="color:green;">Title</strong> to "MoleMash". Open the Blocks Editor and connect to the phone.</p> <p>Also download this <a href="/sites/all/files/ai2tutorials/moleMash/mole.png">picture of a mole</a> and save it on your computer.</p> <h4 class="ai-header">Introduction</h4> <p>You'll design the game so that the mole moves once every half-second. If it is touched, the score increases by one, and the phone vibrates. Pressing restart resets the score to zero.</p> <p>This tutorial introduces:</p> <ul><li>image sprites</li> <li>timers and the <strong>Clock</strong> component</li> <li>procedures</li> <li>picking random numbers between 0 and 1</li> <li>text blocks</li> <li>typeblocking</li> </ul><h4 class="ai-header">The first components</h4> <p>Several components should be familiar from previous tutorials:</p> <ul><li>A <strong>Canvas</strong> named "MyCanvas". This is the area where the mole moves.</li> <li>A <strong>Label</strong> named "ScoreLabel" that shows the score, i.e., the number of times the player has hit the mole.</li> <li>A <strong>Button</strong> named "ResetButton".</li> </ul><p>Drag these components from the Palette onto the Viewer and assign their names. Put <span style="color:green;">MyCanvas</span> on top and set its dimensions to 300 pixels wide by 300 pixels high. Set the <strong style="color:green;">Text</strong> of ScoreLabel to "Score: ---". Set the <strong style="color:green;">Text</strong> of ResetButton to "Reset". Also add a <strong>Sound</strong> component and name it "Noise". You'll use <span style="color:green;">Noise</span> to make the phone vibrate when the mole is hit, similar to the way you made the kitty purr in <span style="color:green;">HelloPurr</span>.</p> <h4 class="ai-header">Timers and the Clock component</h4> <p>You need to arrange for the mole to jump periodically, and you'll do this with the aid of a <strong>Clock</strong> component. The Clock component provides various operations dealing with time, like telling you what the date is. Here, you'll use the component as a timer that fires at regular internals. The firing interval is determined by the Clock 's <strong style="color:green;">TimerInterval</strong> property. Drag out a Clock component; it will go into the non-visible components area. Name it "MoleTimer". Set its TimeInterval to 500 milliseconds to make the mole move every half second. Make sure that <strong style="color:green;">Enabled</strong> is checked.</p> <h4 class="ai-header">Adding an Image Sprite</h4> <p>To add the moving mole we'll use a <em>sprite</em>.</p> <p>Sprites are images that can move on the screen within a Canvas. Each sprite has a <strong style="color:green;">Speed</strong> and a <strong style="color:green;">Heading</strong>, and also an <strong style="color:green;">Interval</strong> that determines how often the sprite moves at its designated speed. Sprites can also detect when they are touched. In <span style="color:green;">MoleMash</span>, the mole has a speed zero, so it won't move by itself. Instead, you'll be setting the mole's position each time the timer fires. Drag an <strong>ImageSprite</strong> component onto the Viewer. You'll find this component in the Animation category of the Palette. Place it within <span style="color:green;">MyCanvas</span> area. Set these properties for the Mole sprite:</p> <ul><li><strong style="color:green;">Picture</strong>: Use mole.png, which you downloaded to your computer at the beginning of this tutorial.</li> <li><strong style="color:green;">Enabled</strong>: checked</li> <li><strong style="color:green;">Interval</strong>: 500 (The interval doesn't matter here, because the mole's speed is zero: it's not moving by itself.)</li> <li><strong style="color:green;">Heading</strong>: 0 The heading doesn't matter here either, because the speed is 0.</li> <li><strong style="color:green;">Speed</strong>: 0.0</li> <li><strong style="color:green;">Visible</strong>: checked</li> <li><strong style="color:green;">Width</strong>: Automatic</li> <li><strong style="color:green;">Height</strong>: Automatic</li> </ul><p>You should see the <strong style="color:green;">x</strong> and <strong style="color:green;">y</strong> properties already filled in. They were determined by where you placed the mole when you dragged it onto <span style="color:green;">MyCanvas</span>. Go ahead and drag the mole some more. You should see <strong style="color:green;">x</strong> and <strong style="color:green;">y</strong> change. You should also see the mole on your connected phone, and the mole moving around on the phone as you drag it around in the Designer. You've now specified all the components. The Designer should look like this. Notice how <span style="color:green;">Mole</span> is indented under <span style="color:green;">MyCanvas</span> in the component structure list, indicating that the sprite is a sub-component of the canvas.</p> <p><img src="/sites/all/files/ai2tutorials/moleMash/MoleMashDesigner.png" /></p> <h4 class="ai-header">Component Behavior and Event Handlers</h4> <p>Now you'll specify the component behavior. This introduces some new App Inventor ideas. The first is the idea of a <em>procedure</em>. For an overview and explanation of procedures, check out the <a href="/ai2/support/concepts/procedures">Procedures page</a>.</p> <p>A procedure is a sequence of statements that you can refer to all at once as single command. If you have a sequence that you need to use more than once in a program, you can define that as a procedure, and then you don't have to repeat the sequence each time you use it. Procedures in App Inventor can take arguments and return values. This tutorial covers only the simplest case: procedures that take no arguments and return no values.</p> <h4 class="ai-header">Define Procedures</h4> <p>Define two procedures:</p> <ul><li><span class="callblock-ai2">MoveMole</span> moves the <span style="color:green;">Mole</span> sprite to a new random position on the canvas.</li> <li><span class="callblock-ai2">UpdateScore</span> shows the score, by changing the text of the <strong style="color:green;">ScoreLabel</strong></li> </ul><p>Start with MoveMole:</p> <ul><li>In the Blocks Editor, under Built-In, open the Procedures drawer. Drag out a <span class="callblock-ai2">to procedure</span> block and change the label "procedure" to "MoveMole".<br /><pre class="box">Note: There are two similar blocks: <span class="callblock-ai2">procedure then do</span> and <span class="callblock-ai2">procedure then resu;t</span>. Here you should use <span class="callblock-ai2">procedure then do</span>.</pre><p>The <span class="callblock-ai2">to MoveMole</span> block has a slot labeled "do". That's where you put the statements for the procedure. In this case there will be two statements: one to set the mole's x position and one to set its y position. In each case, you'll set the position to be a random fraction, between 0 and 1, of the difference between the size of the canvas and the size of the mole. You create that value using blocks for <span class="mathblock-ai2">random fraction</span> and multiplication and subtraction. You can find these in the Math drawer.</p></li> <li>Build the <span style="color:green;">MoveMole</span> procedure. The completed definition should look like this:<br /><img src="/sites/all/files/ai2tutorials/moleMash/MoveMole.png" /><br /><span style="color:green;">MoveMole</span> does not take any arguments so you don't have to use the <a href="/ai2/support/concepts/mutators">mutator</a> function of the procedure block. Observe how the blocks connect together: the first statement uses the <span class="setblock">Mole.X set</span> block to set mole's horizontal position. The value plugged into the block's socket is the result of multiplying: <ol><li>The result of the <span class="mathblock-ai2">call random fraction</span> block, which a value between 0 and 1</li> <li>The result of subtracting the mole's width from the canvas width</li> </ol><p>The vertical position is handled similarly.</p></li> </ul><p>With <span style="color:green;">MoveMole</span> done, the next step is to define a variable called <span style="color:green;">score</span> to hold the score (number of hits) and give it initial value 0. Also define a procedure <span class="callblock-ai2">UpdateScore</span> that shows the score in <span style="color:green;">ScoreLabel</span>. The actual contents to be shown in <span style="color:green;">ScoreLabel</span> will be the text "Score: " joined to the value of <span style="color:green;">score</span>.</p> <ul><li>To create the "Score: " part of the label, drag out a text block from the Text drawer. Change the block to read "Score: " rather than "text".</li> <li>Use a join block to attach this to a block that gives the value of the score variable. You can find the join block in the Text drawer.</li> </ul><p>Here's how <span style="color:green;">score</span> and <span class="callblock-ai2">UpdateScore</span> should look:</p> <p><img src="/sites/all/files/ai2tutorials/moleMash/UpdateScore.png" /></p> <h4 class="ai-header">Add a Timer</h4> <p>The next step is to make the mole keep moving. Here's where you'll use <span style="color:green;">MoleTimer</span>. Clock components have an event handler called <span class="eventblock">when ... Timer</span> that triggers repeatedly at a rate determined by the <strong style="color:green;">TimerInterval</strong>.</p> <p>Set up <span style="color:green;">MoleTimer</span> to call <span class="callblock-ai2">MoveMole</span> each time the timer fires, by building the event handler like this:</p> <p><img src="/sites/all/files/ai2tutorials/moleMash/MoleMashTimerEventHandler.png" /></p> <pre class="ai-box">Notice how the mole starts jumping around on the phone as soon as you define the event handler. This is an example of how things in App Inventor start happening instantaneously, as soon as you define them.</pre><h4 class="ai-header">Add a Mole Touch Handler</h4> <p>The program should increment the score each time the mole is touched. Sprites, like canvases, respond to touch events. So create a touch event handler for <span style="color:green;">Mole</span> that:</p> <ol><li>Increments the score.</li> <li>Calls <span class="callblock-ai2">UpdateScore</span> to show the new score.</li> <li>Makes the phone vibrate for 1/10 second (100 milliseconds).</li> <li>Calls <span class="callblock-ai2">MoveMole</span> so that the mole moves right away, rather than waiting for the timer.</li> </ol><p>Here's what this looks like in blocks. Go ahead and assemble the <span class="eventblock">when Mole.Touched</span> blocks as shown.</p> <p><img src="/sites/all/files/ai2tutorials/moleMash/MoleMashTouchEventHandler.png" /></p> <p>Here's a tip: You can use <a href="/tips/typeblocking">typeblocking</a>: typing to quickly create blocks.</p> <ul><li>To create a value block containing 100, just type 100 and press return.</li> <li>To create a <span class="callblock-ai2">MoveMole</span> block, just type <span class="callblock-ai2">MoveMole</span> and select the block you want from the list</li> </ul><h4 class="ai-header">Reset the Score</h4> <p>One final detail is resetting the score. That's simply a matter of making the <span style="color:green;">ResetButton</span> change the score to 0 and calling <span class="callblock-ai2">UpdateScore</span>.</p> <h4 class="ai-header">Complete Program</h4> <p>Here's the complete <span style="color:green;">MoleMash</span> program:</p> <p><img src="/sites/all/files/ai2tutorials/moleMash/MoleMashComplete.png" /></p> <h4 class="ai-header">Variations</h4> <p>Once you get the game working, you might want to explore some variations. For example:</p> <ul><li>Make the game vary the speed of the mole in response to how well the player is doing. To vary how quickly the mole moves, you'll need to change the <span style="color:green;">MoleTimer</span>'s <strong style="color:green;">Interval</strong> property.</li> <li>Keep track of when the player hits the mole and when the player misses the mole, and show a score with both hits and misses. To do this, you'll need do define touched handlers both for <span style="color:green;">Mole</span>, same as now, and for <span style="color:green;">MyCanvas</span>. One subtle issue, if the player touches the mole, does that also count as a touch for <span style="color:green;">MyCanvas</span>? The answer is yes. Both touch events will register.</li> </ul><h4 class="ai-header">Review</h4> <p>Here are some of the ideas covered in this project:</p> <ul><li>Sprites are touch-sensitive shapes that you can program to move around on a <strong>Canvas</strong>.</li> <li>The <strong>Clock</strong> component can be used as a timer to make events that happen at regular intervals.</li> <li>Procedures are defined using <span class="callblock-ai2">to</span> blocks.</li> <li>For each procedure you define, App Inventor automatically creates an associated call block and places it in the My Definitions drawer.</li> <li>Making a <span class="mathblock-ai2">random-fraction</span> block produces a number between 0 and 1.</li> <li>Text blocks specify literal text, similar to the way that number blocks specify literal numbers.</li> <li><a href="/tips/typeblocking">Typeblocking</a> is a way to create blocks quickly, by typing a block's name.</li> </ul><h4 class="ai-header">Scan the Sample App to your Phone</h4> <p>Scan the following barcode onto your phone to install and run the sample app. </p><p><img src="/sites/all/files/ai2tutorials/moleMash/MoleMashBarcode.png" /></p> <h4 class="ai-header">Download Source Code</h4> <p>If you'd like to work with this sample in App Inventor, download the <a href="/sites/all/files/ai2tutorials/moleMash/MoleMash.aia">source code</a> to your computer, then open App Inventor, go to the My Projects page, and choose <b>More Actions | Upload Source</b>.</p> <pre class="ai-box">Done with <span style="color:black;">MoleMash</span>? Return to the other tutorials <a href="/ai2/tutorials">here</a>.</pre></div></div></div><section class="field field-name-field-version field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Version:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-version/app-inventor-2" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">App Inventor 2</a> </li> </ul> </section> <section class="field field-name-field-tutorial-difficulty field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Difficulty:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-difficulty/basic" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Basic</a> </li> </ul> </section> <section class="field field-name-field-tutorial-type field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Type:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-type/clock-timer" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Clock Timer</a> </li> <li class="field-item odd"> <a href="/tutorial-type/game" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Game</a> </li> <li class="field-item even"> <a href="/tutorial-type/sprites" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Sprites</a> </li> </ul> </section> Wed, 03 Jul 2013 14:29:42 +0000 aaron 356 at http://dev-explore.appinventor.mit.edu http://dev-explore.appinventor.mit.edu/ai2/molemash#comments Magic 8-ball for App Inventor 2 http://dev-explore.appinventor.mit.edu/ai2/magic-8-ball <div class="field field-name-body field-type-text-with-summary field-label-hidden view-mode-rss"><div class="field-items"><div class="field-item even" property="content:encoded"><h3>Lesson One: Magic 8-Ball Predicts the Future</h3> <p><img src="/sites/all/files/ai2tutorials/magic8ball/8ball.jpg" style="float:left;width:75px;" /></p> <p>This introductory module will guide you through building a “Magic 8-Ball” app with App Inventor 2. When activated, your 8-ball will deliver one of its classic predictions, such as “It is decidedly so” or “Reply hazy, try again.”</p> <h3>Learning Goals</h3> <p>After completing this app, you will be able to:</p> <ul><li>Navigate the App Inventor environment: designer, blocks editor, emulator and/or physical phone</li> <li>Correctly use the following App Inventor components: accelerometer sensor, button, sound</li> <li>Correctly use the following App Inventor concepts: making and using a list, responding to an event</li> </ul><h3>Materials</h3> <ul><li>A selection of images and sounds are available at the <a href="/media-library">App Inventor Media Library</a>.</li> <li>Optional supporting hard copy materials for the original App Inventor version of this tutorial such as the <a href="/media-library">App Inventor Overview Handouts</a>, and the <a href="https://docs.google.com/viewer?a=v&amp;pid=sites&amp;srcid=ZGVmYXVsdGRvbWFpbnxhcHBpbnZlbnRvcmxlc3NvbnN8Z3g6MjJkNmViOGQ3MDNmZWYwZQ">PDF version</a> of this lesson.</li> </ul><h3>Outline</h3> <ol><li><a href="/content/setup">Set up</a> computers and phones or emulators. (Suggestion: do this ahead of time)</li> <li>Part One:<a href="#m8one">Click a Button, Hear a Sound</a> </li> <li>Part Two: <a href="#m8two">Click the Button, Get a Prediction + Hear a Sound</a> </li> <li>Part Three: <a href="#m8three">Shake the Phone, Get a Prediction + Hear a Sound</a> </li> <li><a href="#m8four">Suggestions for further exploration:</a> Text-to-Speech, Rotating image, Custom prediction lists</li> </ol><h3><a name="m8one" id="m8one">Part One: Click a Button, Hear a Sound</a></h3> <p>The final Magic 8-Ball App will deliver a prediction from a list that you have designed. To get started, first we'll make a button with a picture on it, and program it to play a sound when the button is clicked.</p> <p style="color:green;">DESIGN: App Inventor Designer</p> <ol><li>To open the App Inventor Designer window, go to <a href="http://newblocks.appinventor.mit.edu">http://newblocks.appinventor.mit.edu</a>. See setup instructions above if you are not sure how to sign in.</li> <li>If you have already made an app (such as Hello Purr), you will automatically be directed to the Designer with the last project you worked on showing. Click "My Projects" in the upper left corner of the screen, which will take you to your list of projects. Click "New" and name your project something like "Magic8Ball"(note: spaces are not allowed).<br /><img src="/sites/all/files/ai2tutorials/magic8ball/newproject.png" /></li> <li>Download one image and one sound file from below to be used in your app. Right click (control-click) on the link of the image or sound, then choose "Download" or "Save As". Save the media files to a convenient location that you will remember. <ul><li><a href="http://dl.dropbox.com/u/90819513/Cha_Ching_Sound.mp3">ChaChing Sound</a></li> <li><a href="http://dl.dropbox.com/u/90819513/Clinking_Teaspoon_Sound.mp3">Clinking Teaspoon Sound</a></li> <li><a href="http://dl.dropbox.com/u/90819513/Ta%20Da%20sound.mp3">Tada Sound</a></li> <li><a href="http://dl.dropbox.com/u/90819513/image_8_ball.jpeg">Magic 8 Ball Image</a></li> <li><a href="http://dl.dropbox.com/u/90819513/magic8ball3.jpeg">Blank 8 Ball Image</a></li> </ul></li> <li>On the left column of the Designer, open the Basic palette, and drag a Button component over to the Viewer(#1).</li> <li>Set the button image to an 8-Ball image:<br />Click on your newly added button to see its properties in the Properties pane on the right. Under "Image" click on the word "None..." and a small selection window will pop up (#2). Click the "Add" button and browse to where you saved the 8-Ball image. Select the file, then click “OK” to close the selection window. Click “OK” again on the properties pane to close the small popup window (#3).</li> <li>Go to the text field in the Properties pane and delete the display text of your button component (#4).<br /><img src="/sites/all/files/ai2tutorials/magic8ball/newbutton.png" /><br /><img src="/sites/all/files/ai2tutorials/magic8ball/buttonproperties.png" /></li> <li>From the Media palette, drag over a Sound component onto the Viewer pane (#1). Notice that since the sound will not be a visible part of the app, it appears at the bottom of the Viewer pane, as a “Non-visible component”.</li> <li>Set the sound component's source file:<br />Click on your newly added sound component to see its properties in the Properties pane on the right. Under "Source" click in the small box on the word "None..." and a small selection window will pop up (#2). Click the "Add" button and browse to where you saved the sound file. Select the sound file, then click “OK” to close the selection window. Click “OK” again on the properties pane to close the small popup window (#3). </li> <p><img src="/sites/all/files/ai2tutorials/magic8ball/soundproperties.png" /></p><li>You have now completed the work in the Designer for Part One of this app. It's time now to go over to the Blocks Editor to program the behavior of these components.</li> </ol><p style="color:green;">BUILD: Blocks Editor</p> <p>In the upper right corner of the Designer, click on the Blocks button.</p> <p>Now you are going to tell your app how to behave when the button is clicked. This is actually very simple in App Inventor, because the "code" for the program only consists of two blocks!</p> <p>Once the Blocks Editor is open, there are several options running along the left side of the screen. We refer to these as "Palettes" with “Drawers.” </p> <p>From the blocks palette located under Screen1, click on the Button1 drawer. Drag the <span class="eventblock">when Button1.Click</span> block into the work area (#1). From the blocks palette, click on the Sound1 drawer, drag the <span class="callblock">Sound1.Play</span> block into the work area and insert it into the <span class="eventblock">when Button1.Click</span> block (#2). They will click together like magnetic puzzle pieces.</p> <p><img src="/sites/all/files/ai2tutorials/magic8ball/blocks_drag.png" /></p> <p>Your blocks should now look like this:</p> <p><img src="/sites/all/files/ai2tutorials/magic8ball/button1.click.png" /></p> <p>That's it! You've written the program for Part One of Magic 8-Ball. Now it's time to test that it's working right.</p> <p style="color:green;">TEST: Phone/Emulator</p> <p>You have now built an app! To test that it works, you either have to launch an emulator, or connect to a phone. Go back to the <a href="/ai2/setup">Setup Instructions</a> if you do not have a phone or an emulator running.</p> <p><b>Emulator</b>: click on the picture, you will hear the sound play.<br /><b>Phone</b>: tap the picture, you will hear the sound play.</p> <pre class="ai-box">Note: If you don't hear the sound, first be sure you have the volume turned up on your device (or computer if using emulator). Also, make sure your device has an SD card. App Inventor stores media files to the SD card. In some devices, the Play component does not work correctly. You will need to use the Player component instead of the Sound component.</pre><h3><a name="#m8two" id="#m8two">Part Two</a>: Output a Prediction</h3> <p>Now that we've gotten the button to perform an action (play a sound), we want to extend that action to include giving the user a prediction. First we'll need two labels: Label1 will display the instructions, and Label2 will display the chosen prediction. We'll use blocks to program a "list picker" to choose from a list of predictions. Each time the button is clicked, the app will change the text of Label2 to display the chosen prediction.</p> <p style="color:green;">DESIGN: App Inventor</p> <p>Go back to the Designer window in your browser and add some new things to your app.</p> <ol><li>From the Screen Arrangement palette, drag over the Vertical Arrangement component (#1). At first it will just look like an empty box, but when you put things in it, App Inventor will know that you want to line them up vertically (one on top of the other).</li> <p><img src="/sites/all/files/ai2tutorials/magic8ball/viewer_addvertarr.png" /></p> <li>From the Basic palette, drag over a Label component (#2) and drop it inside of the vertical arrangement component. In the Properties pane, change the "Text" property of Label1 to “Ask the Magic 8-Ball a question”.(#3)</li> <p><img src="/sites/all/files/ai2tutorials/magic8ball/vert_addlabel.png" /></p> <li>From the Basic palette, drag over another Label component (Label2) into the Vertical Arrangement box so that it sits right below Label1. Change the "Text" property of the Label2 to “Touch the Magic 8-Ball to receive your answer.” Now drag the 8-Ball image so that it is also inside the Vertical Arrangement component on top of the two labels. This will cause them to line up with each other in a vertical line. (Note: this can be tricky mouse work, but get them in there just right and the vertical arrangement will resize itself to fit everything.)</li> </ol><p><img src="/sites/all/files/ai2tutorials/magic8ball/align.png" /></p> <p>Now it’s time to go back into the Blocks Editor to program the components you just added to your project.</p> <p style="color:green;">BUILD: Blocks Editor</p> <p>Now for the fun part! You're going to make a list of predictions and program the button to pick one item from the list and display it inside Label2. The button will also still play the sound that you programmed in Part One. Here's how to do it...</p> <ol><li>From the blocks palette, click on Label2 drawer to see all of its associated blocks. Drag over the green <span class="componentsetblock">set Label2.BackgroundColor</span> and insert it just above the <span class="callblock">Sound1.Play</span> block. Notice that the <span class="eventblock">when Button1.Click</span> block automatically gets bigger to accomodate the new block.</li> <li>Clicking on the word "BackgroundColor" will allow you to change the property that is being set. We want to change the Text so our block will look like <span class="componentsetblock">set Label2.Text</span>. This is called a <a href="/support/concepts/mutators">mutator</a> block.<br /><img src="/sites/all/files/ai2tutorials/magic8ball/Label2.Text.png" /></li> <li>From the Built-In palette, click on the Lists drawer. Drag over the <span class="listblock">pick random item</span> block and connect it to the open socket of the <span class="componentsetblock">set Label2.Text</span> block.<br /><img src="/sites/all/files/ai2tutorials/magic8ball/Listdrawer.png" /></li> <li>From the Built-In palette, click on Lists again, then drag out the <span class="listblock">make a list</span> block and plug it into the "list" socket on the right side of the <span class="listblock">pick random item</span> block.</li> <li>From the Built-In palette, click on the Text drawer, drag out a <span class="textblock">text</span> block and connect it to the item socket of the <span class="listblock">make a list</span> block. Click directly on the word “text” so that it gets highlighted. You can then type in new text there. Think about the sayings you want in your list of predictions for the Magic 8-Ball. Type the first prediction into this new text block.</li> <li>Notice after you plug in two text blocks, there are no more sockets to add more responses. To create more sockets, you need to click the blue plus button. <span class="listblock">make a list</span> is a <a href="/support/concepts/mutators">mutator</a> block and thus can be expanded or shrunk by clicking the blue plus sign.<br /><img src="/sites/all/files/UserGuide/blocks/lists/makealist.gif" /></li> <li> Plug each text block into the <span class="ai-blocks">pick random item</span> block. (Ideas for answers: <a href="http://en.wikipedia.org/wiki/Magic_8-Ball">http://en.wikipedia.org/wiki/Magic_8-Ball</a>)</li> </ol><p>Blocks should look something like this:</p> <p><img src="/sites/all/files/ai2tutorials/magic8ball/finalblocks.png" /></p> <p>You've got a Magic 8-Ball App! Now your app is fully functional and will predict the future with absolute certainty. Test out that this works, and then come back for some challenge tasks to make the app even more fun.</p> <p style="color:green;">TEST: Emulator or Phone</p> <p><b>Emulator</b>: Click on the picture of the 8-Ball, you should see one of your answers displayed in the Label2.Text field, followed by the sound.<br /><b>Phone</b>: Tap on the picture of the 8-Ball, you should see one of your answers displayed in the Label2.Text field, followed by the sound.</p> <h3><a name="#m8three" id="#m8three">Part Three</a>: Shake the Phone, Get a Prediction</h3> <p>Even though you have a working Magic 8-Ball app, there is a way to make it even more fun. You can use the accelerometer component to make the phone respond to shaking instead of responding to a button click. This will make the app much more like a real Magic 8-Ball toy. <em>Note: This part can only be done with an actual phone or tablet equipped with an accelerometer. If you are using an emulator, skip this part and go to Challenge 1 instead.</em></p> <p style="color;green;">DESIGN: App Inventor</p> <p>From the Sensors palette, drag over an <b>AccelerometerSensor</b> sensor component. Notice that it automatically drops down to the “Non-visible components” area of the Viewer window. This is the only new component you need, so go on over to the Blocks Editor to change your program.</p> <p style="color:green;">BUILD: Blocks Editor</p> <ol><li>From the blocks drawer, click on AccelerometerSensor, then drag out the block for <span class="eventblock">when AccelerometerSensor.Shaking</span>.</li> <li>Disconnect all of the blocks from inside the <span class="eventblock">Button1.Click</span> block and move them inside the <span class="eventblock">AccelerometerSensor.Shaking</span> block. <b>NOTE:</b> you can move whole sections of connected blocks by clicking on the uppermost or leftmost block and dragging it. The connected blocks will come with it.</li> <li>Delete the <span class="eventblock">Button1.Click</span> block to keep your work area tidy.</li> </ol><p>The blocks should look something like this:</p> <p><img src="/sites/all/files/ai2tutorials/magic8ball/shaking.png" /></p> <p style="color:green;">TEST: Phone/Emulator</p> <p><b>Phone</b>: When you shake the phone it should show an answer and play a sound.<br /><b>Emulator</b>: unfortunately, you can not simulate shaking the phone when using the emulator.</p> <p style="color:green;">Package the App to Your Phone!</p> <p>Your app would disappear if you were to disconnect your phone from the Blocks Editor. This is because the app is still stored on the App Inventor server and not on your phone. Follow <a href="/support/packaging-apps">these instructions</a> to package your app to your phone or to make an ".apk" file that can be installed on any android phone. Or, if you want to make your app even cooler, try the challenges below.</p> <h3>Challenge 1: Make the Magic 8-Ball Speak</h3> <p>Instead of (or in addition to) making the prediction appear as text, can you make the 8-Ball speak it aloud? Hint: the text-to-speech component is under the <b>Other Stuff</b> palette in the Designer. <em>Note: Most Android devices have the text-to-speech (TTS) capability, but if you have trouble getting the TTS component in App Inventor to work, you may need to find out how to install TTS and/or enable TTS on your device.</em></p> <h3><a name="#m8four" id="#m8four">Suggestions for Further Exploration</a></h3> <p> </p><ul><li>Make the image rotate when the phone is shaken or have several images that the app rotates through while the phone is shaken. You could use this technique to make it look like the triangle piece inside the 8-ball window is surfacing. You could also make different images for different predictions and display the correct image for each prediction.</li> <li>Make a similar app but for a different purpose. The phone could be used in place of dice or yahtzee letters. It could simulate a coin toss or a random number or color generator for investigating probability.</li> <li>Ask end users to add choices to the list of predictions (See <a href="/ai2/makequiz">Make Quiz</a> tutorial).</li> <li>"Crowd source" for prediction choices: allow people to send text messages and have the app add them to the list.</li> <li>Make the 8 Ball app a "server" so that anyone who sends a text to it will receive a text message prediction in return.</li> <li>Complete change the list to humorous choices (e.g. an app for teacher to use when a student has an excuse for not doing homework), or for useful purposes like randomly selecting a name from amongst people in the class.</li> </ul><pre class="ai-box">Done with Magic 8-Ball? Return to <a href="/teach">Curriculum (Teacher Resources)</a> or <a href="/ai2/tutorials">Tutorials</a>.</pre><h4 class="ai-header">Scan the Sample App to your Phone</h4> <p>Scan the following barcode onto your phone to install and run the sample app. </p><p><img src="/sites/all/files/ai2tutorials/magic8ball/Magic8BallBarcode.png" /></p> <p>Or <a href="/sites/all/files/ai2tutorials/magic8ball/Magic8Ball.apk">download the apk</a></p> <h4 class="ai-header">Download Source Code</h4> <p>If you'd like to work with this sample in App Inventor, download the <a href="/sites/all/files/ai2tutorials/magic8ball/Magic8Ball.aia">source code</a> to your computer, then open App Inventor, go to the My Projects page, and choose <b>More Actions | Upload Source</b>.</p> </div></div></div><section class="field field-name-field-version field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Version:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-version/app-inventor-2" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">App Inventor 2</a> </li> </ul> </section> <section class="field field-name-field-tutorial-difficulty field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Difficulty:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-difficulty/basic" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Basic</a> </li> </ul> </section> <section class="field field-name-field-tutorial-type field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Type:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-type/accelerometer" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Accelerometer</a> </li> </ul> </section> Tue, 02 Jul 2013 20:43:39 +0000 joanie 355 at http://dev-explore.appinventor.mit.edu http://dev-explore.appinventor.mit.edu/ai2/magic-8-ball#comments Hello Purr for App Inventor 2 http://dev-explore.appinventor.mit.edu/ai2/hellopurr <div class="field field-name-body field-type-text-with-summary field-label-hidden view-mode-rss"><div class="field-items"><div class="field-item even" property="content:encoded"><h2 class="ai-header">Building your first app: HelloPurr</h2> <p>Now that you've set up your computer and device, and you've learned how the Designer and the Blocks Editor work, you are ready to build the HelloPurr app. At this point, you should have the Designer or Blocks Editor open in your browser, and either an Android device or an Android emulator connected to the Blocks Editor. (See <a href="/ai2/setup">Setup Instructions for App Inventor 2</a> if you do not have these things running.)</p> <h3>HelloPurr: tap the kitty, hear him meow</h3> <p>HelloPurr is a simple app that you can build in a very short time. You create a button that has a picture of a cat on it, and then program the button so that when it is clicked a "meow" sound plays.</p> <p>To build HelloPurr, you'll need a image file of a cat and an audio file with a "meow" sound. Download these files to your computer by clicking the following links. To download: after clicking a link, right click on the image or sound bar and select "Save As." Save both files onto your desktop or downloads folder, or anywhere that you can easily find later.</p> <ul><li>Kitty picture: <a href="/sites/all/files/ai2tutorials/helloPurr/kitty.png"> kitty.png</a> (Right-click and Save)</li> <li>Meow sound: <a href="/sites/all/files/ai2tutorials/helloPurr/meow.mp3">meow.mp3</a> (Right-click and Save)</li> </ul><h3>Select components to design your app</h3> <p><img src="/sites/all/files/ai2tutorials/helloPurr/basic_palette.png" style="float:left;" /></p> <p>The App Inventor <b>Components</b> are located on the left hand side of the <em>Designer Window</em> under the title <b>Palette</b>. Components are the basic elements you use to make apps on the Android phone. They're like the ingredients in a recipe. Some components are very simple, like a <b>Label</b> component, which just shows text on the screen, or a <b>Button</b> component (#1 left) that you tap to initiate an action. </p> <p>Other components are more elaborate: a drawing <b>Canvas</b> (#2 left) that can hold still images or animations, an <b>Accelerometer</b> sensor that works like a Wii controller and detects when you move or shake the phone, components that send text messages, components that play music and video, components that get information from Web sites, and so on.</p> <p>To use a component in your app, you need to click and drag it onto the viewer in the middle of the <b>Designer</b>. When you add a component to the <b>Viewer</b> (#1 below), it will also appear in the components list on the right hand side of the Viewer.</p> <p>Components (#2 below) have properties that can be adjusted to change the way the component appears or behaves within the app. To view and change the properties of a component (#3 below), you must first select the desired component in your list of components.</p> <p><a href="/sites/all/files/ai2tutorials/helloPurr/viewer.png"><img src="/sites/all/files/ai2tutorials/helloPurr/viewer.png" /></a></p> <h3>Steps for selecting components and setting properties</h3> <p><span style="color:green;">HelloPurr</span> will have a <b>Button</b> component that displays the image of the kitty you downloaded earlier. To accomplish this:</p> <p><b>Step 1a</b>. From the <b>Basic</b> palette, drag and drop the <b>Button</b> component to Screen1 (#1). </p> <p><img src="/sites/all/files/ai2tutorials/helloPurr/clickdrag.gif" /></p> <p><b>Step 1b</b>.To make the button have an image of a cat, in the <b>Properties</b> pane, under Image, click on the text <em>"None..."</em> and click <em>"Upload New…"</em> (#2). A window will pop up to let you choose the image file. Click "Browse" and then navigate to the location of the <em>kitty.png</em> file you downloaded earlier (#3). Click the <em>kitty.png</em> file, click "Open", and then click "OK".</p> <p><a href="/sites/all/files/ai2tutorials/helloPurr/upload.png"><img src="/sites/all/files/ai2tutorials/helloPurr/upload.png" /></a></p> <hr /><p><b>Step 2</b>. Change the Button's <b>Text</b> property: Delete "Text for Button1", leaving the Button's text property blank so that there is no writing over the kitty's face. Your Designer should look like this:</p> <blockquote><p>If the entire kitty picture is not showing up, you can fix this by setting the Height and Width properties of the button to "Fill Parent". To do this, click on the Button component, go to the right-hand Properties pane, scroll down to the very bottom to where it says Width and click on the word "Automatic..." to activate the drop down list. Choose "Fill Parent". Do the same for the Height property.</p></blockquote> <p><a href="/sites/all/files/ai2tutorials/helloPurr/kitty.png"><img src="/sites/all/files/ai2tutorials/helloPurr/kitty.png" /></a></p> <hr /><p><b>Step 3</b>. From the <b>Basic</b> palette, drag and drop the <b>Label</b> component to the Viewer (#1), placing it below the picture of the kitty. It will appear under your list of components as <span style="color:green;">Label1</span>.</p> <p>Under the <b>Properties</b> pane, change the <b>Text</b> property of Label1 to read "Pet the Kitty" (#2). You'll see the text change in the Designer and on your device. Change the FontSize of Label1 to 30 (#3). Change the BackgroundColor of Label1 by clicking on the box (#4): you can change it to any color you like. Change the TextColor of Label1 (#5) to any color you like. Here, the background color is set to blue and the text color is set yellow. </p> <p><a href="/sites/all/files/ai2tutorials/helloPurr/label.png"><img src="/sites/all/files/ai2tutorials/helloPurr/label.png" /></a></p> <hr /><p><b>Step 4</b>. Under Palette, click on the <b>Media</b> drawer and drag out a <b>Sound</b> component and place it in the Viewer (#1). Wherever you drop it, it will appear in the area at the bottom of the Viewer marked <b>Non-visible components</b>. Under the <b>Media</b> pane, Click <em>Upload New...</em> (#2) Browse to the location of the <b>meow.mp3</b> file that you downloaded earlier and upload it to this project (#3). Under the Properties pane, see that the <b>Source</b> property currently says <em>None...</em>. Click the word <em>None...</em> to change the Sound1 component's <b>Source</b> to <em>meow.mp3</em> (#4).</p> <p><img src="/sites/all/files/ai2tutorials/helloPurr/sound.png" /></p> <h3>Programming with the Blocks Editor</h3> <p>So far you have been arranging your app's screen and components in the <em>Designer</em>, which is in a web browser window. To start programming the behavior of the app, you need to go to the <em>Blocks Editor</em>. Click the Blocks button to go to the Blocks Editor.</p> <p><img src="/sites/all/files/ai2tutorials/helloPurr/blocks.png" /></p> <p>Once you have the Blocks Editor in front of you, continue to the next step to start programming your app with blocks.</p> <h3>Making the sound play</h3> <p><b>Step 1</b>. On the left side of the Blocks Editor, click the <b>Button1</b> drawer to open it. Drag and drop the <span class="blocks">Button1.Click</span> block in the work area (the open area on the right). </p> <p><img src="/sites/all/files/ai2tutorials/helloPurr/buttonblocks.png" /></p> <p>Those mustard yellow blocks are called <b>event handler</b> blocks. The event handler blocks specifiy how the phone should respond to certain events: a button has been pressed, the phone is being shaken, the user is dragging her finger over a canvas, etc. The event handler blocks are mustard yellow in color and use the word <em>when</em>. For example, <span class="eventblock">when Button1.Click</span> is an event handler</p> <p>.</p> <p><b>Step 2</b>. Click the <b>Sound1</b> drawer and drag the <span class="blocks">Sound1.Play</span> block and connect it to the "do" section of the <span class="blocks">when Button1.Click</span> block. The blocks connect together like puzzle pieces and you can hear a clicking sound when they connect.</p> <p><a href="/sites/all/files/ai2tutorials/helloPurr/soundblocks.png"><img src="/sites/all/files/ai2tutorials/helloPurr/soundblocks.png" /></a></p> <p>The purple blocks are called <b>command</b> blocks, which are placed in the body of event handlers. When an event handler is executed, it runs the sequence of commands in its body. A command is a block that specifies an action to be performed (e.g., playing sound) when the event (e.g., pressing Button1) is triggered.</p> <p>Your blocks should look like this at this point:</p> <p><img src="/sites/all/files/ai2tutorials/helloPurr/button1.click.png" /></p> <p>Now you can see that the <b>command block</b> is in the <b>event handler</b>. This set of blocks means; "when Button1 is clicked, Sound1 will play." The event handler is like a category of action (e.g., a button is being clicked), and the command specifies the type of action and the details of the action (e.g., playing sound a specified sound).</p> <p>You can read more about the blocks and how they work here: <a href="/ai2/support/blocks">Understanding Blocks in App Inventor 2</a>.</p> <p>Try It! When you click the button you should hear the kitty meow. Congratulations, your first app is running! </p> <pre class="box">Note: there is a known issue with the Sound component on some devices. If you see an "OS Error" and the sound does not play - or is very delayed in playing, go back into the Designer and try using a Player component (found under Media) instead of the Sound component.</pre><h3>Packaging your app</h3> <p>While your device (emulator or phone/tablet) has been connected to App Inventor, your app has been running in real time on your device. If you disconnect the emulator/phone/tablet from the Blocks Editor, the app will vanish. You can always make it return by reconnecting the device. To have an app running without being connected to App Inventor, you must "<b>package</b>" the app to produce an application package (apk file).</p> <p>To "package" the app to your phone, connect your phone to the Blocks Editor and choose "Package for Phone" at the upper right of the designer page. App Inventor will present three options for packaging:</p> <p><img src="/sites/all/files/ai2tutorials/helloPurr/package.png" /></p> <p>1. <b>Show Barcode:</b> You can generate a Barcode (a QR Code), which you can use to install the app on a phone or tablet that has a camera, with the aid of a barcode scanner, like the ZXing barcode scanner (freely available in Google Play). </p> <p style="color:red;">Note: this barcode works only for your own device because it is associated with your google account. If you want to share your app with others via barcode, you'll need to download the .apk file to your computer and use a third-party software to convert the file into a barcode. More information can be found <a href="/support/packaging-apps">here</a>.</p> <p>2. <b>Download to this Computer:</b> You can download the app to your computer as an apk file, which you can distribute and share as you like by manually installing it on other devices. (sometimes called <a href="http://www.techrepublic.com/blog/smartphones/how-to-side-load-apps-on-your-android-device/3114" target="blank">"side loading"</a>.</p> <p>3. <b>Generate YAIL</b> You can generate the YAIL code of all of your blocks.</p> <h3>Challenge! Make the cat purr</h3> <p> The challenge is to have the cat purr when the phone is shaken. Go to the Blocks Editor and open the <b>Sound1</b> drawer and drag the <span class="blocks">Sound1.Vibrate</span> block and place it under the <span class="blocks">Sound1.Play</span> block.</p> <p><img src="/sites/all/files/ai2tutorials/helloPurr/vibrate.png" /></p> <p>The <span class="blocks">Sound1.Vibrate</span> block has an open slot, which means you need to plug something into it to specify more about how the behavior should work. Here, we want to specify the length of the vibration. Numbers are calculated in thousandths of a second (milliseconds): to make the phone vibrate for half a second, we need to plug in a value of <b>500 milliseconds</b>.</p> <p>In the Built-In palette, go to the <b>Math</b> drawer, drag the number block and place it at the socket of the <span class="blocks">Sound1.Vibrate</span>.</p> <p><img src="/sites/all/files/ai2tutorials/helloPurr/selectnumber.png" /></p> <p>After you place the number block, click the number "0". It highlights the number in black: type "500" with your keyboard.</p> <p><img src="/sites/all/files/ai2tutorials/helloPurr/change0to500.png" /><br /><img src="/sites/all/files/ai2tutorials/helloPurr/done.png" /></p> <p>Done!</p> <p>Now connect your phone and tap the image of the cat on the phone. The phone should vibrate <em>and</em> meow at the same time.</p> <h4>Review</h4> <p>Here are the key ideas covered so far:</p> <ul><li>You build apps by selecting components (ingredients) and then telling them what to do and when to do it.</li> <li>You use the <strong>Designer</strong> to select components and set each component's properties. Some components are visible and some aren't.</li> <li>You can add media (sounds and images) to apps by uploading them from your computer.</li> <li>You use the <strong>Blocks Editor</strong> to assemble blocks that define the components' behavior</li> <li><span class="eventblock">when ... do ...</span> blocks define <em>event handlers</em>, that tell components what to do <em>when</em> something happens.</li> <li><span class="blocks">call ...</span> blocks tell components to do things.</li> </ul><h4 class="ai-header">Scan the Sample App to your Phone</h4> <p>Scan the following barcode onto your phone to install and run the sample app. </p><p><img src="/sites/all/files/ai2tutorials/helloPurr/HelloPurrBarcode.png" /></p> <p>Or <a href="http://explore.appinventor.mit.edu/sites/all/files/ai2tutorials/helloPurr/HelloPurr.apk">download the apk</a></p> <h4 class="ai-header">Download Source Code</h4> <p>If you'd like to work with this sample in App Inventor, download the <a href="/sites/all/files/ai2tutorials/helloPurr/HelloPurr.aia">source code</a> to your computer, then open App Inventor, go to the My Projects page, and choose <b>More Actions | Upload Source</b>.</p> <h4 class="ai-header">Next steps</h4> <p>Now that you know the basics of how App Inventor works we recommend you:</p> <ul><li>Complete additional <a href="/ai2/tutorials">Tutorials</a>.</li> <li>Review the <a href="/library">User Guide for App Inventor 2</a>.</li> <li>Join the <a href="/forums">User Discussion Forum</a>.</li> <li>Read the guide to <a href="/ai2/support/blocks">Understanding Blocks</a><a>.</a></li> <li>Or, if you've been using the emulator and want to start using your phone, you can <a href="/ai2/setup-device">set up your Android device</a> to build apps.</li> </ul><pre class="ai-box">Something not working right? Visit the <a href="/ai2/support/troubleshooting">troubleshooting page</a>, or check the <a href="/forums">App Inventor User Forum</a> for help.</pre></div></div></div><section class="field field-name-field-version field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Version:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-version/app-inventor-2" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">App Inventor 2</a> </li> </ul> </section> <section class="field field-name-field-tutorial-difficulty field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Difficulty:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-difficulty/basic" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Basic</a> </li> </ul> </section> Mon, 01 Jul 2013 20:02:18 +0000 joanie 348 at http://dev-explore.appinventor.mit.edu http://dev-explore.appinventor.mit.edu/ai2/hellopurr#comments PicCall http://dev-explore.appinventor.mit.edu/content/piccall <div class="field field-name-body field-type-text-with-summary field-label-hidden view-mode-rss"><div class="field-items"><div class="field-item even" property="content:encoded"><p><span style="color:green;">PicCall</span> shows you how you can use App Inventor to make apps that do actual things, like calling friends.</p> <p>This tutorial assumes that you have completed the <a href="/content/setup-mit-app-inventor">Set Up</a> process.</p> <h4 class="ai-header">Before starting</h4> <p>To run <span style="color:green;">PicCall</span>, your phone must be set up and activated for making phone calls. If it isn't you can still build <span style="color:green;">PicCall</span> for practice, but the phone won't actually make the calls.</p> <pre class="ai-warning">Warning: <span style="color:black;">PicCall</span> does not work on all Android phones in the current implementation of App Inventor: you'll get an error notice on some phones when you try to pick a phone number. Also, you won't see all your contacts — only those created from Gmail. These limitations will be removed in the future.</pre><p>In this tutorial, unlike <span style="color:green;">HelloPurr</span>, you'll give names to components, rather than just using the default names that App Inventor provides (like "Button1"). Using meaningful names is good programming practice: it helps you keep your programs straight in your own mind, and it helps others understand your programs.</p> <p>Your phone should also contain a few contacts with pictures. You can use the Contacts app to save pictures for your contacts. You can also click on Contacts in your Gmail account on your computer and add pictures there.</p> <p>Make sure your computer and your phone are set up to use App Inventor. Start a new project in the Designer window. Name it "PicCall" and change the screen <strong style="color:green;">Title</strong> to <span style="color:green;">PicCall</span>. Open the Blocks Editor, click <strong>Connect to Phone</strong>, and check that the phone has started the App Inventor app.</p> <h4 class="ai-header">Getting started</h4> <p>Start out just like in <span style="color:green;">HelloPurr</span> by placing a button on the screen. Make the button 150 pixels wide and 150 pixels high. Set button's <strong style="color:green;">Image</strong> to a picture. You may as well use the picture of the kitty if it's handy -- you'll be changing the picture soon. Set the <strong style="color:green;">Text</strong> of the button to "Press to Call", although you'll be changing that soon as well.</p> <p>Change the name of the Button component to "TopButton" (you'll make one called "BottomButton" later in the tutorial). To change a component's <strong style="color:green;">Name</strong>, click the <strong>Rename</strong> button in the Components panel and enter the new name.</p> <pre class="ai-box">In this tutorial, unlike <span style="color:black;">HelloPurr</span>, you'll give names to components, rather than just using the default names that App Inventor provides (like "Button1"). Using meaningful names is good programming practice: it helps you keep your programs straight in your own mind, and it helps others understand your programs.Don't confuse the <strong style="color:black;">Name</strong> of a component with the <strong style="color:black;">Text</strong> of a component. The <strong style="color:black;">Text</strong> is what appears on the screen. The <strong style="color:black;">Name</strong> is the name your program uses to refer to the component. You'll see the name in the Components structure list in the Designer and on the drawers in the Blocks editor.</pre><h4 class="ai-header">Making phone calls</h4> <p>In <span style="color:green;">HelloPurr</span>, you made the phone play a sound when the button was clicked. <span style="color:green;">PicCall</span> in almost the same, except that instead of playing a sound, the phone makes a call.</p> <p>App Inventor's <strong>PhoneCall</strong> component makes phone calls. You can find PhoneCall in the Social section of the Palette. Open that section and drag out a PhoneCall into the viewer. It will go into the Non-visible components area. Name it "TopCall". The PhoneCall's <strong style="color:green;">PhoneNumber</strong> property determines the number to call. Set that to some 10-digit phone number you'd like to call. Here's how the Designer should look:</p> <p><img src="/sites/all/files/tutorials/picCall/PicCallDesigner.png" /></p> <p>Now switch to the Blocks Editor and pull out the <span class="basicblock">when TopButton.Click do</span> block. In the <span class="basicblock">do</span> slot, place a <span class="callblock">call TopCall.MakePhoneCall</span> block from the TopCall drawer, so that the event handler looks like this:</p> <p><img src="/sites/all/files/tutorials/picCall/PicCallWhenClick.png" /></p> <p>Go ahead and test what you have so far on the phone: Press the button and make the call. You could package this up as an app right now. It would be a pretty limited app, always calling the same fixed number, some people might find that useful.</p> <h4 class="ai-header">Phone contact information</h4> <p>In addition to making phone calls, App Inventor apps can also get information from the phone's contact list. You do this with the <strong>PhoneNumberPicker</strong> component.</p> <p>Pull out a PhoneNumberPicker component from the Social section of the Palette, place it under <span style="color:green;">TopButton</span>. A PhoneNumberPicker is a kind of button: when you press it, it brings up your phone contacts list and lets you pick someone. Change the name of the PhoneNumberPicker to "TopPick", and change its <strong style="color:green;">Text</strong> to "Press to pick a number to call". Try it by pressing the picker on your phone: you should see your contacts come up, and you can pick one. Nothing will happen after you pick, because you haven't yet told the components to do anything. You'll do that next.</p> <h4 class="ai-header">Using the picker</h4> <p>Switch to the Blocks window and open the drawer for <span style="color:green;">TopPick</span>. Drag out the <span class="basicblock">when TopPick.AfterPicking do</span> block. This lets you define an event handler that says what to do after you've picked a number from your contacts.</p> <p>Now open the <span style="color:green;">TopCall</span> drawer and drag out <span class="setblock">set TopCall.PhoneNumber to</span> and fit it into the slot in the <span class="basicblock">when TopPick.AfterPicking do</span> block. Now drag out <span class="argblock">TopPick.PhoneNumber</span> from the <span style="color:green;">TopPick</span> drawer and plug it into the empty socket. Here's how your event handler should look:</p> <p><img src="/sites/all/files/tutorials/picCall/PicCallAfterPicking.png" /></p> <p>Try it on the phone: Press the picker, choose a contact and a phone number. Then press the phone call button to make the call. Add a command to the event handler to set <span style="color:green;">TopButton</span>'s <strong style="color:green;">Text</strong> property to <span class="argblock">TopPick.PhoneNumber:</span></p> <p><img src="/sites/all/files/tutorials/picCall/PicCallAfterPicking2.png" /></p> <h4 class="ai-header">Pictures</h4> <p>If you have a picture stored with your contacts, you can make the button show that along with the phone number, rather than always using the picture of the kitty. To do this, add an command to the event handler, to set the <strong style="color:green;">Image</strong> property of <span style="color:green;">TopButton</span> to be the the <strong style="color:green;">Picture</strong> property of <span style="color:green;">TopPick</span>:</p> <p><img src="/sites/all/files/tutorials/picCall/PicCallAfterPicking3.png" /></p> <pre class="ai-box">PhoneNumberPicker has two properties that are easy to confuse: <strong style="color:black;">Picture</strong> and <strong style="color:black;">Image</strong>. <strong style="color:black;">Picture</strong> is the picture associated with the contact that's picked. <strong style="color:black;">Image</strong> is the image of the PhoneNumberPicker component as it appears in the Designer and on the phone.</pre><h4 class="ai-header">Enhancements</h4> <p>Here are some variations for you to try:</p> <ul><li>Add a second button, <span style="color:green;">BottomButton</span>, and a second PhoneNumberPicker, so that your app gives you the choice of two numbers.</li> <li>Add a label with instructions on how to use the app.</li> <li>Show the name of the person being called in addition to the phone number. Use an extra label to show the additional information.</li> </ul><h4 class="ai-header">Using PicCall</h4> <p>You can package <span style="color:green;">PicCall</span> and download it to the phone so you can use it when you're not connected to the computer. But there's a big limitation: Every time you restart <span style="color:green;">PicCall</span>, it starts fresh and does not remember what you picked last time. Later, we'll see how to use the <strong>TinyDB</strong> component to create apps that can remember information from one time to the next. Such information is called <em>persistent data</em>.</p> <h4 class="ai-header">Review</h4> <p>Here are the key ideas covered in this tutorial:</p> <ul><li>You can name components by means of the <strong>Rename</strong> button.</li> <li>App Inventor has components that can use information stored on the phone. The <strong>PhoneNumberPicker</strong> can get phone numbers and pictures for your contacts, and <strong>PhoneCall</strong> can make calls.</li> </ul><pre class="ai-box" style="background-image: url('/sites/all/files/tutorials/picCall/android-warning-30x30.gif"><strong>Tip</strong>: The list that comes up when you run the phone number picker does not show the pictures associated with your contacts, on some Android systems. Even though the image isn't shown, the "Picture" property will still return a picture that will show up when you run the app, provided that the phone has a picture for that contact.</pre><p>Scan the following barcode onto your phone to install and run the sample app. </p><p><img src="/sites/all/files/tutorials/picCall/PicCallBarcode.png" /></p> <h4 class="ai-header">Download Source Code</h4> <p>If you'd like to work with this sample in App Inventor, download the <a href="/sites/all/files/tutorials/picCall/PicCall.zip">source code</a> to your computer, then open App Inventor, go to the My Projects page, and choose <b>More Actions | Upload Source</b>.</p> <pre class="ai-box">Done with <span style="color:black;">PicCall</span>? Return to the other tutorials <a href="/tutorials">here</a>.</pre></div></div></div><section class="field field-name-field-version field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Version:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-version/app-inventor-1" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">App Inventor 1</a> </li> </ul> </section> <section class="field field-name-field-tutorial-difficulty field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Difficulty:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-difficulty/basic" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Basic</a> </li> </ul> </section> Fri, 14 Jun 2013 15:45:03 +0000 aaron 307 at http://dev-explore.appinventor.mit.edu http://dev-explore.appinventor.mit.edu/content/piccall#comments MoleMash http://dev-explore.appinventor.mit.edu/content/molemash <div class="field field-name-body field-type-text-with-summary field-label-hidden view-mode-rss"><div class="field-items"><div class="field-item even" property="content:encoded"><p><img style="float:right;" src="/sites/all/files/tutorials/moleMash/MoleOnEmulator.png" /></p> <p>In the game <span style="color:green;">MoleMash</span>, a mole pops up at random positions on a playing field, and the player scores points by hitting the mole before it jumps away. This tutorial shows how to build <span style="color:green;">MoleMash</span> as an example of a simple game that uses animation.</p> <p>The tutorial assumes that you have completed the <a href="http://explore.appinventor.mit.edu/hellopurr">HelloPurr</a> and <a href="/content/paintpot-part-1">PaintPot</a> tutorials.</p> <p><a href="http://cs.usfca.edu/~wolber/appinventor/bookSplits/ch3MoleMash.pdf">Download Book Chapter (PDF)</a></p> <h4 class="ai-header">Getting Started</h4> <p>Connect to the App Inventor web site and start a new project. Name it "MoleMash", and also set the screen's <strong style="color:green;">Title</strong> to "MoleMash". Open the Blocks Editor and connect to the phone.</p> <p>Also download this <a href="/sites/all/files/tutorials/moleMash/mole.png">picture of a mole</a> and save it on your computer.</p> <h4 class="ai-header">Introduction</h4> <p>You'll design the game so that the mole moves once every half-second. If it is touched, the score increases by one, and the phone vibrates. Pressing restart resets the score to zero.</p> <p>This tutorial introduces:</p> <ul><li>image sprites</li> <li>timers and the <strong>Clock</strong> component</li> <li>procedures</li> <li>picking random numbers between 0 and 1</li> <li>text blocks</li> <li>typeblocking</li> </ul><h4 class="ai-header">The first components</h4> <p>Several components should be familiar from previous tutorials:</p> <ul><li>A <strong>Canvas</strong> named "MyCanvas". This is the area where the mole moves.</li> <li>A <strong>Label</strong> named "ScoreLabel" that shows the score, i.e., the number of times the player has hit the mole.</li> <li>A <strong>Button</strong> named "ResetButton".</li> </ul><p>Drag these components from the Palette onto the Viewer and assign their names. Put <span style="color:green;">MyCanvas</span> on top and set its dimensions to 300 pixels wide by 300 pixels high. Set the <strong style="color:green;">Text</strong> of ScoreLabel to "Score: ---". Set the <strong style="color:green;">Text</strong> of ResetButton to "Reset". Also add a <strong>Sound</strong> component and name it "Noise". You'll use <span style="color:green;">Noise</span> to make the phone vibrate when the mole is hit, similar to the way you made the kitty purr in <span style="color:green;">HelloPurr</span>.</p> <h4 class="ai-header">Timers and the Clock component</h4> <p>You need to arrange for the mole to jump periodically, and you'll do this with the aid of a <strong>Clock</strong> component. The Clock component provides various operations dealing with time, like telling you what the date is. Here, you'll use the component as a timer that fires at regular internals. The firing interval is determined by the Clock 's <strong style="color:green;">TimerInterval</strong> property. Drag out a Clock component; it will go into the non-visible components area. Name it "MoleTimer". Set its TimeInterval to 500 milliseconds to make the mole move every half second. Make sure that <strong style="color:green;">Enabled</strong> is checked.</p> <h4 class="ai-header">Adding an Image Sprite</h4> <p>To add the moving mole we'll use a <em>sprite</em>.</p> <p>Sprites are images that can move on the screen within a Canvas. Each sprite has a <strong style="color:green;">Speed</strong> and a <strong style="color:green;">Heading</strong>, and also an <strong style="color:green;">Interval</strong> that determines how often the sprite moves at its designated speed. Sprites can also detect when they are touched. In <span style="color:green;">MoleMash</span>, the mole has a speed zero, so it won't move by itself. Instead, you'll be setting the mole's position each time the timer fires. Drag an <strong>ImageSprite</strong> component onto the Viewer. You'll find this component in the Animation category of the Palette. Place it within <span style="color:green;">MyCanvas</span> area. Set these properties for the Mole sprite:</p> <ul><li><strong style="color:green;">Picture</strong>: Use mole.png, which you downloaded to your computer at the beginning of this tutorial.</li> <li><strong style="color:green;">Enabled</strong>: checked</li> <li><strong style="color:green;">Interval</strong>: 500 (The interval doesn't matter here, because the mole's speed is zero: it's not moving by itself.)</li> <li><strong style="color:green;">Heading</strong>: 0 The heading doesn't matter here either, because the speed is 0.</li> <li><strong style="color:green;">Speed</strong>: 0.0</li> <li><strong style="color:green;">Visible</strong>: checked</li> <li><strong style="color:green;">Width</strong>: Automatic</li> <li><strong style="color:green;">Height</strong>: Automatic</li> </ul><p>You should see the <strong style="color:green;">x</strong> and <strong style="color:green;">y</strong> properties already filled in. They were determined by where you placed the mole when you dragged it onto <span style="color:green;">MyCanvas</span>. Go ahead and drag the mole some more. You should see <strong style="color:green;">x</strong> and <strong style="color:green;">y</strong> change. You should also see the mole on your connected phone, and the mole moving around on the phone as you drag it around in the Designer. You've now specified all the components. The Designer should look like this. Notice how <span style="color:green;">Mole</span> is indented under <span style="color:green;">MyCanvas</span> in the component structure list, indicating that the sprite is a sub-component of the canvas.</p> <p><img src="/sites/all/files/tutorials/moleMash/MoleMashDesigner.png" /></p> <h4 class="ai-header">Component Behavior and Event Handlers</h4> <p>Now you'll specify the component behavior. This introduces some new App Inventor ideas. The first is the idea of a <em>procedure</em>.</p> <p>A procedure is a sequence of statements that you can refer to all at once as single command. If you have a sequence that you need to use more than once in a program, you can define that as a procedure, and then you don't have to repeat the sequence each time you use it. Procedures in App Inventor can take arguments and return values. This tutorial covers only the simplest case: procedures that take no arguments and return no values.</p> <h4 class="ai-header">Define Procedures</h4> <p>Define two procedures:</p> <ul><li><span class="callblock">MoveMole</span> moves the <span style="color:green;">Mole</span> sprite to a new random position on the canvas.</li> <li><span class="callblock">UpdateScore</span> shows the score, by changing the text of the <strong style="color:green;">ScoreLabel</strong></li> </ul><p>Start with MoveMole:</p> <ul><li>In the Blocks Editor, under Built-In, open the Definition drawer. Drag out a <span class="callblock">to procedure</span> block and change the label "procedure" to "MoveMole".<br /><pre class="box">Note: There are two similar blocks: <span class="callblock">procedure</span> and <span class="callblock">procedureWithResult</span>. Here you should use <span class="callblock">procedure</span>.</pre><p>The <span class="callblock">to MoveMole</span> block has a slot labeled "do". That's where you put the statements for the procedure. In this case there will be two statements: one to set the mole's x position and one to set its y position. In each case, you'll set the position to be a random fraction, between 0 and 1, of the difference between the size of the canvas and the size of the mole. You create that value using blocks for <span class="basicblock">random fraction</span> and multiplication and subtraction. You can find these in the Math drawer.</p></li> <li>Build the <span style="color:green;">MoveMole</span> procedure. The completed definition should look like this:<br /><img src="/sites/all/files/tutorials/moleMash/MoveMole.png" /><br /> Leave the arg socket for <span style="color:green;">MoveMole</span> empty because <span style="color:green;">MoveMole</span> does not take any arguments. Observe how the blocks connect together: the first statement uses the <span class="setblock">Mole.X set</span> block to set mole's horizontal position. The value plugged into the block's socket is the result of multiplying: <ol><li>The result of the <span class="basicblock">call random fraction</span> block, which a value between 0 and 1</li> <li>The result of subtracting the mole's width from the canvas width</li> </ol><p>The vertical position is handled similarly.</p></li> </ul><p>With <span style="color:green;">MoveMole</span> done, the next step is to define a variable called <span style="color:green;">score</span> to hold the score (number of hits) and give it initial value 0. Also define a procedure <span class="callblock">UpdateScore</span> that shows the score in <span style="color:green;">ScoreLabel</span>. The actual contents to be shown in <span style="color:green;">ScoreLabel</span> will be the text "Score: " joined to the value of <span style="color:green;">score</span>.</p> <ul><li>To create the "Score: " part of the label, drag out a text block from the Text drawer. Change the block to read "Score: " rather than "text".</li> <li>Use a join block to attach this to a block that gives the value of the score variable. You can find the join block in the Text drawer.</li> </ul><p>Here's how <span style="color:green;">score</span> and <span class="callblock">UpdateScore</span> should look:</p> <p><img src="/sites/all/files/tutorials/moleMash/UpdateScore.png" /></p> <h4 class="ai-header">Add a Timer</h4> <p>The next step is to make the mole keep moving. Here's where you'll use <span style="color:green;">MoleTimer</span>. Clock components have an event handler called <span class="basicblock">when ... Timer</span> that triggers repeatedly at a rate determined by the <strong style="color:green;">TimerInterval</strong>.</p> <p>Set up <span style="color:green;">MoleTimer</span> to call <span class="callblock">MoveMole</span> each time the timer fires, by building the event handler like this:</p> <p><img src="/sites/all/files/tutorials/moleMash/MoleMashTimerEventHandler.png" /></p> <pre class="ai-box">Notice how the mole starts jumping around on the phone as soon as you define the event handler. This is an example of how things in App Inventor start happening instantaneously, as soon as you define them.</pre><h4 class="ai-header">Add a Mole Touch Handler</h4> <p>The program should increment the score each time the mole is touched. Sprites, like canvases, respond to touch events. So create a touch event handler for <span style="color:green;">Mole</span> that:</p> <ol><li>Increments the score.</li> <li>Calls <span class="callblock">UpdateScore</span> to show the new score.</li> <li>Makes the phone vibrate for 1/10 second (100 milliseconds).</li> <li>Calls <span class="callblock">MoveMole</span> so that the mole moves right away, rather than waiting for the timer.</li> </ol><p>Here's what this looks like in blocks. Go ahead and assemble the <span class="basicblock">when Mole.Touched</span> blocks as shown.</p> <p><img src="/sites/all/files/tutorials/moleMash/MoleMashTouchEventHandler.png" /></p> <p>Here's a tip: You can use typeblocking: typing to quickly create blocks.</p> <ul><li>To create a value block containing 100, just type 100 and press return.</li> <li>To create a <span class="callblock">MoveMole</span> block, just type <span class="callblock">MoveMole</span> and select the block you want from the list</li> </ul><h4 class="ai-header">Reset the Score</h4> <p>One final detail is resetting the score. That's simply a matter of making the <span style="color:green;">ResetButton</span> change the score to 0 and calling <span class="callblock">UpdateScore</span>.</p> <h4 class="ai-header">Complete Program</h4> <p>Here's the complete <span style="color:green;">MoleMash</span> program:</p> <p><img src="/sites/all/files/tutorials/moleMash/MoleMashComplete.png" /></p> <h4 class="ai-header">Variations</h4> <p>Once you get the game working, you might want to explore some variations. For example:</p> <ul><li>Make the game vary the speed of the mole in response to how well the player is doing. To vary how quickly the mole moves, you'll need to change the <span style="color:green;">MoleTimer</span>'s <strong style="color:green;">Interval</strong> property.</li> <li>Keep track of when the player hits the mole and when the player misses the mole, and show a score with both hits and misses. To do this, you'll need do define touched handlers both for <span style="color:green;">Mole</span>, same as now, and for <span style="color:green;">MyCanvas</span>. One subtle issue, if the player touches the mole, does that also count as a touch for <span style="color:green;">MyCanvas</span>? The answer is yes. Both touch events will register.</li> </ul><h4 class="ai-header">Review</h4> <p>Here are some of the ideas covered in this project:</p> <ul><li>Sprites are touch-sensitive shapes that you can program to move around on a <strong>Canvas</strong>.</li> <li>The <strong>Clock</strong> component can be used as a timer to make events that happen at regular intervals.</li> <li>Procedures are defined using <span class="callblock">to</span> blocks.</li> <li>For each procedure you define, App Inventor automatically creates an associated call block and places it in the My Definitions drawer.</li> <li>Making a <span class="basicblock">random-fraction</span> block produces a number between 0 and 1.</li> <li>Text blocks specify literal text, similar to the way that number blocks specify literal numbers.</li> <li>Typeblocking is a way to create blocks quickly, by typing a block's name.</li> </ul><h4 class="ai-header">Scan the Sample App to your Phone</h4> <p>Scan the following barcode onto your phone to install and run the sample app. </p><p><img src="/sites/all/files/tutorials/moleMash/MoleMashBarcode.png" /></p> <h4 class="ai-header">Download Source Code</h4> <p>If you'd like to work with this sample in App Inventor, download the <a href="/sites/all/files/tutorials/moleMash/MoleMash.zip">source code</a> to your computer, then open App Inventor, go to the My Projects page, and choose <b>More Actions | Upload Source</b>.</p> <pre class="ai-box">Done with <span style="color:black;">MoleMash</span>? Return to the other tutorials <a href="/tutorials">here</a>.</pre></div></div></div><section class="field field-name-field-version field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Version:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-version/app-inventor-1" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">App Inventor 1</a> </li> </ul> </section> <section class="field field-name-field-tutorial-difficulty field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Difficulty:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-difficulty/basic" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Basic</a> </li> </ul> </section> <section class="field field-name-field-tutorial-type field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Type:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-type/clock-timer" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Clock Timer</a> </li> <li class="field-item odd"> <a href="/tutorial-type/game" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Game</a> </li> <li class="field-item even"> <a href="/tutorial-type/sprites" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Sprites</a> </li> </ul> </section> Fri, 14 Jun 2013 15:43:59 +0000 aaron 306 at http://dev-explore.appinventor.mit.edu http://dev-explore.appinventor.mit.edu/content/molemash#comments PaintPot (Part 1) http://dev-explore.appinventor.mit.edu/content/paintpot-part-1 <div class="field field-name-body field-type-text-with-summary field-label-hidden view-mode-rss"><div class="field-items"><div class="field-item even" property="content:encoded"><p>This tutorial has two parts: <a href="/content/paintpot-part-1">Part 1</a> | <a href="/content/paintpot-part-2">Part 2</a> </p> <p>This tutorial introduces the <strong>Canvas</strong> component for creating simple two-dimensional graphics. You'll build an app that lets you draw on the phone screen in different colors.</p> <pre class="ai-box"><strong>Historical note</strong>: PaintPot was one of the first programs developed to demonstrate the potential of personal computers, as far back as the 1970s.</pre><p><a href="http://cs.usfca.edu/~wolber/appinventor/bookSplits/ch2PaintPot.pdf">Download Book Chapter PDF</a></p> <h4>What you're building</h4> <p><img style="float:right;margin:5px;" src="/sites/all/files/tutorials/paintPot1/PaintPotPart1PhoneImage.png" /></p> <p>With the <span style="font-style:italic;">PaintPot</span> app, you can:</p> <ul><li>Dip your finger into a virtual paint pot to draw in that color.</li> <li>Drag your finger along the screen to draw a line.</li> <li>Tap the screen to make dots.</li> <li>Use the button at the bottom to wipe the screen clean.</li> <li>Include an image as a drawing background.</li> </ul><p><span style="font-style:italic;"> <p>This tutorial assumes that you have completed the <span style="font-style:italic;">HelloPurr</span> tutorial. It introduces the following App Inventor concepts:</p> <ul><li>The <strong>Canvas</strong> component for drawing.</li> <li>Controlling screen layout with <strong>Arrangement</strong> components.</li> <li>Event handlers that take arguments.</li> <li>Variables.</li> </ul><h4 class="ai-header">Before starting</h4> <p>Make sure your computer and your phone are set up to use App Inventor. Start a new project in the Designer window, and name it <span style="font-style:italic;">"PaintPot"</span>. Open the Blocks Editor, click <strong>Connect to Phone</strong>, and make sure the phone has started the App Inventor app.</p> <h4 class="ai-header">Screen title</h4> <p>To get started, go to the Properties panel on the right of the Designer and change the screen <strong>Title</strong> to "PaintPot". You should see this change on phone, with the new title showing in the title bar.</p> <p>There are three names in App Inventor, and it's easy to confuse them:</p> <ol><li>The name you choose for your project as you work on it (in this case, <span style="font-style:italic;">PaintPot</span>). This will also be the name of the application if you package it for the phone.</li> <li>The name "Screen1", which is the name of the <strong>Screen</strong> component. You'll see it listed in the Components panel in the Designer. You can't change the name of the Screen component in the current version of App Inventor.</li> <li>The <strong>Title</strong> property of the screen, which is what you'll see in the phone's title bar. Title is a property of the <strong>Screen</strong> component. The Title starts out being "Screen1", which is what you used in <span style="font-style:italic;">HelloPurr</span>. However, you can change it, as you're doing for <span style="font-style:italic;">PaintPot</span>. To reiterate, the name and the title of Screen1 are initially the same, but you can change the title if you want.</li> </ol><h4 class="ai-header">Set up the Components</h4> <p>You'll use these components to make <span style="font-style:italic;">PaintPot</span>:</p> <ul><li>Three <strong>Buttons</strong> for selecting red, blue, or green paint, and another button for wiping the drawing.</li> <li>A <strong>Canvas</strong>, the drawing surface. This canvas has a <strong>BackgroundImage</strong>, which is <a href="/sites/all/files/tutorials/helloPurr/kitty.png">this kitty</a> from the <span style="font-style:italic;">HelloPurr</span> tutorial. You can also draw on a blank canvas. That's just a canvas without a background image.</li> <li>There's also a component you don't see: you use a <strong>HorizontalArrangement</strong> to make the three color buttons line up.</li> </ul><p>That makes five components in all. Let's get them and build the app.</p> <h4 class="ai-header">Color Buttons</h4> <ul><li>Drag a <strong>Button</strong> component onto the Viewer and change the button's <strong>Text</strong> attribute to "Red" and make its <strong>BackgroundColor</strong> red.</li> <li>Click on <span style="font-style:italic;">Button1</span> in the components list in the Viewer to highlight it (it might already be highlighted) and use the <strong>Rename...</strong> button to change its name from "Button1" to "ButtonRed".</li> <li>Similarly, make two more buttons for blue and green, named "ButtonBlue" and "ButtonGreen", placing them vertically under the red button.</li> </ul><p>Here's how this should look in the designer, with the button names appearing in the list of project components. In this project, you're changing the names of the components rather than leaving them as the default names as you did with <span style="font-style:italic;">HelloPurr</span>. Using meaningful names makes your projects more readable to yourself and others.</p> <p><img src="/sites/all/files/tutorials/paintPot1/PaintPotThreeButtonsVertically.png" /></p> <p>You should also see the three buttons on the phone screen.</p> <h4 class="ai-header">Layout with Screen Arrangement</h4> <p>You should now have three buttons, one above the other. The next step is to make them line up horizontally. You do this using a <strong>HorizontalArrangement</strong> component.</p> <ol><li>From the Palette's Screen Arrangement category, drag out a <strong>HorizontalArrangement</strong> component and place it under the buttons. Change the name of this component from "HorizontalArrangement1" to "ThreeButtons".</li> <li>In the Properties panel, change the <strong>Width</strong> of <span style="font-style:italic;">ThreeButtons</span> to "Fill Parent..." so that it fills the entire width of the screen.</li> <li>Move the three buttons side by side into the <strong>HorizontalArrangement</strong> component. <strong>Hint</strong>: You'll see a blue vertical line that shows where the piece you're dragging will go.</li> </ol><p>If you look in the list of project components, you'll see the three buttons indented under the <span style="font-style:italic;">ThreeButtons</span> to show that they are now its subcomponents. Notice that all the components are indented under <span style="font-style:italic;">Screen1</span>.</p> <p><img src="/sites/all/files/tutorials/paintPot1/PaintPotThreeButtonsHorizontally.png" /></p> <p>You should also see your three buttons line up in a row on the phone screen, although things might not look exactly as on the Designer. For example, the Arrangement's outline shows in the Designer but not on the phone.</p> <p>In general, you use Screen Arrangement to create simple vertical or horizontal layouts. You can create more complex layouts by nesting Screen Arrangement components. There is also a <strong>TableArrangement</strong> component (not covered in this tutorial).</p> <h4 class="ai-header">Canvas and wipe button</h4> <p>The final two components are the canvas and the wipe button.</p> <ol><li>From the Palette's Basic category drag a <strong>Canvas</strong> component onto the Viewer. Change its name to "DrawingCanvas". Set its <strong>Width</strong> to "Fill Parent" and set its <strong>Height</strong> to 300 pixels.</li> <li>Add a Background Image to the <strong>Canvas</strong>. Click on the field containing "None..." next to <strong>BackgroundImage</strong> in the canvas's Properties panel. You can use the same kitty.png file, if you still have it on your desktop from an earlier tutorial. Or you can use another image.<br /><pre class="ai-box">You can use any image you like, but you'll get the best results if the size of the image (in pixels) is close to the size at which you'll be showing it on the phone. Also, large images will take a long time to load, and might exceed the memory capacity of the phone allocates for applications.</pre></li> <li>From the Palette, drag the final button onto the screen, placing it under the canvas. Change its <strong>id</strong> to "ButtonWipe" and change its <strong>Text</strong> attribute to "Wipe".</li> </ol><p>You've now completed the steps to set the appearance of your app. Here's how this should look in the Designer. Next, you'll define how the components behave.</p> <p><img src="/sites/all/files/tutorials/paintPot1/PaintPotDesigner.png" /></p> <h4 class="ai-header">Add behaviors to the components</h4> <p>The Blocks Editor should already be open. First set up the buttons that change the paint color. Later you will add blocks to decide what happens when someone touches or drags the screen.</p> <h4 class="ai-header">Add button event handlers</h4> <p>In the Blocks Editor:</p> <ol><li>Switch to the My Blocks column.</li> <li>Open the drawer for <span style="font-style:italic;">ButtonRed</span> and drag out the <span class="basicblock">when ButtonRed.Click </span> block.</li> <li>Open the <span style="font-style:italic;">DrawingCanvas</span> drawer. Drag out the <span class="setblock">set DrawingCanvas.PaintColor to </span> block (you may have to scroll the list of blocks in the drawer to find it) and place it in the <span class="basicblock">do</span> section of <span class="basicblock">when ButtonRed.Click </span>.</li> <li>Switch to the Built-In Column. Open the Colors drawer and drag out the block for the color Red and put it into <span class="setblock">set DrawingCanvas.PaintColor to </span>.</li> <li>Repeat steps 2-4 for the blue and green buttons.</li> <li>The final button to set up is the Wipe button. Switch back to the My Blocks column. Make a click event handler for <span style="font-style:italic;">ButtonWipe</span> by dragging <span class="basicblock">when ButtonWipe.Click </span> from the <span style="font-style:italic;">ButtonWipe</span> drawer. From the <span style="font-style:italic;">DrawingCanvas</span> drawer, drag <span class="callblock">call DrawingCanvas.Clear </span> and place it in the <span class="basicblock">do</span> area of the <span class="basicblock">when ButtonWipe.Click</span> block.</li> </ol><p>The blocks for the buttons should look like this:</p> <p><img src="/sites/all/files/tutorials/paintPot1/buttonsclick.PNG" /></p> <h4 class="ai-header">Add Touch-event Handlers</h4> <p>Now for the next step: drawing on the <strong>Canvas</strong>. You'll arrange things so that when you touch the canvas, you get a dot at the spot where you touch. If you drag your finger slowly along the canvas, it draws a line.</p> <ul><li>In the Blocks Editor, open the drawer for the canvas and drag the <span class="basicblock">when DrawingCanvas.Touched</span> block to the workspace. As soon as you drag the block out, the three plugs on the right automatically fill in with name blocks called <strong>x</strong>, <strong>y</strong>, and <strong>touchedSprite</strong>.</li> </ul><p>You've already seen button click events. Clicks are simple, because there's nothing to know about the click other than that it happened. Other event handlers such as <span class="basicblock">when ... Touched</span> need information about the event. In App Inventor, this information is expressed as the value of arguments associated with the event handler. For the <span class="basicblock">when ... Touched</span> event, the first two arguments stand for the x and y coordinates of where the touch happened. We'll save <strong>touchedSprite</strong> for a later tutorial.</p> <ul><li>For this touch event, make the canvas draw a small circle at the point with coordinates (x, y). Drag out a <span class="callblock">call DrawingCanvas.DrawCircle</span> command from the canvas drawer and place it in the <span class="basicblock">do</span> section of <span class="basicblock">when DrawingCanvas.Touched</span>.</li> </ul><p>On the right side of the <span class="callblock">call DrawingCanvas.DrawCircle</span> block are three sockets where you must specify values for the x and y coordinates where the circle should be drawn, and <strong>r</strong>, which is the radius of the circle. For <strong>x</strong> and <strong>y</strong>, you'll use values of the arguments that were supplied to the Touched handler:</p> <ol><li>Open the My Definitions drawer at the top of the column. Find the blocks for <span class="argblock">value x</span> and <span class="argblock">value y</span>. The blocks were automatically created when you dragged out the touch event handler block.</li> <li>Drag out the <span class="argblock">value x</span> and <span class="argblock">value y</span> blocks and plug them into the corresponding sockets in the <span class="basicblock">when DrawingCanvas.Touched</span> block. Make sure to drag the <span class="argblock">value</span> blocks, not the corresponding <span class="argblock">name</span> blocks; they look very similar.</li> <li>You'll also need to specify the radius of the circle to draw. Five (pixels) is a good value for this app. Click in a blank area of the screen to bring up the hover menu and select <strong>math</strong> (green). Select "123" from the dropdown list, to create a number block. Change the "123" to "5" and plug that in for the radius slot.</li> </ol><pre class="ai-box">You can also just type 5 followed by return, to create a number block with a value of 5. This is an example of <em>typeblocking</em>: if you start typing, the Blocks Editor shows a list of blocks whose names match what you are typing; if you type a number it creates a number block.</pre><p>Here's how the touch event handler should look:</p> <p><img src="/sites/all/files/tutorials/paintPot1/PaintPotTouchHandler.png" /></p> <p>Try out what you have so far on the phone. Touch a color button. Now touch the canvas, and your finger should leave a spot at each place you touch. Touching the Wipe button should clear your drawing.</p> <h4 class="ai-header">Add Drag Events</h4> <p>Finally, add the drag event handler. Here's the difference between a touch and a drag:</p> <ul><li>A touch is when you place your finger on the canvas and lift it without moving it.</li> <li>A drag is when you place your finger on the canvas and move your finger while keeping it in contact.</li> </ul><p>When you drag your finger across the screen, it appears to draw a giant, curved line where you moved your finger. What you're actually doing is drawing hundreds of tiny straight lines: each time you move your finger, even a little bit, you extend the line from your finger's immediate last position to its new position.</p> <p>A drag event comes with 6 arguments. These are three pairs of x and y coordinates that show:</p> <ul><li>The position of your finger back where the drag started.</li> <li>The current position of your finger.</li> <li>The immediately previous position of your finger.</li> </ul><p>There's also a sprite, which we'll ignore for this tutorial.</p> <p>Now make dragging draw a line between the previous position and the current position by creating a drag handler:</p> <ol><li>From the <span style="font-style:italic;">DrawingCanvas</span> drawer, drag the <span class="basicblock">when DrawingCanvas.Dragged</span> block to the workspace.</li> <li>Also from the <span style="font-style:italic;">DrawingCanvas</span> drawer, drag the <span class="callblock">call DrawingCanvas.DrawLine</span> block into the <span class="basicblock">do</span> slot of the <span class="basicblock">when DrawingCanvas.Dragged</span> block.</li> <li>Click on the My Definitions drawer. You should see the blocks for the arguments you need. Drag the corresponding value blocks to the appropriate slots in <span class="basicblock">when DrawingCanvas.Dragged</span>: <strong>x1</strong> and <strong>y1</strong> should be <strong>prevX</strong> and <strong>prevY</strong>; <strong>x2</strong> and <strong>y2</strong> should be <strong>currentX</strong> and <strong>currentY</strong>.</li> </ol><p>Here's the result:</p> <p><img src="/sites/all/files/tutorials/paintPot1/PaintPotDragHandler.png" /></p> <p>Test your work by trying it on the phone: drag your finger around on the screen to draw lines and curves. Touch the screen to make spots. Use the Wipe button to clear the screen.</p> <p>In <a href="/content/paintpot-part-2">PaintPot Part 2</a>, you'll see how to use global variables to create dots of different sizes.</p> <h4 class="ai-header">Review</h4> <p>Here are some of the ideas covered in this tutorial:</p> <ul><li>You can use Screen Arrangement components to specify screen layouts other than just placing components one under the other.</li> <li>The <strong>Canvas</strong> component lets you draw on it. It can also sense touches and drags.</li> <li>Some event handlers are called with information about the event, such as the coordinates of where the screen was touched. This information is represented by arguments. When you select an event handler that has arguments, App Inventor creates <span class="argblock">value</span> blocks for these and places them in the My Definitions drawer.</li> </ul><h4 class="ai-header">Scan the Sample App to your Phone</h4> <p>Scan the following barcode onto your phone to install and run the sample app. </p><p><img src="/sites/all/files/tutorials/paintPot1/PaintPotBarcode.png" /></p> <h4 class="ai-header">Download Source Code</h4> <p>If you'd like to work with this sample in App Inventor, download the <a href="/sites/all/files/tutorials/paintPot1/PaintPot.zip">source code</a> to your computer, then open App Inventor, go to the My Projects page, and choose <b>More Actions | Upload Source</b>.</p> </span></p></div></div></div><section class="field field-name-field-version field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Version:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-version/app-inventor-1" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">App Inventor 1</a> </li> </ul> </section> <section class="field field-name-field-tutorial-difficulty field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Difficulty:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-difficulty/basic" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Basic</a> </li> </ul> </section> <section class="field field-name-field-tutorial-type field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Type:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-type/drawing-canvas" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Drawing Canvas</a> </li> </ul> </section> Fri, 14 Jun 2013 15:43:28 +0000 aaron 305 at http://dev-explore.appinventor.mit.edu http://dev-explore.appinventor.mit.edu/content/paintpot-part-1#comments Magic 8-Ball http://dev-explore.appinventor.mit.edu/teach/magic-8-ball <div class="field field-name-body field-type-text-with-summary field-label-hidden view-mode-rss"><div class="field-items"><div class="field-item even" property="content:encoded"><h3>Lesson One: Magic 8-Ball Predicts the Future</h3> <p><img src="/sites/all/files/tutorials/magic8ball/8ball.jpg" style="float:left;width:75px;" /></p> <p>This introductory module will guide you through building a “Magic 8-Ball” app with App Inventor. When activated, your 8-ball will deliver one of its classic predictions, such as “It is decidedly so” or “Reply hazy, try again.”</p> <h3>Learning Goals</h3> <p>After completing this app, you will be able to:</p> <ul><li>Navigate the App Inventor environment: designer, blocks editor, emulator and/or physical phone</li> <li>Correctly use the following App Inventor components: accelerometer sensor, image, list-picker</li> </ul><h3>Materials</h3> <ul><li>A selection of images and sounds are available at the <a href="/media-library">App Inventor Media Library</a>.</li> <li>Optional supporting hard copy materials such as the <a href="/media-library">App Inventor Overview Handouts</a>, and the <a href="https://docs.google.com/viewer?a=v&amp;pid=sites&amp;srcid=ZGVmYXVsdGRvbWFpbnxhcHBpbnZlbnRvcmxlc3NvbnN8Z3g6MjJkNmViOGQ3MDNmZWYwZQ">PDF version</a> of this lesson.</li> <li>Basic App Inventor tutorial - getting around [<a href="http://www.youtube.com/watch?v=nC_x9iOby0g">video</a>]</li> </ul><h3>Outline</h3> <ol><li><a href="/content/setup">Set up</a> computers and phones or emulators. (Suggestion: do this ahead of time)</li> <li>Part One:<a href="#m8one">Click a Button, Hear a Sound</a> </li> <li>Part Two: <a href="#m8two">Click the Button, Get a Prediction + Hear a Sound</a> </li> <li>Part Three: <a href="#m8three">Shake the Phone, Get a Prediction + Hear a Sound</a> </li> <li><a href="#m8four">Suggestions for further exploration:</a> Text-to-Speech, Rotating image, Custom prediction lists</li> </ol><h3><a name="m8one" id="m8one">Part One: Click a Button, Hear a Sound</a></h3> <p>The final Magic 8-Ball App will deliver a prediction from a list that you have designed. To get started, first we'll make a button with a picture on it, and program it to play a sound when the button is clicked.</p> <p style="color:green;">DESIGN: App Inventor Designer</p> <ol><li>To open the App Inventor Designer window, go to <a href="http://appinventor.mit.edu">http://appinventor.mit.edu</a> and click "Invent". See setup instructions above if you are not sure how to sign in.</li> <li>If you have already made an app (such as Hello Purr), you will automatically be directed to the Designer with the last project you worked on showing. Click "My Projects" in the upper left corner of the screen, which will take you to your list of projects. Click "New" and name your project something like "Magic8Ball"(note: spaces are not allowed).<br /><img src="/sites/all/files/tutorials/magic8ball/my_projects.JPG" /></li> <li>Download one image and one sound file from below to be used in your app. Right click (control-click) on the link of the image or sound, then choose "Download" or "Save As". Save the media files to a convenient location that you will remember. <ul><li><a href="http://dl.dropbox.com/u/90819513/Cha_Ching_Sound.mp3">ChaChing Sound</a></li> <li><a href="http://dl.dropbox.com/u/90819513/Clinking_Teaspoon_Sound.mp3">Clinking Teaspoon Sound</a></li> <li><a href="http://dl.dropbox.com/u/90819513/Ta%20Da%20sound.mp3">Tada Sound</a></li> <li><a href="http://dl.dropbox.com/u/90819513/image_8_ball.jpeg">Magic 8 Ball Image</a></li> <li><a href="http://dl.dropbox.com/u/90819513/magic8ball3.jpeg">Blank 8 Ball Image</a></li> </ul></li> <li>On the left column of the Designer, open the Basic palette, and drag a Button component over to the Viewer(#1).</li> <li>Set the button image to an 8-Ball image:<br />Click on your newly added button to see its properties in the Properties pane on the right. Under "Image" click on the word "None..." and a small selection window will pop up (#2). Click the "Add" button and browse to where you saved the 8-Ball image. Select the file, then click “OK” to close the selection window. Click “OK” again on the properties pane to close the small popup window (#3).</li> <li>Go to the text field in the Properties pane and delete the display text of your button component (#4).<br /><img src="/sites/all/files/tutorials/magic8ball/viewer_button1.png" /><br /><img src="/sites/all/files/tutorials/magic8ball/viewer_button1_properties.png" /></li> <li>From the Media palette, drag over a Sound component onto the Viewer pane (#1). Notice that since the sound will not be a visible part of the app, it appears at the bottom of the Viewer pane, as a “Non-visible component”.</li> <li>Set the sound component's source file:<br />Click on your newly added sound component to see its properties in the Properties pane on the right. Under "Source" click in the small box on the word "None..." and a small selection window will pop up (#2). Click the "Add" button and browse to where you saved the sound file. Select the sound file, then click “OK” to close the selection window. Click “OK” again on the properties pane to close the small popup window (#3). </li> <p><img src="/sites/all/files/tutorials/magic8ball/viewer_sound1.png" /></p><li>You have now completed the work in the Designer for Part One of this app. It's time now to go over to the Blocks Editor to program the behavior of these components.</li> </ol><p style="color:green;">BUILD: Blocks Editor</p> <p>In the upper right corner of the Designer, click on the Blocks Editor button. Wait for a few moments while the blocks editor loads. This takes some time, and often requires you to click “accept”, “ok”, or “keep” as the java program downloads to your computer. (Be sure to look at the very top or very bottom of your browser to see if it is prompting you to accept.) If you are having trouble loading the Blocks Editor, go back to the <a href="/content/setup">Setup Instructions</a> for help.</p> <p>Now you are going to tell your app how to behave when the button is clicked. This is actually very simple in App Inventor, because the "code" for the program only consists of two blocks!</p> <p>Once the Blocks Editor is open, there are several options running along the left side of the screen. We refer to these as "Palettes" with “Drawers.” </p> <p>From the My Blocks palette, click on the Button1 drawer. Drag the <span class="basicblock">when Button1.Click</span> block into the work area (#1). From the My Blocks palette, click on the Sound1 drawer, drag the <span class="call block">Sound1.Play</span> block into the work area and insert it into the <span class="basicblock">when Button1.Click</span> block (#2). They will click together like magnetic puzzle pieces.</p> <p><img src="/sites/all/files/tutorials/magic8ball/blocks_button1.JPG" /></p> <p>Your blocks should now look like this:</p> <p><img src="/sites/all/files/tutorials/magic8ball/button1.click.png" /></p> <p>That's it! You've written the program for Part One of Magic 8-Ball. Now it's time to test that it's working right.</p> <p style="color:green;">TEST: Phone/Emulator</p> <p>You have now built an app! To test that it works, you either have to launch an emulator, or connect to a phone. Go back to the <a href="/content/setup">Setup Instructions</a> if you do not have a phone or an emulator running.</p> <p><b>Emulator</b>: click on the picture, you will hear the sound play.<br /><b>Phone</b>: tap the picture, you will hear the sound play.</p> <pre class="ai-box">Note: If you don't hear the sound, first be sure you have the volume turned up on your device (or computer if using emulator). Also, make sure your device has an SD card. App Inventor stores media files to the SD card. In some devices, the Play component does not work correctly. You will need to use the Player component instead of the Sound component.</pre><h3><a name="#m8two" id="#m8two">Part Two</a>: Output a Prediction</h3> <p>Now that we've gotten the button to perform an action (play a sound), we want to extend that action to include giving the user a prediction. First we'll need two labels: Label1 will display the instructions, and Label2 will display the chosen prediction. We'll use blocks to program a "list picker" to choose from a list of predictions. Each time the button is clicked, the app will change the text of Label2 to display the chosen prediction.</p> <p style="color:green;">DESIGN: App Inventor</p> <p>Go back to the Designer window in your browser and add some new things to your app.</p> <ol><li>From the Screen Arrangement palette, drag over the Vertical Arrangement component (#1). At first it will just look like an empty box, but when you put things in it, App Inventor will know that you want to line them up vertically (one on top of the other).</li> <p><img src="/sites/all/files/tutorials/magic8ball/viewer_vertical1.png" /></p> <li>From the Basic palette, drag over a Label component (#2) and drop it inside of the vertical arrangement component. In the Properties pane, change the "Text" property of Label1 to “Ask the Magic 8-Ball a question”.(#3)</li> <p><img src="/sites/all/files/tutorials/magic8ball/viewer_label1.png" /></p> <li>From the Basic palette, drag over another Label component (Label2) into the Vertical Arrangement box so that it sits right below Label1. Change the "Text" property of the Label2 to “Touch the Magic 8-Ball to receive your answer.” Now drag the 8-Ball image so that it is also inside the Vertical Arrangement component on top of the two labels. This will cause them to line up with each other in a vertical line. (Note: this can be tricky mouse work, but get them in there just right and the vertical arrangement will resize itself to fit everything.)</li> </ol><p>Now it’s time to go back into the Blocks Editor to program the components you just added to your project. (Remember, the Blocks Editor is running in a window outside of your web browser, signified by the java icon that looks like a coffee cup.)</p> <p style="color:green;">BUILD: Blocks Editor</p> <p>Now for the fun part! You're going to make a list of predictions and program the button to pick one item from the list and display it inside Label2. The button will also still play the sound that you programmed in Part One. Here's how to do it...</p> <ol><li>From the My Blocks palette, click on Label2 drawer to see all of its associated blocks. Drag over the blue <span class="ai-blocks">set Label2.Text</span> and insert it just above the <span class="ai-blocks">Sound1.Play</span> block. Notice that the <span class="ai-blocks">when Button1.Click</span> block automatically gets bigger to accomodate the new block.<br /><img src="/sites/all/files/tutorials/magic8ball/button1.click_label2.text.png" /></li> <li>From the Built-In palette, click on the Lists drawer. Drag over the <span class="ai-blocks">pick random item</span> block and connect it to the open socket of the <span class="ai-blocks">set Label2.Text</span> block.<br /><img src="/sites/all/files/tutorials/magic8ball/button1.click_label2.text_list.png" /></li> <li>From the Built-In palette, click on Lists again, then drag out the <span class="ai-blocks">make a list</span> block and plug it into the "list" socket on the right side of the <span class="ai-blocks">pick random item</span> block.</li> <li>From the Built-In palette, click on the Text drawer, drag out a <span class="ai-blocks">text</span> block and connect it to the item socket of the <span class="ai-blocks">make a list</span> block. Click directly on the word “text” so that it gets highlighted. You can then type in new text there. Think about the sayings you want in your list of predictions for the Magic 8-Ball. Type the first prediction into this new text block.</li> <li>Notice that when you plug in a new <span class="ai-blocks">text</span> block, the <span class="ai-blocks">make a list</span> block automatically creates a new socket. Repeat the previous step for each of the prediction choices you want programmed into your 8-Ball App. Plug each text block into the <span class="ai-blocks">pick random item</span> block. (Ideas for answers: <a href="http://en.wikipedia.org/wiki/Magic_8-Ball">http://en.wikipedia.org/wiki/Magic_8-Ball</a>)</li> </ol><p>Blocks should look something like this:</p> <p><img src="/sites/all/files/tutorials/magic8ball/button1.click_final.png" /><br /> (Note: it is normal for there to be a blank "item" space at the end of the make list block.)</p> <p>You've got a Magic 8-Ball App! Now your app is fully functional and will predict the future with absolute certainty. Test out that this works, and then come back for some challenge tasks to make the app even more fun.</p> <p style="color:green;">TEST: Emulator or Phone</p> <p><b>Emulator</b>: Click on the picture of the 8-Ball, you should see one of your answers displayed in the Label2.text field, followed by the sound.<br /><b>Phone</b>: Tap on the picture of the 8-Ball, you should see one of your answers displayed in the Label2.text field, followed by the sound.</p> <h3><a name="#m8three" id="#m8three">Part Three</a>: Shake the Phone, Get a Prediction</h3> <p>Even though you have a working Magic 8-Ball app, there is a way to make it even more fun. You can use the accelerometer component to make the phone respond to shaking instead of responding to a button click. This will make the app much more like a real Magic 8-Ball toy. <em>Note: This part can only be done with an actual phone or tablet equipped with an accelerometer. If you are using an emulator, skip this part and go to Challenge 1 instead.</em></p> <p style="color;green;">DESIGN: App Inventor</p> <p>From the Sensors palette, drag over an <b>AccelerometerSensor</b> sensor component. Notice that it automatically drops down to the “Non-visible components” area of the Viewer window. This is the only new component you need, so go on over to the Blocks Editor to change your program.</p> <p style="color:green;">BUILD: Blocks Editor</p> <ol><li>From the My Blocks drawer, click on AccelerometerSensor, then drag out the block for <span class="ai-blocks">when AccelerometerSensor.Shaking</span>.</li> <li>Disconnect all of the blocks from inside the <span class="ai-blocks">Button1.Click</span> block and move them inside the <span class="ai-blocks">AccelerometerSensor.Shaking</span> block. <b>NOTE:</b> you can move whole sections of connected blocks by clicking on the uppermost or leftmost block and dragging it. The connected blocks will come with it.</li> <li>Delete the <span class="ai-blocks">Button1.Click</span> block to keep your work area tidy.</li> </ol><p>The blocks should look something like this:</p> <p><img src="/sites/all/files/tutorials/magic8ball/accelSensor1.shaking.png" /></p> <p style="color:green;">TEST: Phone/Emulator</p> <p><b>Phone</b>: When you shake the phone it should show an answer and play a sound.<br /><b>Emulator</b>: unfortunately, you can not simulate shaking the phone when using the emulator.</p> <p style="color:green;">Package the App to Your Phone!</p> <p>Your app would disappear if you were to disconnect your phone from the Blocks Editor. This is because the app is still stored on the App Inventor server and not on your phone. Follow <a href="/support/packaging-apps">these instructions</a> to package your app to your phone or to make an ".apk" file that can be installed on any android phone. Or, if you want to make your app even cooler, try the challenges below.</p> <h3>Challenge 1: Make the Magic 8-Ball Speak</h3> <p>Instead of (or in addition to) making the prediction appear as text, can you make the 8-Ball speak it aloud? Hint: the text-to-speech component is under the <b>Other Stuff</b> palette in the Designer. <em>Note: Most Android devices have the text-to-speech (TTS) capability, but if you have trouble getting the TTS component in App Inventor to work, you may need to find out how to install TTS and/or enable TTS on your device.</em></p> <h3><a name="#m8four" id="#m8four">Suggestions for Further Exploration</a></h3> <p> </p><ul><li>Make the image rotate when the phone is shaken or have several images that the app rotates through while the phone is shaken. You could use this technique to make it look like the triangle piece inside the 8-ball window is surfacing. You could also make different images for different predictions and display the correct image for each prediction.</li> <li>Make a similar app but for a different purpose. The phone could be used in place of dice or yahtzee letters. It could simulate a coin toss or a random number or color generator for investigating probability.</li> <li>Ask end users to add choices to the list of predictions (See Make Quiz tutorial).</li> <li>"Crowd source" for prediction choices: allow people to send text messages and have the app add them to the list.</li> <li>Make the 8 Ball app a "server" so that anyone who sends a text to it will receive a text message prediction in return.</li> <li>Complete change the list to humorous choices (e.g. an app for teacher to use when a student has an excuse for not doing homework), or for useful purposes like randomly selecting a name from amongst people in the class.</li> </ul><pre class="ai-box">Done with Magic 8-Ball? Return to <a href="/teach">Curriculum (Teacher Resources)</a> or <a href="/tutorials">Tutorials</a>.</pre><h4 class="ai-header">Scan the Sample App to your Phone</h4> <p>Scan the following barcode onto your phone to install and run the sample app. </p><p><img src="/sites/all/files/tutorials/magic8ball/Magic8BallBarcode.png" /></p> <p>Or <a href="http://explore.appinventor.mit.edu/sites/all/files/tutorials/magic8ball/Magic8Ball.apk">download the apk</a></p> <h4 class="ai-header">Download Source Code</h4> <p>If you'd like to work with this sample in App Inventor, download the <a href="http://explore.appinventor.mit.edu/sites/all/files/tutorials/magic8ball/Magic8Ball.zip">source code</a> to your computer, then open App Inventor, go to the My Projects page, and choose <b>More Actions | Upload Source</b>.</p> </div></div></div><section class="field field-name-field-version field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Version:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-version/app-inventor-1" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">App Inventor 1</a> </li> </ul> </section> <section class="field field-name-field-tutorial-difficulty field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Difficulty:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-difficulty/basic" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Basic</a> </li> </ul> </section> <section class="field field-name-field-tutorial-type field-type-taxonomy-term-reference field-label-above view-mode-rss clearfix"> <h2 class="field-label">Tutorial Type:&nbsp;</h2> <ul class="field-items"> <li class="field-item even"> <a href="/tutorial-type/accelerometer" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">Accelerometer</a> </li> </ul> </section> Fri, 14 Jun 2013 15:41:43 +0000 aaron 304 at http://dev-explore.appinventor.mit.edu http://dev-explore.appinventor.mit.edu/teach/magic-8-ball#comments