A simple component scripts.
cc.Class({
extends: cc.Component,
properties: {
},
// use this for initialization
onLoad: function () {
},
// called every frame, uncomment this function to activate update callback
update: function (dt) {
},
});
Declared in the component by script attribute, we can script components visually displayed in the field attribute inspector , thereby easily adjust the value of the attribute in the scene.
To declare a property, only needed cc.Class defined properties
field, fill in the attribute name and attribute parameters can be convenient to view the changes in the Properties inspector.
Usefulness write these scripts: This code is to write a component (script) needed for construction. Cocos Creator script is having the components of such a structure (the Component), they can be mounted to the nodes in the scene, provides various functions of the control node. For example: the example of the Player control small monster jumping, Game generate stars.
Complete statement:
properties: {
score: {
default: 0,
displayName: "Score (player)",
tooltip: "The score of player",
}
}
The above code score
attribute set of three parameters default
, displayName
and tooltip
. These parameters are assigned score
default value 0, the property inspector , the attribute name is shown in which: "Score (player)", and when the mouse is moved on the parameters corresponding to the display Tooltip.
The following is a type, displayName attribute parameters such as: http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
These parameters control the properties in the property inspector is displayed, as well as the behavior of the properties of a sequence of scenes in the process.
type parameter
When default
the time does not provide sufficiently detailed information of the type, in order to property inspector shows the correct control inputs, it is necessary with type
explicitly declared specific types:
-
When the default value is null, setting type to the specified type constructors, this property inspector only know a Node display control.
enemy: { default: null, type: cc.Node }
-
When the default value is the value (number) types, the type is set
cc.Integer
, which is an integer used to indicate such properties in the property inspector Lane not enter a decimal point.score: { default: 0, type: cc.Integer }
-
When the default value is an enumeration (
cc.Enum
the time), because the enumeration value itself is actually a digital (number), so you want to type set as an enumerated type can in the Property inspector display enumeration drop-down box.wrap: { default: Texture.WrapMode.Clamp, type: Texture.WrapMode }