Cocos Creator 2.4.x创建3D场景并控制角色移动的基本示例代码

以下是使用Cocos Creator 2.4.x创建3D场景并控制角色移动的基本示例代码:

```typescript
const {cc, Vec3} = window;

cc.Class({
    extends: cc.Component,

    properties: {
        // 场景中的相机
        camera: cc.Camera,
        // 角色节点
        player: cc.Node,
        // 角色移动速度
        speed: 100,
    },

    start() {
        // 注册键盘事件
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },

    onDestroy() {
        // 取消键盘事件的注册
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    },

    onKeyDown(event) {
        const keyCode = event.keyCode;
        switch (keyCode) {
            case cc.macro.KEY.w:
                // 按下‘w’键,向前移动
                this.move(new Vec3(0, 0, -1));
                break;
            case cc.macro.KEY.s:
                // 按下‘s’键,向后移动
                this.move(new Vec3(0, 0, 1));
                break;
            case cc.macro.KEY.a:
                // 按下‘a’键,向左移动
                this.move(new Vec3(-1, 0, 0));
                break;
            case cc.macro.KEY.d:
                // 按下‘d’键,向右移动
                this.move(new Vec3(1, 0, 0));
                break;
            default:
                break;
        }
    },

    onKeyUp(event) {
        const keyCode = event.keyCode;
        switch (keyCode) {
            case cc.macro.KEY.w:
            case cc.macro.KEY.s:
            case cc.macro.KEY.a:
            case cc.macro.KEY.d:
                // 松开方向键时,停止移动
                this.move(new Vec3(0, 0, 0));
                break;
            default:
                break;
        }
    },

    move(dir) {
        // 角色节点移动
        const position = this.player.getPosition();
        const delta = new Vec3(dir.x * this.speed, 0, dir.z * this.speed);
        Vec3.add(position, delta, position);
        this.player.setPosition(position);

        // 将相机跟随角色节点移动
        const cameraPos = this.camera.node.getPosition();
        Vec3.add(position, new Vec3(0, 50, -100), cameraPos);
        this.camera.node.setPosition(cameraPos);
        this.camera.lookAt(position);
    }
});
```
在上述示例代码中,我们创建了一个包含场景中相机和角色节点的组件,并监听键盘事件来移动角色节点,同时将相机跟随角色节点移动。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个基于 cocoscreator 2.4.x 的发光 shader。以下是代码: ``` CCEffect %{ techniques: - name: glow passes: - vert: ccShader_PositionTextureColor_vert frag: glow_frag } %} CCProgram glow { CCShader_PositionTextureColor_vert, highp vec4 blurSize; void main() { mat4 mvp; CC_USE_MVP(mvp); gl_Position = mvp * vec4(a_position, 1); v_fragmentColor = a_color; v_texCoord = a_texCoord; } CCShader_Header { uniform highp vec4 blurSize; varying lowp vec4 v_fragmentColor; varying mediump vec2 v_texCoord; const highp float glowPow = 2.0; const highp float glowStrength = 0.5; } CCShader glow_frag { highp vec4 blurSize; highp float blurRadius = 2.0; void main() { highp vec4 sum = vec4(0.0); highp vec2 texcoord = v_texCoord.xy; highp vec2 blur = blurSize.xy; highp float strength = blurSize.z; highp float glow = blurSize.w; highp float samples = 8.0; highp vec4 col = texture2D(CC_Texture0, texcoord); for (highp float i = 0.0; i < samples; i++) { highp float angle = i / samples * 3.14159265 * 2.0; highp vec2 blurDirection = vec2(sin(angle), cos(angle)); highp vec4 blurCol = texture2D(CC_Texture0, texcoord + blurDirection * blurRadius * blur); sum += blurCol; } highp vec4 glowCol = sum / samples; glowCol = (glowCol - col) * glow * glowPow + col; gl_FragColor = mix(col, glowCol, strength) * v_fragmentColor; } } } ``` 您可以将此代码添加到您的 cocoscreator 2.4.x 项目中,然后将此 shader 应用于需要发光效果的 sprite 上。希望这可以帮到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值