{"id":183,"date":"2014-10-21T14:00:22","date_gmt":"2014-10-21T21:00:22","guid":{"rendered":"http:\/\/appinventor.pevest.com\/?p=183"},"modified":"2014-10-21T14:00:22","modified_gmt":"2014-10-21T21:00:22","slug":"controlling-the-sprite-animation-with-your-finger-on-the-screen","status":"publish","type":"post","link":"https:\/\/coldstreams.com\/appinventor\/2014\/10\/21\/controlling-the-sprite-animation-with-your-finger-on-the-screen\/","title":{"rendered":"Controlling the sprite animation with your finger on the screen"},"content":{"rendered":"<p>In <a href=\"http:\/\/appinventor.pevest.com\/?p=167\">the prior tutorial<\/a>, we created a sprite-based bouncing animation &#8211; which is surprisingly easy to do in App Inventor.<\/p>\n<p>But how might we make the animation interactive? Such as touching the bouncing object with a finger and flinging it (throwing it) off in another direction? In other words, as the object travels around the screen, we would like to touch it with our finger and then &#8220;drag&#8221; the object off in a new direction.<\/p>\n<p>To implement this, just handle the\u00a0Image Sprite&#8217;s &#8220;Flung&#8221; event, like this:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/ImageSprite-Flung.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-184\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/ImageSprite-Flung.png\" alt=\"ImageSprite-Flung\" width=\"555\" height=\"153\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>App Inventor (and the Android OS) handle the details of sensing our finger on the screen and translating that into some values we can use in our app.<\/p>\n<p>When the &#8220;<em>Flung<\/em>&#8221; event occurs, App Inventor passes along several values &#8211; including the (<em>x,y<\/em>) coordinate values at the time we touch the object, the <em>speed<\/em> and <em>heading<\/em> in which we&#8217;ve dragged the object with our finger, and the x and y velocity. \u00a0When this event is detected, our program sets the sprite&#8217;s current heading and speed to the values determined by our finger drag across the screen.<\/p>\n<p>Click on the &#8220;<em>speed<\/em>&#8221; and &#8220;<em>heading<\/em>&#8221; parameters in the <em>Flung<\/em> event to find the &#8220;get heading&#8221; and &#8220;get speed&#8221; values.<\/p>\n<p>I multiplied the\u00a0speed parameter by 3 as the values returned by <em>Flung<\/em> were small and resulted in slow movement of the sprite. You can set this value to a smaller or larger value based on your preference.<\/p>\n<p>Here&#8217;s the app screen, when in use:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Screenshot_2014-10-20-15-53-14.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-185\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/Screenshot_2014-10-20-15-53-14-168x300.png\" alt=\"Screenshot_2014-10-20-15-53-14\" width=\"168\" height=\"300\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>As the sprite moves across the screen, touch it with your finger and &#8220;fling&#8221; it off in a new direction. (You may have to experiment for a bit in terms of how you do your &#8220;flings&#8221;).<\/p>\n<p><strong>Stopping the Sprite When Touched<\/strong><\/p>\n<p>If the sprite is moving quickly, it moves out from under your finger before you can take action. To fix this, we sense the finger touching the sprite and stop the sprite while it is being touched.<\/p>\n<p>We implement the &#8220;<em>TouchDown<\/em>&#8221; event for <em>ImageSprite1<\/em>. The moment our finger touches the sprite, the\u00a0sprite is\u00a0stopped by setting its <em>Speed<\/em> property to zero. Now we can casually &#8220;fling&#8221; the sprite in a new direction. \u00a0When we stop touching the sprite, the &#8220;<em>TouchUp<\/em>&#8221; event occurs &#8211; here we restore the original speed so that the sprite resumes moving. Of course, the &#8220;<em>Flung<\/em>&#8221; event occurs next and sets a new speed for the sprite based on our finger movement.<\/p>\n<p><strong>Update<\/strong>: Delete &#8211; do not use &#8211; the <em>.Touchup<\/em> block, shown below. It is not needed. Instead, when you lift your finger up as part of a &#8220;flung&#8221; operation, the flung event handler will set the sprite&#8217;s speed proportional to your finger movement. Thus, it is not necessary to restore the saved speed value.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/ImageSprite-Touch.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-186\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/ImageSprite-Touch.png\" alt=\"ImageSprite-Touch\" width=\"508\" height=\"284\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the prior tutorial, we created a sprite-based bouncing animation &#8211; which is surprisingly easy to do in App Inventor. But how might we make the animation interactive? Such as touching the bouncing object with a finger and flinging it (throwing it) off in another direction? In other words, as the object travels around the &hellip; <a href=\"https:\/\/coldstreams.com\/appinventor\/2014\/10\/21\/controlling-the-sprite-animation-with-your-finger-on-the-screen\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Controlling the sprite animation with your finger on the screen<\/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-183","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\/183","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=183"}],"version-history":[{"count":0,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/183\/revisions"}],"wp:attachment":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/media?parent=183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/categories?post=183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/tags?post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}