Programming Your App’s Memory 靜宜大學資管系 楊子青
Memory Slots Just as people need to remember things, so do apps. When someone tells you the phone number of a pizza place for a onetime immediate call, your brain stores it in a memory slot. In this chapter, you’ll learn how to set up an app’s memory, how to store information in it, and how to retrieve that information at a later time.
Named Memory Slots An app’s memory consists of a set of named memory slots. Some of these memory slots are created when you drag a component into your app; these slots are called properties. You can also define named memory slots that are not associated with a particular component; these are called variables.
1. Properties Components (Button, TextBox, and Canvas…) are part of the user interface. Each component is completely defined by a set of properties. The values stored in the memory slots of each property determine how the component appears.
Modifying the memory slots in the property form to change the app’s appearance
2. Variables Defining Variables Setting and getting a Variable Setting a Variable to an Expression Incrementing a Variable Building Complex Expressions Displaying Variables
2.1 Defining Variables You define a variable when your app needs to remember something that is not being stored within a component property. For example, a game app might need to remember what level the user has reached. If the level number is not something the user will see, you’d define a variable to store it.
Defining Variables 1. 拖曳出 initialize global name to
Defining Variables 2. 設定變數名稱 3. 設定初值
2.2 Setting and getting a Variable 設定(變更)變數值 選擇變數名稱
Setting and getting a Variable 取得變數值 選擇變數名稱
2.3 Setting a Variable to an Expression Incrementing a Variable Building Complex Expressions
Math drawer In the Math drawer, App Inventor provides a wide range of mathematical functions.
2.4 Displaying Variables Changing a variable has no direct effect on the app’s appearance. When you modify a component property, the user interface is directly affected. This is not true for variables. This is one of the advantages of storing data in a variable as opposed to a component property it allows you to show just the data you want when you want to show it. It also allows you to separate the computational part of your app from the user interface, making it easier to change that user interface later.
HelloPurr_Extend
1. 複製專案進行修改 開啟HelloPurr專案,Save as為HelloPurr_Extend
Media部分,以Upload File加入獅子圖片(lion.jpg)與獅吼聲音(roar.mp3)
回顧:LabelName的Text屬性,決定顯示文字內容
回顧:Button1的Image屬性,決定該按鈕的長相(顯示哪張圖片)
回顧:Sound1的Source屬性,決定 播放哪個聲音檔
如何動態調整顯示文字? 圖片?聲音?
執行結果: 初次播放roar.mp3發生error
Google找解決方法
Screen1.Initialize
鱷魚拔牙整人玩具 http://www.i-gamer.net/play/2978.html
2. 按圖片,第6次出現獅子
3. 隨機第2-6次出現獅子