{"id":125,"date":"2014-10-14T02:20:02","date_gmt":"2014-10-14T09:20:02","guid":{"rendered":"http:\/\/appinventor.pevest.com\/?p=125"},"modified":"2014-10-14T02:20:02","modified_gmt":"2014-10-14T09:20:02","slug":"how-to-create-a-bouncing-ball-animation-in-app-inventor-2","status":"publish","type":"post","link":"https:\/\/coldstreams.com\/appinventor\/2014\/10\/14\/how-to-create-a-bouncing-ball-animation-in-app-inventor-2\/","title":{"rendered":"How to create a bouncing ball animation in App Inventor 2"},"content":{"rendered":"<p>App Inventor 2 provides easy to use features for creating games. These features have proven popular for introducing programming concepts to students in K-12, especially at the elementary and middle school level. While our focus is oriented towards practical\u00a0applications rather than games, we can have a little fun too!<\/p>\n<p>The purpose of the\u00a0sample app in this blog post is to illustrate the basic programming concepts of a bouncing ball animation.<\/p>\n<p><strong>The &#8220;Game&#8221;<\/strong><\/p>\n<p>Well, its not much of a game! To keep the programming simple, all this does is enable a ball to bounce around on the screen, to change its heading or trajectory, and to make it travel faster or slower. When the ball hits the edge of the screen, it rebounds in an appropriate direction. Here&#8217;s the user interface as seen on the smart phone:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Screenshot_2014-10-13-11-40-46.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-126 \" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Screenshot_2014-10-13-11-40-46-576x1024.png\" alt=\"Screenshot_2014-10-13-11-40-46\" width=\"322\" height=\"572\" \/><\/a><\/p>\n<p>Press the <em>Start<\/em> button to make the ball start\u00a0moving; press <em>Stop<\/em> to end the movement.<\/p>\n<p>To change the speed of movement press the <em>Faster<\/em> or <em>Slower<\/em> buttons. Pretty easy?<\/p>\n<p>To change the trajectory, you may enter a heading\u00a0value in degrees, in the field next to the <em>Set heading<\/em> button, and then press the <em>Set heading<\/em> button.<\/p>\n<p><!--more--><\/p>\n<p><strong>Coordinate System<\/strong><\/p>\n<p>As soon we\u00a0start talking about graphics, we need to think about our coordinate system &#8211; what does a <strong><em>heading<\/em><\/strong> mean, for example?<\/p>\n<p>In App Inventor, the heading values are\u00a0specified by entering a value from 0 to 359. If we enter a value greater than 359, App Inventor converts the value to a modulus 360 value. Is <em>modulus<\/em> a new word for you? Modulus (sometimes called the modulo function in programming) is a math function best explained by example &#8211; its the remainder part left over after a division.<\/p>\n<p>For example, if we enter 400 degrees, this will be converted to 40 degrees &#8211; that is, 400 minus 360.<\/p>\n<p>The modulus or modulo function is the left over part of dividing the heading (in this case 400) by 360, leaving us with a remainder of 40. Okay, you have probably learned more about modulus than you cared to know. But this is how arbitrarily large values are converted back into the range 0 to 359.<\/p>\n<p>But which way is zero degrees and which way is +90 degrees?<\/p>\n<p>In App Inventor, 0 degrees is directly to the right, +90 degrees is directly upwards, +180 degrees is directly to the left, +270 degrees is directly downwards and 360 degrees takes us back to directly to the right:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/AI-CircleHeading.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-129\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/AI-CircleHeading.png\" alt=\"AI-CircleHeading\" width=\"233\" height=\"233\" \/><\/a>The heading below the mid point of the circle can also be referenced using negative values, as shown. A value of -30 degrees is downwards to the right and a value of -150 would be downwards and to the left.<\/p>\n<p>A heading of zero means our ball will move directly to the right; a heading of 45 degrees means our ball will move in the direction upwards to the right (Between the 0 and 90 degree marks). Thus, a heading refers to the direction of travel for our ball.<\/p>\n<p>When we draw objects on the screen, each object has a position specified as an (X,Y) pair. Think\u00a0back to your high school algebra and drawing graphs: our drawing area has an X and a Y axis. In high school algebra, we generally put the (X,Y) coordinates of (0,0) right in the middle of the chart, with +X values to the right of zero and -X values to the left. Similarly +Y values are above the middle point and -Y values are below.<\/p>\n<p><em>But that is not how App Inventor works!<\/em><\/p>\n<p>Instead of a &#8220;chart&#8221; we are drawing on a &#8220;canvas&#8221;. And the canvas describes the (<em>X,Y<\/em>) point (0,0) as the upper left corner of the drawing surface. Increasing values of <em>X<\/em> refer to a point across to the right on the canvas; increasing values of <em>Y<\/em> refer to a point going downwards from the top of the canvas. Here is a picture to illustrate sample (<em>X,Y<\/em>) coordinate pairs:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/AI-CanvasCoords.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-130\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/AI-CanvasCoords.png\" alt=\"AI-CanvasCoords\" width=\"411\" height=\"210\" \/><\/a><\/p>\n<p>To summarize &#8211; the heading value of zero is horizontally to the right and +90 is straight over head. If you remember that you can figure out the other heading directions.<\/p>\n<p>The (0,0) point of the drawing canvas is at the upper left. We will not be referencing the (X,Y) values in our little app. However, you can initialize the ball to a specific (X,Y) position on the Canvas, if you wish, by setting the Properties for the Ball.<\/p>\n<p><strong>Building the User Interface<\/strong><\/p>\n<p>I created a simple user interface in the Designer, using a horizontal layout to arrange the first four buttons as shown below. The main item that is new, of course, is the <em>Canvas<\/em>. Find the <em>Canvas<\/em> component in the <em>Drawing and Animation<\/em> section of the Palette. Click and drag to the <em>Viewer<\/em> and drop on your app. Also click and drag a <em>Ball<\/em> component on to the <em>Canvas<\/em> area. If you want, you can adjust the properties of the <em>Canvas<\/em> and <em>Ball<\/em> to change their color, as I did with the background of the <em>Canvas<\/em> area (this might be pink but I live in the U.S. Pacific Northwest where we prefer to call this &#8220;salmon&#8221;).<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallUserInterface.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-132\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallUserInterface.png\" alt=\"BallUserInterface\" width=\"281\" height=\"413\" \/><\/a><\/p>\n<p>Here is a list of all the components I added, and the names I gave to the various controls.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallComponents.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-133\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallComponents.png\" alt=\"BallComponents\" width=\"217\" height=\"379\" \/><\/a><\/p>\n<p>I mentioned that the properties for the <em>Ball<\/em> object may be changed:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallProperties.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-134\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallProperties-260x1024.png\" alt=\"BallProperties\" width=\"178\" height=\"701\" \/><\/a><\/p>\n<p>The <em>default<\/em> value for the heading is 0. (&#8220;Default&#8221; in computer programming, means the assumed value if we do not specify something different. In banking, &#8220;default&#8221; is what happens when you do not make the payments on your loan &#8211; and that is completely different than what we mean here. Really.)<\/p>\n<p><strong>The Blocks Program<\/strong><\/p>\n<p>Amazingly, we need add very little code to make our ball jump around the screen. In the <em>Screen&#8217;s<\/em> Initialize event handler, we set the initial heading (direction) for the ball to 210 degrees (you can choose a different value if you wish) and we display the current heading on the screen:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallInitialize.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-135\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallInitialize.png\" alt=\"BallInitialize\" width=\"418\" height=\"113\" \/><\/a><\/p>\n<p>When the app starts running, the speed of movement is set to zero, and hence, the ball is not yet moving. Pressing the <em>Start<\/em> button sets an initial speed of 10 while pressing the <em>Stop<\/em> button sets the speed to zero.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallStartStop.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-136\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallStartStop-1024x134.png\" alt=\"BallStartStop\" width=\"625\" height=\"81\" \/><\/a><\/p>\n<p>Pressing the <em>Faster<\/em> or <em>Slower<\/em> buttons just changes the speed in increments of 10. Note that for the <em>Slower<\/em> option, we choose to prevent the use of negative speeds (speed less than zero) by using an <em>if then<\/em> block to only set the speed to positive values.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallFasterSlower.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-137\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallFasterSlower-1024x425.png\" alt=\"BallFasterSlower\" width=\"625\" height=\"259\" \/><\/a><\/p>\n<p>Changing the heading is simple &#8211; we set the <em>Ball&#8217;s<\/em> property <em>Heading<\/em> to the value we have entered. We do a little &#8220;user input error checking&#8221; to ensure that the user has entered an actual number:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallSetHeading.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-138\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallSetHeading-1024x321.png\" alt=\"BallSetHeading\" width=\"625\" height=\"195\" \/><\/a><\/p>\n<p>And finally, the big moment &#8211; now that our ball is moving, how do we handle a collision with the edge of the box on the screen? Would you believe it is this easy?<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallEdgeReached.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-139\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/BallEdgeReached.png\" alt=\"BallEdgeReached\" width=\"910\" height=\"294\" \/><\/a>We write an event handler for <em>EdgeReached<\/em> &#8211; this event is triggered when the ball collides with an edge. We could write some code of our own to adjust the heading of the ball upon encountering the edge &#8211; but we do not have to because this function is built in to App Inventor! Use the &#8220;<em>call Ball1.Bounce<\/em>&#8221; procedure to automatically bounce the ball off the wall it hit &#8211; the local variable &#8220;<em>edge<\/em>&#8221; holds a number that corresponds to the top, left, right, bottom and so on, and the <em>Bounce<\/em> procedure uses this to determine which way to bounce the ball. You can find the &#8220;get edge&#8221; block by clicking on the &#8220;edge&#8221; value in the surrounding event handler.<\/p>\n<p>That is pretty easy!<\/p>\n<p>There is much more that can be done with the <em>Ball<\/em> component. For example, we can add some code so that we can touch the ball with our finger and &#8220;fling&#8221; the ball in a new direction. When this is done, an event is generated that our program can process.<\/p>\n<p>What if you wanted to bounce something other than a plain ball around on the screen? Check out <em>ImageSprite<\/em> in the Drawing and Animation palette. It works the same as <em>Ball<\/em> except that you can set a <em>picture<\/em> for the object that moves around. <em>Hmmmm &#8230; I&#8217;d better write up a tutorial on using pictures soon!<\/em><\/p>\n<p>But the goal of\u00a0this tutorial is just to get you started! Have fun!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>App Inventor 2 provides easy to use features for creating games. These features have proven popular for introducing programming concepts to students in K-12, especially at the elementary and middle school level. While our focus is oriented towards practical\u00a0applications rather than games, we can have a little fun too! The purpose of the\u00a0sample app in &hellip; <a href=\"https:\/\/coldstreams.com\/appinventor\/2014\/10\/14\/how-to-create-a-bouncing-ball-animation-in-app-inventor-2\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to create a bouncing ball animation in App Inventor 2<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,10],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-components","category-programming-method"],"_links":{"self":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":0,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"wp:attachment":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}