Commit 554fcf01 by 李楚霏

移动操作

parent 70b92bde
...@@ -75,22 +75,25 @@ ...@@ -75,22 +75,25 @@
"__id__": 5 "__id__": 5
}, },
{ {
"__id__": 7 "__id__": 8
}, },
{ {
"__id__": 12 "__id__": 10
},
{
"__id__": 16
} }
], ],
"_active": true, "_active": true,
"_components": [ "_components": [
{ {
"__id__": 17 "__id__": 20
}, },
{ {
"__id__": 18 "__id__": 21
}, },
{ {
"__id__": 19 "__id__": 22
} }
], ],
"_prefab": null, "_prefab": null,
...@@ -180,7 +183,7 @@ ...@@ -180,7 +183,7 @@
"array": [ "array": [
0, 0,
0, 0,
371.6221153255573, 573.7012350663882,
0, 0,
0, 0,
0, 0,
...@@ -241,7 +244,7 @@ ...@@ -241,7 +244,7 @@
}, },
{ {
"__type__": "cc.Node", "__type__": "cc.Node",
"_name": "bg", "_name": "joyStick",
"_objFlags": 0, "_objFlags": 0,
"_parent": { "_parent": {
"__id__": 2 "__id__": 2
...@@ -251,6 +254,153 @@ ...@@ -251,6 +254,153 @@
"_components": [ "_components": [
{ {
"__id__": 6 "__id__": 6
},
{
"__id__": 7
}
],
"_prefab": null,
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 720,
"height": 1280
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
0,
0,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": "d2h2ShBatItJOqNhnibdTb"
},
{
"__type__": "3b6caruSLNAnp8j/pNXOWou",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 5
},
"_enabled": false,
"label": null,
"text": "hello",
"_id": "8bUf4RweZFWZ3TnM08/0Ig"
},
{
"__type__": "cc.Button",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 5
},
"_enabled": true,
"_normalMaterial": null,
"_grayMaterial": null,
"duration": 0.1,
"zoomScale": 1.2,
"clickEvents": [],
"_N$interactable": true,
"_N$enableAutoGrayEffect": false,
"_N$transition": 0,
"transition": 0,
"_N$normalColor": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_N$pressedColor": {
"__type__": "cc.Color",
"r": 211,
"g": 211,
"b": 211,
"a": 255
},
"pressedColor": {
"__type__": "cc.Color",
"r": 211,
"g": 211,
"b": 211,
"a": 255
},
"_N$hoverColor": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"hoverColor": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_N$disabledColor": {
"__type__": "cc.Color",
"r": 124,
"g": 124,
"b": 124,
"a": 255
},
"_N$normalSprite": null,
"_N$pressedSprite": null,
"pressedSprite": null,
"_N$hoverSprite": null,
"hoverSprite": null,
"_N$disabledSprite": null,
"_N$target": {
"__id__": 5
},
"_id": "0aEgKUAvpMsKyUXJZb+pyX"
},
{
"__type__": "cc.Node",
"_name": "bg",
"_objFlags": 0,
"_parent": {
"__id__": 2
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 9
} }
], ],
"_prefab": null, "_prefab": null,
...@@ -306,7 +456,7 @@ ...@@ -306,7 +456,7 @@
"_name": "", "_name": "",
"_objFlags": 0, "_objFlags": 0,
"node": { "node": {
"__id__": 5 "__id__": 8
}, },
"_enabled": true, "_enabled": true,
"_materials": [ "_materials": [
...@@ -344,19 +494,19 @@ ...@@ -344,19 +494,19 @@
"_active": true, "_active": true,
"_components": [ "_components": [
{ {
"__id__": 8 "__id__": 11
}, },
{ {
"__id__": 9 "__id__": 12
}, },
{ {
"__id__": 10 "__id__": 13
}, },
{ {
"__id__": 15 "__id__": 14
}, },
{ {
"__id__": 16 "__id__": 19
} }
], ],
"_prefab": null, "_prefab": null,
...@@ -382,7 +532,7 @@ ...@@ -382,7 +532,7 @@
"__type__": "TypedArray", "__type__": "TypedArray",
"ctor": "Float64Array", "ctor": "Float64Array",
"array": [ "array": [
-206.828, -85.4,
63.222, 63.222,
0, 0,
0, 0,
...@@ -412,7 +562,7 @@ ...@@ -412,7 +562,7 @@
"_name": "", "_name": "",
"_objFlags": 0, "_objFlags": 0,
"node": { "node": {
"__id__": 7 "__id__": 10
}, },
"_enabled": true, "_enabled": true,
"_materials": [ "_materials": [
...@@ -444,7 +594,7 @@ ...@@ -444,7 +594,7 @@
"_name": "", "_name": "",
"_objFlags": 0, "_objFlags": 0,
"node": { "node": {
"__id__": 7 "__id__": 10
}, },
"_enabled": true, "_enabled": true,
"_type": 1, "_type": 1,
...@@ -465,44 +615,59 @@ ...@@ -465,44 +615,59 @@
"_id": "fc/er1Rl9D2oRl/jtSXWGX" "_id": "fc/er1Rl9D2oRl/jtSXWGX"
}, },
{ {
"__type__": "cc.MotorJoint", "__type__": "cc.CircleCollider",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 10
},
"_enabled": true,
"tag": 0,
"_offset": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_radius": 15,
"_id": "4a7L41jTlP4a0BkoZEqTAf"
},
{
"__type__": "cc.RevoluteJoint",
"_name": "", "_name": "",
"_objFlags": 0, "_objFlags": 0,
"node": { "node": {
"__id__": 7 "__id__": 10
}, },
"_enabled": true, "_enabled": true,
"anchor": { "anchor": {
"__type__": "cc.Vec2", "__type__": "cc.Vec2",
"x": 1, "x": 0,
"y": -31 "y": 0
}, },
"connectedAnchor": { "connectedAnchor": {
"__type__": "cc.Vec2", "__type__": "cc.Vec2",
"x": 38, "x": 0,
"y": 0 "y": 0
}, },
"connectedBody": { "connectedBody": {
"__id__": 11 "__id__": 15
}, },
"collideConnected": false, "collideConnected": false,
"_linearOffset": { "_maxMotorTorque": 100,
"__type__": "cc.Vec2", "_motorSpeed": 100,
"x": 30, "_enableLimit": false,
"y": 0 "_enableMotor": true,
}, "referenceAngle": 0,
"_angularOffset": 0, "lowerAngle": 0,
"_maxForce": 10, "upperAngle": 0,
"_maxTorque": 200, "_id": "9bft1/UTtJOqrFIUvJxer4"
"_correctionFactor": 0.3,
"_id": "40EQRA9TRE+rFRdgMnGLW8"
}, },
{ {
"__type__": "cc.RigidBody", "__type__": "cc.RigidBody",
"_name": "", "_name": "",
"_objFlags": 0, "_objFlags": 0,
"node": { "node": {
"__id__": 12 "__id__": 16
}, },
"_enabled": true, "_enabled": true,
"_type": 1, "_type": 1,
...@@ -533,13 +698,13 @@ ...@@ -533,13 +698,13 @@
"_active": true, "_active": true,
"_components": [ "_components": [
{ {
"__id__": 13 "__id__": 17
}, },
{ {
"__id__": 11 "__id__": 15
}, },
{ {
"__id__": 14 "__id__": 18
} }
], ],
"_prefab": null, "_prefab": null,
...@@ -565,8 +730,8 @@ ...@@ -565,8 +730,8 @@
"__type__": "TypedArray", "__type__": "TypedArray",
"ctor": "Float64Array", "ctor": "Float64Array",
"array": [ "array": [
-207.592, 47.682,
-92.126, 60.046,
0, 0,
0, 0,
0, 0,
...@@ -595,7 +760,7 @@ ...@@ -595,7 +760,7 @@
"_name": "", "_name": "",
"_objFlags": 0, "_objFlags": 0,
"node": { "node": {
"__id__": 12 "__id__": 16
}, },
"_enabled": true, "_enabled": true,
"_materials": [ "_materials": [
...@@ -623,69 +788,34 @@ ...@@ -623,69 +788,34 @@
"_id": "d899sMlPZKyrzwmjLmpXSy" "_id": "d899sMlPZKyrzwmjLmpXSy"
}, },
{ {
"__type__": "cc.PhysicsBoxCollider", "__type__": "cc.CircleCollider",
"_name": "", "_name": "",
"_objFlags": 0, "_objFlags": 0,
"node": { "node": {
"__id__": 12 "__id__": 16
}, },
"_enabled": true, "_enabled": true,
"tag": 0, "tag": 0,
"_density": 1,
"_sensor": false,
"_friction": 0.2,
"_restitution": 0,
"body": null,
"_offset": { "_offset": {
"__type__": "cc.Vec2", "__type__": "cc.Vec2",
"x": 0, "x": 0,
"y": 0 "y": 0
}, },
"_size": { "_radius": 15,
"__type__": "cc.Size", "_id": "42P+6j9N5LXYWkcyzM1PV3"
"width": 30,
"height": 30
},
"_id": "b1GoUkKj5DJZ6aJKJtsrcU"
},
{
"__type__": "cc.PhysicsBoxCollider",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 7
},
"_enabled": true,
"tag": 0,
"_density": 1,
"_sensor": false,
"_friction": 0.2,
"_restitution": 0,
"body": null,
"_offset": {
"__type__": "cc.Vec2",
"x": 0.6,
"y": 0
},
"_size": {
"__type__": "cc.Size",
"width": 30,
"height": 30
},
"_id": "5fqfCs7LBL+IbOXJB113Tk"
}, },
{ {
"__type__": "f8d7b/0hj9IhpmWwu6imtTe", "__type__": "9f879YGuoROFrLJrd9nEcLA",
"_name": "", "_name": "",
"_objFlags": 0, "_objFlags": 0,
"node": { "node": {
"__id__": 7 "__id__": 10
}, },
"_enabled": true, "_enabled": true,
"label": null, "label": null,
"target": null, "target": null,
"text": "hello", "text": "hello",
"_id": "d5YHYCIbNEwI7HelCAj6Oc" "_id": "aaTLhHzJRDvbZhDslxn9T5"
}, },
{ {
"__type__": "cc.Canvas", "__type__": "cc.Canvas",
...@@ -732,13 +862,13 @@ ...@@ -732,13 +862,13 @@
"_id": "29zXboiXFBKoIV4PQ2liTe" "_id": "29zXboiXFBKoIV4PQ2liTe"
}, },
{ {
"__type__": "f69d1tXlplDb5zzCXRfk+k+", "__type__": "1a6bcOcZghGVogtysviFCX0",
"_name": "", "_name": "",
"_objFlags": 0, "_objFlags": 0,
"node": { "node": {
"__id__": 2 "__id__": 2
}, },
"_enabled": true, "_enabled": true,
"_id": "e5BZ2xDIpGBKEVUwco0CRX" "_id": "53H/nqM0ZFU4TErvg9jH2/"
} }
] ]
\ No newline at end of file
/**
* Copyright (c) 2019 AnyGame. All rights reserved.
* 事件派发
* @class clientEvent
* @static
*/
export default class clientEvent {
private static handlerList = {};
/**
* 监听事件
* @param {string} eventName 事件名称
* @param {function} handler 监听函数
* @param {object} target 监听目标
* @method on
*/
static on(eventName, handler, target) {
if (typeof handler !== 'function') {
console.error(`[clientEvent] handler must be function when register ${eventName}`);
return;
}
if (!clientEvent.handlerList[eventName]) {
clientEvent.handlerList[eventName] = [];
}
const list = clientEvent.handlerList[eventName];
let alreadyRegister = false;
const { length } = list;
for (let i = 0; i < length; i ++) {
const handlerInList = list[i].handler;
const targetInList = list[i].target;
if (handler === handlerInList && target === targetInList) {
alreadyRegister = true;
console.warn(`[clientEvent] ${eventName} already register with same hanlder and target.`);
break;
}
}
if (!alreadyRegister) {
list.push({
handler,
target,
});
}
}
/**
* 取消监听
* @param {string} eventName 监听事件
* @param {function} handler 监听函数
* @param {object} target 监听目标
* @method off
*/
static off(eventName, handler, target) {
const list = clientEvent.handlerList[eventName];
if (list) {
for (let i = 0; i < list.length; i ++) {
const handlerInList = list[i].handler;
const targetInList = list[i].target;
if (handler === handlerInList && target === targetInList) {
list.splice(i, 1);
break;
}
}
}
}
/**
* 分发事件
* @param {string} eventName 分发事件名
* @param {...any} params 分发事件参数
* @method dispatchEvent
*/
static dispatchEvent(eventName, ...params) {
const list = clientEvent.handlerList[eventName];
if (!list || list.length === 0) {
console.warn(`[clientEvent] please retister ${eventName} first.`);
return;
}
for (const data of list) {
const { handler, target } = data;
handler.apply(target, params);
}
}
};
\ No newline at end of file
{
"ver": "1.0.8",
"uuid": "ac308f04-56c0-45ce-8d27-0bf6e70c7c3c",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
// Learn life-cycle callbacks: // Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
import clientEvent from "./clientEvent";
const {ccclass, property} = cc._decorator; const {ccclass, property} = cc._decorator;
@ccclass @ccclass
...@@ -13,19 +15,44 @@ export default class NewClass extends cc.Component { ...@@ -13,19 +15,44 @@ export default class NewClass extends cc.Component {
@property(cc.Label) @property(cc.Label)
label: cc.Label = null; label: cc.Label = null;
@property @property(cc.Node)
target: cc.Node = null; target: cc.Node = null;
@property @property
text: string = 'hello'; text: string = 'hello';
rigidBody: cc.RigidBody; rigidBody: cc.RigidBody;
motor: cc.MotorJoint; motor: cc.MotorJoint;
collider: cc.CircleCollider;
// LIFE-CYCLE CALLBACKS: // LIFE-CYCLE CALLBACKS:
onLoad () { onLoad () {
this.rigidBody = this.node.getComponent(cc.RigidBody); this.rigidBody = this.node.getComponent(cc.RigidBody);
this.motor = this.node.getComponent(cc.MotorJoint); this.motor = this.node.getComponent(cc.MotorJoint);
this.motor.connectedBody // this.motor.connectedBody
this.collider =this.node.getComponent(cc.CircleCollider);
const collisionManager = cc.director.getCollisionManager();
collisionManager.enabled = true;
}
onEnable() {
clientEvent.on('move', this.move, this);
}
move(dir) {
console.error('move');
this.node.position = dir;
}
onCollsionEnter(other, self) {
console.error('on collision enter');
}
onCollisionStay(other, self) {
console.error('on collision stay');
}
onCollisionExit(other, self) {
console.error('on collision exit');
} }
start () { start () {
......
// Learn TypeScript:
// - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
import clientEvent from "./clientEvent";
const {ccclass, property} = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
@property(cc.Label)
label: cc.Label = null;
@property
text: string = 'hello';
startPoint: cc.Vec2;
points:Array<cc.Vec2> = new Array();
// LIFE-CYCLE CALLBACKS:
onLoad () {
this.initEvent();
}
initEvent() {
this.node.on(cc.Node.EventType.TOUCH_START, this._touchStartEvent, this);
this.node.on(cc.Node.EventType.TOUCH_MOVE, this._touchMoveEvent, this);
this.node.on(cc.Node.EventType.TOUCH_END, this._touchEndEvent, this);
}
_touchStartEvent(event: cc.Event.EventTouch) {
this.startPoint = event.getStartLocation();
}
_touchMoveEvent(event: cc.Event.EventTouch) {
this.points.push(event.getLocation());
if (this.points.length > 20) {
this.points.shift();
}
const averagePoint = new cc.Vec2(0, 0);
this.points.forEach((point) => {
averagePoint.x += point.x;
averagePoint.y += point.y;
})
averagePoint.multiplyScalar(1 / this.points.length);
const delta = new cc.Vec2(event.getLocation().x - averagePoint.x, event.getLocation().y - averagePoint.y);
let dir = delta;
// let dir = event.getLocation();
clientEvent.dispatchEvent('move', dir);
}
_touchEndEvent(event: cc.Event.EventTouch) {
this.points = [];
}
start () {
}
// update (dt) {}
}
{
"ver": "1.0.8",
"uuid": "3b6caaee-48b3-409e-9f23-fe9357396a2e",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
...@@ -20,6 +20,7 @@ export default class NewClass extends cc.Component { ...@@ -20,6 +20,7 @@ export default class NewClass extends cc.Component {
onLoad () { onLoad () {
cc.director.getPhysicsManager().enabled = true; cc.director.getPhysicsManager().enabled = true;
// cc.director.getPhysicsManager().gravity = cc.v2(0,0);
} }
start () { start () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment