Corona Images, Shapes, and Text About Display Objects Part4
Text
display.newText( [parentGroup,] string, x, y, font, size ) creates a text object with its top-left corner at (x, y). The local origin is at the center of the text; the reference point is initialized to this local origin. By default, there is no text color.
Text
display.newText([parentGroup]字符串的x,y,字體,大小)創建一個文本對象的左上角在(x,y)的。當地的原產地是在該中心的文本;參考點初始化為這個地區的起源。默認情況下,沒有文字的顏色。
The font
parameter is a string that names the font.
You can obtain an array of available fonts vianative.getFontNames()
.
Alternatively, you can also pass the following constants instead of a
string:
字體參數是一個字符串,名稱的字體。你能獲得一個陣列的可用字體vianative.getFontNames()。另外,您還可以通過以下常量,而不是一個字符串:
- native.systemFont
- native.systemFontBold
object.size的大小的文字
object:setTextColor上(R,G,B的[,1])的所有組件必須介於 0和255之間。Alpha是可選的255(不透明)默認情況下。
object.text是一個字符串,包含文本的TextField。這可以用來更新字符串值的文本對象。
ex:
local myText = display.newText( "Hello, World", 0, 0, "Helvetica", 16 )
myText:setTextColor(0, 0, 255)
--Change the value of myText
myText.text = "Hello, Universe!"
Using a custom font
You can embed a font in your app. Include the font file in your project directory. You must have the legal right to embed the font. For iOS, you must register the font using the UIAppFonts plist entry. This will only work with iOS 3.2 and higher, and Android versions 1.6 and higher. (The Harrowprint font used in this example can be found here). The plist entry includes the font's extension, and is included in the build.settings file. No build.settings entry is required on Android.
使用自定義字體
您可以嵌入你的應用程序中的一種字體。包括字體文件在您的項目目錄。您必須有合法的權利嵌入字體。內部監督辦公室,你必須註冊使用的字體 UIAppFonts plist中的條目。這將只能與 IOS3.2以及更高,而Android1.6和更高版本。 (該Harrowprint這個例子中使用的字體可以在這裡找到)。 plist中的條目包含字體的延伸,包括在build.settings文件。沒有 build.settings需要輸入在Android。
settings =
{
iphone =
{
plist =
{
UIAppFonts =
{
"Harrowprint.ttf"
}
},
},
}
Then, refer to the font when creating the text object. You must use the name of the font as returned by native.getFontNames(), not the file name.
然後,是指在創建字體的文本對象。您必須使用的名稱的字體返回 native.getFontNames(),而不是文件名。
local text = display.newText( "Hello Harrowprint font!", 0, 0, "Harrowprint", 50 )
Groups
Group
objects are a special type of DisplayObject
. You can add other
display objects as children of a group object. You can also remove them. Even if
an object is not visible, it remains in the group object until explicitly
removed. Thus, to minimize memory consumption, you should explicitly remove any
object that will no longer be used.
All objects are implicitly added to the current stage which itself is a kind of group object. (Currently, there is only one stage in Corona. This stage represents the entire screen.)
The children of group objects are simply other display objects (vector, groups, etc.). They are accessed by integer index. The first child is at index 1.
團體
組對象是一個特殊類型的DisplayObject。您可以添加其他顯示對象隨著孩子的一組對象。您還可以將其刪除。即使對象是不可見的,但它仍然在組對象,直到顯式刪除。因此,為了最大限度地減少內存消耗,你應該明確地刪除任何對象將不再使用。
所有的對象都是隱式添加到當前階段,這本身就是一個種組對象。 (目前,只有一個階段,電暈。這一階段代表了整個畫面。)
孩子們的組對象僅僅是其他顯示對象(向量,團體等)。他們訪問的整數索引。第一個孩子是在索引1。
Note
Lua table
library functions such as table.insert
and ipairs(table)
are incompatible with groups. This also includes getting the
size of a group. #table
will return the size of table but not a
group or any other display object. To get the size of a group (the number of
items in the group) use group.numChildren
注意:
Lua表庫的功能,如table.insert和ipairs(表)不符合團體。這也包括讓一組的大小。 #表將返回表的大小,但沒有一個團體或任何其他顯示對象。為了得到一組的大小(這個數字中的項目組)使用group.numChildren
Creating groups
display.newGroup() creates a group in which you can add and remove child display objects. Initially, there are no children in a group. The local origin is at the parent’s origin; the reference point is initialized to this local origin.
創建組
display.newGroup()創建一個組,可以在其中添加和刪除子顯示對象。起初,有沒有孩子在一組。當地的原產地是在父母的起源,參考點初始化為這個地區的起源。
Getting the Number of Children in a Group (Size)
local group = display.newGroup() group:insert( rect1 ) -- assume rect1 is an existing display object group:insert( rect2 ) -- assume rect2 is an existing display object for i=1, group.numChildren do local child = group[i] local description = (child.isVisible and "visible") or "not visible" print( "child["..i.."] is " .. description ) end
Inserting and Removing Objects in a Group
child
at index into group, shifting up other
elements as necessary. The default value index is n+1
where n
is the number of children in the group.
The resetTransform
parameter determines what happens to child
’s transform. When false, child
’s local position, rotation, and
scale properties are preserved, except the local origin is now relative to the
new parent group, not its former parent; when true, child
’s transform is reset (i.e. the
x, y, rotation, xScale, and yScale properties of child
are reset to 0, 0, 0, 1, and 1,
respectively). The default value for resetTransform is false. An easy way to move an object above
its siblings is to re-insert it:object.parent:insert( object )
.孩子在索引插入到組,轉移了其他元素是必要的。默認值是n+1指數其中n是多少兒童組。該 resetTransform參數確定會發生什麼事對孩子的轉變。當假的,孩子的局部位置,旋轉和縮放屬性被保留,除了本地的起源是現在相對於新的父組,而不是它的前母公司,當真實的,孩子的變換復位(即在x,y,旋轉,Xscale處理器,兒童和yScale屬性被重置為 0,0,0,1和1,分別)。默認值resetTransform是假的。一個簡單的方法來移動對象以上的兄弟姐妹是重新插入它:object.parent:插入(對象)。
Note
That if you insert a display object into one group and then later insert the same object into another group, the display object is removed the original group into which it was inserted. An object can only exist in one group at a time. The following code sample illustrates this:
注意:
如果你插入一個顯示對象為一組,然後插入相同的對象,後來到另一組,顯示對象被刪除原組到它插入。一個對象只能存在於一組一次。下面的代碼示例說明了這一點:
local txt=display.newText('Hello',0,0) local g1=display.newGroup() local g2=display.newGroup() -- Insert text object into g1 g1:insert(txt) -- Insert same text object into g2 g2:insert(txt) print("g1[1]: " .. tostring(g1[1])) -- prints nil print("g2[1]: " .. tostring(g2[1])) -- prints textObject
group:remove( indexOrChild )
Removes
from group the display object specified by indexOrChild
, shifting down other
elements as needed. The argument indexOrChild is either the index position of
the child within group or the child display object itself. In either case, it
returns the removed display object.
group:remove(
indexOrChild )
刪除組中的顯示對象指定indexOrChild,轉移了其他元素的需要。這個論點 indexOrChild要么是索引位置的孩子的內心組或子顯示對象本身。在兩種情況下,它返回被刪除的顯示對象。
Stage
Stage objects are a special type of GroupObject, so all methods and properties of group objects are available in stage objects. Conceptually, the stage is the root group for all display objects and groups. You access the current stage object using display.getCurrentStage().
All objects are implicitly added to the current stage which itself is a kind of group object. (Currently, there is only one stage in Corona. This stage represents the entire screen.)
Setting Focus
stage:setFocus( displayObject [, touchID]
) sets displayObject
as the target for all future hit events.
Pass nil to restore default behavior for hit event
dispatches. This is typically used to implement buttons that change appearance
when a user initially presses them.
Stage(舞台)
舞台上的對象是一個特殊類型的GroupObject,因此所有的方法和屬性的組對象可在舞台上的對象。從概念上講,舞台是根組中所有顯示對象和群體。現階段您訪問對象使用display.getCurrentStage()。
所有的對象都是隱式添加到當前階段,這本身就是一個種組對象。 (目前,只有一個階段,電暈。這一階段代表了整個畫面。)
Setting Focus(焦點設置)
階段:使用SetFocus(DisplayObject的[,touchID])設置為目標的DisplayObject今後所有打事件。通無恢復默認行為攻擊事件調度。這通常是用於實現按鈕,當用戶改變外觀最初按他們。
Other Display Functions
display.captureScreen( saveToAlbum ) captures the contents of the screen and
returns it as an image object positioned so that the top-left of the screen is
at the origin. If saveToAlbum
is true, then it adds the image to your device’s
album; on the simulator, it saves to the desktop.
其他顯示功能
display.captureScreen(saveToAlbum)捕獲屏幕內容並返回它作為一個圖像對象定位,使左上角的屏幕是在原點。如果saveToAlbum是真的,那麼它添加到您的設備圖像的專輯,在模擬器上,它保存到桌面。
display.getCurrentStage( ) returns a reference to the current stage
object, which is the root group for all
display objects and groups. Currently, Corona has a single stage
instance, so this function always returns a
reference to the same object (which represents the entire screen).
display.getCurrentStage()返回一個引用到當前階段的對象,這是根組中所有顯示對象和群體。目前,電暈有一個階段的實例,所以這個函數總是返回一個引用同一個對象(它代表整個屏幕)。
display.save( displayObject, filename [,
baseDirectory] ) renders the
display object referenced bydisplayObject
into a JPEG image and saves it in filename
. The display object must
currently be in the display hierarchy; otherwise no image is saved. If the
display object is a group object, then all children are rendered. The
argument baseDirectory
is optional. It can be either system.DocumentsDirectory
(the default)
or system.TemporaryDirectory
.
display.save(DisplayObject的,文件名[,baseDirectory])呈現的顯示對象的DisplayObject引用到一個
JPEG圖像並保存在文件名。顯示對象當前必須在顯示層次,否則沒有圖像被保存。如果顯示對象是一組對象,那麼所有兒童都呈現。這個論點
baseDirectory是可選的。它可以是system.DocumentsDirectory(默認)或system.TemporaryDirectory。
display.setStatusBar( mode ) hides or changes the appearance of the
status bar. The argument mode should be one of:
display.setStatusBar(模式),隱藏或改變外觀的狀態欄。這個論點模式應該是如下之一:
- display.HiddenStatusBar
- display.DefaultStatusBar
- display.TranslucentStatusBar
- display.DarkStatusBar
Content Size Properties
display.contentWidth A read-only property representing the
original width of the content in pixels.This will default to the screen width,
but may be another value if you are using content scaling inconfig.lua
.
display.contentWidth一個只讀屬性,代表原始寬度的內容在pixels.This將默認為屏幕寬度,但可能是另一種價值,如果您使用的是內容縮放
config.lua。
display.contentHeight A read-only property representing the
original height of the content in pixels. This will default to the screen
height, but may be another value if you are using content scaling inconfig.lua
.
display.contentHeight一個只讀屬性,代表原始高度以像素為單位的內容。這將默認為屏幕的高度,但可能是另一種價值,如果您使用的是內容縮放 config.lua。
display.viewableContentWidth A read-only property that contains the width of the viewable screen area in pixels, within the coordinate system of the original content. This is useful since depending on which dynamic scaling mode is used, and the aspect ratio of the device used to view the content, some of the original content may be scaled in such a way that portions are off-screen.
display.viewableContentWidth只讀屬性,其中包含的寬度以像素可視屏幕區域內的坐標系統中的原始內容。這是有用的,因為這取決於使用動態縮放模式,和長寬比的設備用於查看的內容,一些原來的內容可能被縮小在這樣一種方式,部分是屏幕外。
display.viewableContentHeight A read-only property that
contains the height of the viewable screen area in pixels, within the coordinate
system of the original content. This is useful since depending on which dynamic
scaling mode is used, and the aspect ratio of the device used to view the
content, some of the original content may be scaled in such a way that portions
are off-screen.
display.viewableContentHeight只讀屬性,其中包含了高度的可視屏幕區域的像素,在坐標系統中的原始內容。這是有用的,因為這取決於使用動態縮放模式,和長寬比的設備用於查看的內容,一些原來的內容可能被縮小在這樣一種方式,部分是關閉屏幕。
display.statusBarHeight A read-only property representing
the height of the status bar in pixels (only valid on iOS
devices).
display.statusBarHeight一個只讀屬性,代表了高度的狀態欄像素(只適用於
iOS設備)。
Display Objects vs. Tables: A Technical Discussion
At the beginning of this section, we described display objects as behaving "similarly" to Lua tables. An exception to this rule is that you cannot set the metatables of display objects. This is due to the nature of our C++/Lua integration in this particular case.
Corona display objects are native C++ objects internally. The way Lua implements binding native pointers is with "userdata", which sets the metatable. Userdata metatables cannot be replaced by Lua code; this is a measure defined by Lua (see Programming in Lua for details).
Normally, native objects bound to Lua in this way do not behave like tables, and there are a number of examples in Corona of this. Such objects may have properties and/or methods, but are not extensible. In contrast, Corona display objects do behave like tables — this is a convenience feature, and the alternative would have been having a field for "user data", allowing you to access your own data associated with a display object. Raising this functionality directly into the display object itself is simpler and more extensible for the end user, which is why this was done this way. So the underlying native bridge is constrained by what is allowed in Lua, and we view this architectural choice as a fundamental design feature of Corona. Ultimately, Corona is a C++/OpenGL engine with frameworks designed for rapid development, and the product's premise is that these advantages justify this use of metatables on the display objects.
顯示對象的對比表:一個技術討論
在開始本節,我們介紹了顯示對象的行為“同樣“到Lua表。一個例外是,你不能設置元表的顯示對象。這是由於其性質我們的C ++ / Lua的整合在這個特殊的情況。
科羅納顯示對象是原生C ++對象內部。結合的方式實現的Lua本地指針是與“用戶數據“,這台元表。用戶數據元表不能代替Lua代碼,這是一項措施規定由Lua(見編程 Lua中的細節)。
通常情況下,本機對象綁定到Lua這種方式並不像表,並有一個數字的例子電暈這一點。這樣的對象可以有屬性和/或方法,但不可擴展。與此相反,電暈顯示對象做像表-
這是一個方便的功能,替代會一直有一個字段為“用戶數據“,讓您可以訪問自己的數據與顯示對象。此功能直接提高到顯示對象本身更簡單,更可擴展為最終用戶,這就是為什麼這樣做是這樣的。因此,基礎本機橋受制於哪些是允許Lua中,我們認為這建築的選擇,因為一個基本的設計特點電暈。最終,電暈是一個 C++/ OpenGL的引擎框架設計的快速發展,產品的前提是,這些優勢證明這種使用元表上的顯示對象。
留言列表