Cocos Creator 3.x 实现触摸拖动物体(record)

参考:如何实现拖动物体 - Creator 3.x - Cocos中文社区

//注册触摸事件
node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
//事件回调函数
onTouchMove(event) {
        const location = event.getUILocation();
        event.target.setWorldPosition(location.x, location.y, 0);
}

关键:坐标转换。

另外:getWorldPosition() 方法返回的是节点相对于世界坐标系的位置,即节点的中心点相对于世界坐标系的位置。这意味着它返回的是节点在屏幕上的实际位置,而不是节点的左下角或其他特定点的位置。

CCEffect %{ techniques: - passes: - vert: vs frag: fs properties: &props mainTexture: { value: &#39;white&#39; } // 修改为mainTexture outlineColor: { value: [1,0,0,1], editor: { type: &#39;color&#39; } } outlineWidth: { value: 0.02, editor: { type: &#39;slider&#39;, min: 0, max: 0.1 } } }% CCProgram vs %{ precision highp float; #include <cc-global> #include <cc-local> in vec3 a_position; in vec2 a_texCoord; in vec4 a_color; out vec4 v_color; out vec2 v_uv0; void main() { vec4 pos = vec4(a_position, 1.0); pos = cc_matWorld * pos; gl_Position = cc_matProj * cc_matView * pos; v_uv0 = a_texCoord; v_color = a_color; } }% CCProgram fs %{ precision highp float; uniform sampler2D mainTexture; // 修改为mainTexture uniform Constant { vec4 outlineColor; float outlineWidth; }; in vec4 v_color; in vec2 v_uv0; void main() { vec4 texColor = texture2D(mainTexture, v_uv0); // 修改为mainTexture float alpha = texColor.a * v_color.a; // 边缘检测 float maxAlpha = 0.0; for(int x = -1; x <= 1; ++x) { for(int y = -1; y <= 1; ++y) { vec2 offset = vec2(x,y) * outlineWidth; maxAlpha = max(maxAlpha, texture2D(mainTexture, v_uv0 + offset).a); // 修改为mainTexture } } // 颜色混合 vec4 finalColor = mix(outlineColor, v_color * texColor, alpha); finalColor.a = max(outlineColor.a * (maxAlpha - alpha), alpha); gl_FragColor = finalColor; } }% 以上在cocoscreator3.8.2中创建的spine描边.effect报错 1. [Assets] ../bundle/gui/shaders/spine-outline.effect - syntax: Error EFX1001: CCEffect parser failed: YAMLException: bad indentation of a mapping entry (7:43) 4 | ... t: vs 5 | ... g: fs 6 | ... perties: &props 7 | ... ainTexture: { value: &#39;white&#39; } // 修改为mainTexture -----------------------------------------^ 8 | ... utlineColor: { value: [1,0,0,1], editor: { type: &#39;color&#39; } } 9 | ... utlineWidth: { value: 0.02, editor: { type: &#39;slider&#39;, min: 0, m ... [Assets] ../bundle/gui/shaders/spine-outline.effect - syntax: Error EFX1001: CCEffect parser failed: YAMLException: bad indentation of a mapping entry (7:43) -----------------------------------------^ -----------------------------------------^ at Object.encode (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules\@base\electron-worker\node_modules\v-stacks\index.js:14:17) at Logger._logHandler (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules\@base\electron-worker\static\script.ccc:1:531) at Logger.record (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules\@base\electron-logger\lib\renderer.ccc:1:458) at Object.error (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules\@base\electron-logger\lib\renderer.ccc:1:1414) at Object.error (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\builtin\asset-db\dist\worker\console.ccc:1:1429) at EffectImporter.import (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\modules\engine-extensions\extensions\engine-extends\dist\importer\importers\effect.ccc:1:3086) at runMicrotasks (<anonymous>) at runNextTicks (internal/process/task_queues.js:58:5) at processImmediate (internal/timers.js:434:9) 2. [Window] The "path" argument must be of type string or an instance of Buffer or URL. Received undefined TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined at Object.openSync (fs.js:468:10) at Object.func [as openSync] (electron/js2c/asar_bundle.js:5:1846) at readFileSync (fs.js:377:35) at e.readFileSync (electron/js2c/asar_bundle.js:5:8872) at Object.refresh (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\resources\3d\engine\editor\inspector\assets\effect.js:313:39) at Object.<anonymous> (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\resources\3d\engine\editor\inspector\assets\effect.js:415:26) at HTMLElement.update (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\app.asar\node_modules\@editor\creator-ui-kit\dist\renderer.ccc:2:1642565) at C:\ProgramData\cocos\editors\Creator\3.8.2\resources\resources\3d\engine\editor\inspector\contributions\asset.js:358:43 at Array.map (<anonymous>) at Object.update (C:\ProgramData\cocos\editors\Creator\3.8.2\resources\resources\3d\engine\editor\inspector\contributions\asset.js:357:50)
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值