首页 > 代码库 > Cocos Creator脚本开发事例
Cocos Creator脚本开发事例
HelloWorld.js
1 window.Global = { 2 gint: 168, 3 }; 4 cc.Class({ 5 extends: cc.Component, 6 7 properties: { 8 label: { 9 default: null,10 type: cc.Label11 },12 // defaults, set visually when attaching this script to the Canvas13 text: ‘Hello, World!‘14 },15 16 // use this for initialization17 onl oad: function () {18 this.label.string = this.text;19 20 },21 22 // called every frame23 update: function (dt) {24 25 },26 });
SayHello.js
1 var helloWorld = require("HelloWorld"); 2 cc.Class({ 3 extends: cc.Component, 4 5 properties: { 6 userID: { 7 default: 20, 8 displayName: "用户ID", 9 tooltip: "用户的ID",10 },11 userName: "Foobar",12 pos: new cc.Vec2(10, 20),13 color: new cc.Color(255, 255, 255, 128),14 any: [], // 不定义具体类型的数组15 bools: [cc.Boolean],16 strings: [cc.String],17 floats: [cc.Float],18 ints: [cc.Integer],19 values: [cc.Vec2],20 nodes: [cc.Node],21 frames: [cc.SpriteFrame],22 score: {23 default: 0,24 displayName: "Score (player)",25 tooltip: "The score of player",26 },27 width: {28 get: function () {29 return this._width;30 },31 set: function (value) {32 this._width = value;33 }34 },35 player: {36 default: null,37 type: cc.Node38 }39 },40 41 // use this for initialization42 onl oad: function () {43 var Shape = cc.Class({44 properties: {x:1},45 ctor: function () {46 cc.log("Shape"); // 实例化时,父构造函数会自动调用,47 },48 print: function (str) { cc.log("print:" + str+" x:"+this.node.x); },49 start: function () {50 var node = this.node;51 node.x = 100;52 }53 });54 var shape = new Shape();55 //shape.print("hello");56 //shape.start();57 var node = this.node;58 cc.log(node);59 cc.log(node.name);60 cc.log(node.color);61 62 var sprite = this.getComponent("cc.Sprite");63 if(sprite!=null){64 cc.log("sprite.type:"+sprite.type); 65 }66 67 var label = this.getComponent("cc.Label");68 if(label!=null){69 cc.log("label:"+label.fontSize);70 }71 cc.log("this.userID:"+this.userID);72 if(this.player){73 var sprite2 = this.player.getComponent("cc.Sprite");74 cc.log("sprite2:"+sprite2);75 if(sprite2!=null){76 cc.log("this.player.sprite.type:"+sprite2.type);77 } 78 var sayHello = this.player.getComponent("SayHello");79 cc.log("sayHello:"+sayHello);80 if(sayHello!=null){81 cc.log("this.player.sayHello.userID:"+sayHello.userID);82 } 83 }84 cc.log(Global.gint);85 cc.log("helloWorld:"+helloWorld);86 var hw=new helloWorld();87 cc.log("hw:"+hw);88 cc.log("text:"+hw.text);89 90 },91 // called every frame, uncomment this function to activate update callback92 update: function (dt) {93 cc.log("update..");94 },95 });
Cocos Creator脚本开发事例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。