Post-Molemash-Solution

1. MoleMash has an event that doesn't really fit into the category of user-initiated event or external event. What is it?

The MoleMash app responds to a timer event, Clock.Timer. This event is not really user-initiated or external as it is based on the internal clock of the phone. All computer animation is essentially objects moving in response to timer events.

The Clock component has an interval associated with it. The interval is defined in terms of milliseconds (1/1000 of a second). In MoleMash, the Clock.Interval property is set to 500. So every one-half second, the Timer event is triggered. In MoleMash, the Timer event-handler moves the mole:


2. The animation in MoleMash occurs in a Canvas component.

a. What function block allows you to move an image sprite within the canvas? What are its parameters?
b. How do you refer to the vertical and horizontal location of an image sprite within the canvas?  

ImageSprite.MoveTo moves an image sprite. MoveTo has two parameters, x and y, which define the column and row within the canvas to which the image sprite is moved. So x is the horizontal location of the image, and y is the vertical location of the image.

Note that you can move an object by setting the X and Y property separately, instead of using MoveTo.

c. What is the unit of measurement for the canvas?

The canvas consists of tiny dots called pixels. If we say an image sprite is located at (x=5,y=3) we mean that the sprite is located five pixels to the right of the canvas's left edge, and 3 pixels down from the top. More specifically, we mean that the sprite's left-top corner is located there.

x=0
y=0
                   x=20
  y=0
                    
                    
      x=5
y=3
 
             
                    
                    
                    
                    
                    
 x=0
y=9
                  x=20
y=9
The canvas's coordinate system. x gets bigger as you go right, y gets bigger as you go down.

Scan the following app onto your phone to explore the coordinate system:

how to build the coordinate explorer app

d. How big is a canvas? How can you find out how big it is?
You can define how big a canvas is with its Width and Height properties. In the Component Designer, you can set the Width to Fill Parent and the canvas will be as wide as the phone's screen (in pixels). In your blocks, the properties Canvas.Width and Canvas.Height are often used as abstract ways of referring to the canvas's size.

e. How is color defined in App Inventor? How is it defined in HTML?

App Inventor provides a fixed palette of sixteen colors. Most graphics systems provide allow you to define many colors. One common way to define a color is with an RGB value-- three numbers representing how much red, green, and blue is used in the mix to define the color. The three numbers fall between 1 and 255 and are represented in hexadecimal.

3. The MoleMash tutorial has you define one procedures.

a. Name the procedure

MoveMole

b.  Could you write the MoleMash app without defining these procedures?

Yes, you'd just replace the calls to those procedures with the blocks inside them.

c. What is the purpose of defining a procedure, in general?

A procedure is a sub-program, a named sequence of blocks. When your app is going to execute some set of blocks more than once, it is a good idea to define a procedure for those blocks. MoveMole, for instance, is called both in response to a timer event and in response to a Touched event.

Procedures let you add to the language by which you are specifying your program. With a procedure you create a new function, a new block, that can be called. The procedure can abstract some details and give you and other programmers working on your app a high-level term to use. For large apps, breaking down the functionality into procedures is a must. 

Procedural abstraction is a big deal in programming and computer science. Check out p. 47 in the App Inventor book for an explanation.
 
4. Sketch the blocks to increment the HitCount in MoleMash. How would you subtract?

Here are the blocks for incrementing both the hit count and miss count:
In general, you need to say "set score to score + 1"

Subtract would just change the [+] block to a [-] minus block.

5.  When blocks like those to increment score are performed, are they performed left-to-right or right-to-left?


Block sequences are executed right-to-left. First the value of the HitCountLabel.Text is accessed from the app's short term memory. If it is, say, 2, then 1 is added to it. The result, 3, is then placed back into the memory cell for HitCountLabel.Text

6. Discuss how you would implement the variations listed in the MoleMash tutorial, namely:
  • Have the app vary the mole's speed in response to how well the player is doing. 
        The mole's speed is determined by 1) how far you move it on each timer event, and 2) the interval between timer events. When the player touches the mole, you can change the Clock.Interval property to something smaller. Alternatively, you could define a variable for how far to move the mole, and increase it.

Comments