• 类型‘Gamemanger’上不存在属性‘onMouseUp’报错,跟官方学cocos,求解


    关注 码龄 粉丝数 原力等级 -- 被采纳 被点赞 采纳率 qq_34311636 2024-04-01 19:00 采纳率: 0% 浏览 1 首页/ 游戏 / 类型‘Gamemanger’上不存在属性‘onMouseUp’报错,跟官方学cocos,求解 cocos2d 跟着cocos的官方教程学做2D游戏,函数setInputActive里报错提示:类型‘Gamemanger’上不存在属性‘onMouseUp’ setInputActive(active: boolean){ if (active) { input.on(Input.EventType.MOUSE_UP,this.onMouseUp,this); }else{ input.off(Input.EventType.MOUSE_UP,this.onMouseUp,this); } } 完整代码如下Gamemanger.ts import { _decorator, CCInteger, Component, Input, input, instantiate, Label, math, Node, Prefab, Vec3} from 'cc'; import { BLOCK_SIZE,PlayerController} from './PlayerController'; const { ccclass, property } = _decorator; enum BlockType{ BT_NONE, BT_STONE, }; enum GameState{ GS_INIT, GS_PLAYING, GS_END, } @ccclass('GameManger') export class GameManger extends Component { @property({type:Prefab}) public boxPrefab: Prefab|null = null; @property({type:CCInteger}) public roadLength: number = 50; private _road: BlockType[] = []; @property({type:Node}) public startMenu: Node | null = null;//开始的UI @property({type:PlayerController}) public playerCtrl: PlayerController | null = null;//角色控制 @property({type:Label}) public stepsLabel:Label|null = null;//计步器 start() { this.SetCurState(GameState.GS_INIT); //第一初始化要在start里面调用 } setInputActive(active: boolean){ if (active) { input.on(Input.EventType.MOUSE_UP,this.onMouseUp,this); }else{ input.off(Input.EventType.MOUSE_UP,this.onMouseUp,this); } } init(){ if (this.startMenu) { this.startMenu.active = true; } this.generateRoad(); if (this.playerCtrl) { this.playerCtrl.setInputActive(false) this.playerCtrl.node.setPosition(Vec3.ZERO); this.playerCtrl.reset(); } } playing(){ //自己挂载的位置和函数命名,需要重新确定一下 if (this.startMenu) { this.startMenu.active = false; } if (this.stepsLabel) {//将步数重置为0 this.stepsLabel.string = '0'; } setTimeout(() => { //直接设置active会直接开始监听鼠标事件,做了一下延迟处理 if (this.playerCtrl) { this.playerCtrl.setInputActive(true); } },0.1 ) ; } SetCurState(value: GameState){ switch (value) { case GameState.GS_INIT: this.init(); break; case GameState.GS_PLAYING: break; case GameState.GS_END: break; } } onStartButtonClicked(){ this.SetCurState(GameState.GS_PLAYING); } generateRoad() { this.node.removeAllChildren(); this._road = []; // startPos this._road.push(BlockType.BT_STONE); for (let i = 1; i < this.roadLength; i++) { if (this._road[i - 1] === BlockType.BT_NONE) { this._road.push(BlockType.BT_STONE); } else { this._road.push(Math.floor(Math.random() * 2)); } } for (let j = 0; j < this._road.length; j++) { let block: Node | null = this.spawnBlockByType(this._road[j]); if (block) { this.node.addChild(block); block.setPosition(j * BLOCK_SIZE, 0, 0); } } } spawnBlockByType(type: BlockType) { if (!this.boxPrefab) { return null; } let block: Node|null = null; switch(type) { case BlockType.BT_STONE: block = instantiate(this.boxPrefab); break; } return block; } update(deltaTime: number){ } } PlayerController.ts import { _decorator, Animation, CCClass, Component, Event, EventMouse, Input, input, Node, Vec3 } from 'cc'; const { ccclass, property } = _decorator; export const BLOCK_SIZE = 40;//添加一个放大比 @ccclass('PlayerController') export class PlayerController extends Component { @property(Animation) BodyAnim:Animation = null; setInputActive: any; start() { input.on(Input.EventType.MOUSE_UP,this.onMouseUp,this); } reset(){ } onMouseUp(event:EventMouse){ if (event.getButton() === 0) { this.jumpByStep(1); }else if (event.getButton() ===2) { this.jumpByStep(2); } } private _startJump: boolean = false; private _jumpStep: number = 0; private _curJumpTime: number = 0; private _jumpTime: number = 0.1; private _curJumpSpeed: number = 0; private _curPos: Vec3 = new Vec3(); private _deltaPos: Vec3 = new Vec3(0, 0, 0); private _targetPos: Vec3 = new Vec3(); jumpByStep(step:number){ if (this._startJump) { return; } this._startJump = true; //标记开始跳跃 this._jumpStep = step; //跳跃的步数1或者2 this._curJumpTime = 0;//重置开始的时间 const clipName = step == 1 ? 'oneStep' : 'twoStep'; const state = this.BodyAnim.getState(clipName) this._jumpTime = state.duration; this._curJumpSpeed = this._jumpStep * BLOCK_SIZE / this._jumpTime;//根据时间计算出速度 this.node.getPosition(this._curPos);//获取角色当前的位置 Vec3.add(this._targetPos,this._curPos,new Vec3(this._jumpStep * BLOCK_SIZE, 0, 0));//计算出目标位置 if (this.BodyAnim) { if (step === 1) { this.BodyAnim.play('oneStep'); }else if (step ===2) { this.BodyAnim.play('twoStep'); } } } update(deltaTime: number) { if (this._startJump) { this._curJumpTime += deltaTime; //累计总的跳跃时间 if (this._curJumpTime > this._jumpTime) {//当跳跃时间是否结束 // end this.node.setPosition(this._targetPos);//强制位置的终点 this._startJump = false;//清理跳跃标记 }else{ //tween this.node.getPosition(this._curPos); this._deltaPos.x = this._curJumpSpeed * deltaTime; Vec3.add(this._curPos, this._curPos, this._deltaPos); this.node.setPosition(this._curPos); } } } } 展开全部 收起 写回答 好问题 0 提建议 追加酬金 关注问题 微信扫一扫 点击复制链接 分享 邀请回答 编辑 收藏 删除 结题 收藏 举报 追加酬金 (90%的用户在追加酬金后获得了解决方案) 当前问题酬金 ¥ 0 (可追加 ¥500) ¥ 15¥ 20¥ 50¥ 100¥ 200 支付方式 扫码支付 二维码出错 点击刷新 支付金额 15 元 提供问题酬金的用户不参与问题酬金结算和分配 支付即为同意 《付费问题酬金结算规则》 结题 再想想 删除 再等等

    3条回答 默认 最新

    • 专家-赤兔[在线] 全栈领域优质创作者 2024-04-01 19:00
      关注

      引自免费微信小程序:皆我百晓生

      好的,我会尽力为你提供详细的解答。

      在Cocos2D中,有一个名为input的类,它包含了所有的输入事件处理逻辑。其中最重要的事件类型是InputEventType,表示用户对游戏界面的交互。当我们在input.on()方法中处理输入事件时,可以通过传递InputEventType的对象来判断输入的具体类型。

      如果你想要解决你在 gamemanger.ts文件中的问题,你需要找到对应的input.on()方法,并确保其参数符合InputEventType的类型。在这个例子中,你需要在input.on()方法中传递一个EventMouse对象,这个对象包含了一组与用户鼠标交互相关的事件。

      此外, gamemanger.ts文件中的onMouseUp()方法也是一个重要的事件处理方法,这个方法会在用户释放鼠标点击时被调用。为了处理这个事件,你需要创建一个新的变量_startJump来记录开始跳跃的状态,然后在onMouseUp()方法中更新_startJump的值来控制跳跃。

      PlayerController.ts文件中,你需要找到onMouseUp()方法,并在其中添加一些判断语句来判断用户的鼠标点击行为是否满足特定的需求。例如,如果你想要让角色在每次跳跃后立即改变方向,那么你需要修改_startJump的初始值为false

      以上就是针对你的问题的解决方案,希望对你有所帮助。如果有其他问题,欢迎随时向我提问。

      评论 编辑记录
      qq_34311636 2024-04-01 19:20

      第三份代码PlayerController.ts里面已经添加了onMouseUp方法,能让Gamemanger.ts去取到里面的方法吗?

      回复
    • 码农阿豪 Java领域优质创作者 2024-04-01 19:00
      关注
      让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
      问题分析: Gamemanger 类里面的 setInputActive 函数中使用了 this.onMouseUp,但是在 Gamemanger 类中并没有声明一个名为 onMouseUp 的属性或函数。 解决方案: 需要在 Gamemanger 类中声明一个名为 onMouseUp 的函数,并在需要的地方使用它。例如,在 Gamemanger 类中添加以下代码实现 onMouseUp 函数: onMouseUp(event: EventMouse) { // TODO: 处理 onMouseUp 事件 } 修改后的 Gamemanger 类的代码如下(onMouseUp 函数是新增的部分): import { _decorator, CCInteger, Component, EventMouse, Input, input, instantiate, Label, math, Node, Prefab, Vec3 } from 'cc'; const { ccclass, property } = _decorator; enum BlockType { BT_NONE, BT_STONE, }; enum GameState { GS_INIT, GS_PLAYING, GS_END, } @ccclass('GameManger') export class GameManger extends Component { @property({ type: Prefab }) public boxPrefab: Prefab | null = null; @property({ type: CCInteger }) public roadLength: number = 50; private _road: BlockType[][] = []; @property({ type: Node }) public startMenu: Node | null = null; // 开始的UI @property({ type: PlayerController }) public playerCtrl: PlayerController | null = null; // 角色控制 @property({ type: Label }) public stepsLabel: Label | null = null; // 计步器 start() {
      this.SetCurState(GameState.GS_INIT); // 第一初始化要在 start 里面调用
      

      } setInputActive(active: boolean) { if (active) { input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this); } else { input.off(Input.EventType.MOUSE_UP, this.onMouseUp, this); } } onMouseUp(event: EventMouse) { // TODO: 处理 onMouseUp 事件 } init() { if (this.startMenu) { this.startMenu.active = true; } this.generateRoad(); if (this.playerCtrl) { this.playerCtrl.setInputActive(false); this.playerCtrl.node.setPosition(Vec3.ZERO); this.playerCtrl.reset(); } } playing() { // 自己挂载的位置和函数命名,需要重新确定一下 if (this.startMenu) { this.startMenu.active = false; } if (this.stepsLabel) { // 将步数重置为0 this.stepsLabel.string = '0'; } setTimeout(() => { // 直接设置 active 会直接开始监听鼠标事件,做了一下延迟处理 if (this.playerCtrl) { this.playerCtrl.setInputActive(true); } }, 0.1); } SetCurState(value: GameState) { switch (value) { case GameState.GS_INIT: this.init(); break; case GameState.GS_PLAYING: break; case GameState.GS_END: break; } } onStartButtonClicked() { this.SetCurState(GameState.GS_PLAYING); } spawnBlockByType(type: BlockType) { if (this.boxPrefab) { const block = instantiate(this.boxPrefab); switch (type) { case BlockType.BT_NONE: break; case BlockType.BT_STONE: block.color = math.randomRangeInt(0, 1) ? math.Color3.RED : math.Color3.BLUE; break; } return block; } return null; } generateRoad() { this.node.removeAllChildren(); this._road = []; // startPos this._road.push([BlockType.BT_STONE]); for (let i = 1; i < this.roadLength; i++) { if (this._road[i - 1][0] === BlockType.BT_NONE) { this._road.push([BlockType.BT_STONE]); } else { this._road.push([Math.floor(Math.random() * 2)]); } } for (let j = 0; j < this._road.length; j++) { let block: Node | null = this.spawnBlockByType(this._road[j][0]); if (block) { this.node.addChild(block); block.setPosition(j * BLOCK_SIZE, 0, 0); } } } } 这样,在使用 setInputActive 函数时就不会再出现 “类型‘Gamemanger’上不存在属性‘onMouseUp’ ” 的错误提示了。

      展开全部

      评论
      qq_34311636 2024-04-01 19:25

      第三份代码PlayerController.ts里面已经添加了onMouseUp方法,能让Gamemanger.ts去取到里面的方法吗?

      回复
    • GISer Liu 2024-04-01 19:04
      关注

      该回答引用自GPT-3.5,由博主GISer Liu编写:

      根据你描述的问题,报错信息提示类型‘Gamemanger’上不存在属性‘onMouseUp’,这是因为在GameManger类中定义了setInputActive方法,并在该方法中尝试监听MOUSE_UP事件,但GameManger类本身并没有定义onMouseUp方法,导致报错。
      要解决这个问题,你可以在GameManger类中定义一个onMouseUp方法,用于处理鼠标弹起事件。具体步骤如下:

      1. GameManger类中添加onMouseUp方法,处理鼠标弹起事件。
        typescriptCopy code
        onMouseUp(event: EventMouse){
        // 在这里处理鼠标弹起事件
        }
        
      2. 修改setInputActive方法,使用onMouseUp方法作为参数传递给input.oninput.off方法。
        typescriptCopy code
        setInputActive(active: boolean){
        if (active) {
        input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
        } else {
        input.off(Input.EventType.MOUSE_UP, this.onMouseUp, this);
        }
        }
        
        这样修改后,当你调用setInputActive(true)时,游戏管理器会监听鼠标弹起事件,并调用onMouseUp方法处理事件;当调用setInputActive(false)时,会取消对鼠标弹起事件的监听。
        请注意,以上是根据你描述的问题提供的解决思路。如果还有其他问题或需要进一步解释,请随时提出。

      如果该回答解决了您的问题,请采纳!如果没有,请私信联系或评论您的疑惑

      评论
      qq_34311636 2024-04-01 19:20

      第三份代码PlayerController.ts里面已经添加了onMouseUp方法,能让Gamemanger.ts去取到里面的方法吗?

      回复
    编辑
    预览

    报告相同问题?

  • 相关阅读:
    怎么做好web服务器安全措施
    从Mybatis-Plus开始认识SerializedLambda
    【无标题】
    十大经典排序算法之选择排序。
    迅为i.MX8Mmini开发板离线构建Yocto系统
    WEB安全基础 - - -文件上传
    2022-11-18 mysql-filesort-分析
    使用爬虫批量下载图片链接并去重
    【Python】关于自定义对象的Json序列化和反序列化
    I2C协议
  • 原文地址:https://ask.csdn.net/questions/8082404