前言
最近在搞playcanvas引擎,此篇文章可以快速让你入门,完成一些基础的开发。
传送门
PlayCanvas(二)__EngineSetting篇
PlayCanvas(三)__进阶篇
PlayCanvas(四)__业务逻辑篇
PlayCanvas(五)__商业项目篇
PlayCanvas(六)__发布篇
基础语法
添加属性
- entity
- number
- string
- array
- enmu
- asset
XXX.attributes.add("ModelOne", {
type: "entity"
});
XXX.attributes.add('pitchAngleMin', {
type: 'number',
default: 0,
title: '我是标题,不写默认是pitchAngleMin',
description: '我是说明.'
});
XXX.attributes.add('str', {
type: 'string',
default: str11
});
XXX.attributes.add("arrayUI",{
type:"entity",
array:!0
});
XXXXX.attributes.add("eventType",{
type: "number",
"default": 0,
"enum": [{
StepChangeEvent: 0
},{
hahaha : 1
}]
});
XXXXX.attributes.add("asset",{
type:"asset"
});
定义变量
var a = 1;//类外 var,表示整个游戏全局都可访问
this.a = 1; //方法中 this 表示类局部变量 ,外部声明可以调用
class.a = 1;//类内全局使用,会被覆盖,外部声明调用不了
定义方法
//声明prototype只能在类中调用,必须有实例才能调用,一般都声明
Commont.prototype.ActiveGo = function(go,b)
{
go.enabled = b;
};
//为静态方法,可以封装一些工具脚本
Commont.ActiveGo = function(go,b)
{
go.enabled = b;
};
方法顺序
var SwingHeart = pc.createScript('swingHeart');
SwingHeart.prototype.initialize = function() {
console.log("进入init");
this.on('disable', this.OnDisable);
this.on('enable', this.OnEnable);
this.on('destroy', this.OnDestory);
};
SwingHeart.prototype.OnEnable = function()
{
console.log("进入OnEnable");
};
SwingHeart.prototype.OnDisable = function()
{
console.log("进入OnDisable");
};
SwingHeart.prototype.OnDestory = function(name,scriptInstance)
{
console.log("进入OnDestory");
};
SwingHeart.prototype.update = function(dt) {
console.log("进入update");
};
基础操作
获取同一个Entity上的其他脚本
this.xx= this.entity.script.xxxx;
事件
注册事件
//参数 string func xxx是类名
this.xxx.on("EventOne", this.onStepChange, this);
this.app.on();
触发事件
this.xxx.fire(globals.Evt_GeneralEvent, 0); //0是参数
this.app.fire();
移除事件
this.xxx.off("EventOne", this.onStepChange, this);
this.app.off();
鼠标事件和Touch事件
//mouseup touchstart touchend
this.entity.element.on('mousedown', this.onStateChange, this);
//鼠标
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
//Touch
this.app.touch.on(pc.EVENT_TOUCHSTART, this.onTouchStartEndCancel, this);
//注销事件
this.app.touch.off(pc.EVENT_TOUCHSTART, this.onTouchStartEndCancel, this);
UI触发事件
Entity添加Element组件,并且开启Use Input。
键盘事件
if (this.app.keyboard.wasPressed(pc.KEY_SPACE))
{
console.log();
}
Comments | NOTHING