关注 码龄 粉丝数 原力等级 -- 被采纳 被点赞 采纳率 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条回答 默认 最新
-
让阿豪来帮你解答,本回答参考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’ ” 的错误提示了。
本回答被题主选为最佳回答 , 对您是否有帮助呢? 本回答被专家选为最佳回答 , 对您是否有帮助呢? 本回答被题主和专家选为最佳回答 , 对您是否有帮助呢?
解决
无用
评论
打赏
微信扫一扫 点击复制链接
分享 举报
关注
关注
该回答引用自GPT-3.5,由博主GISer Liu编写:
根据你描述的问题,报错信息提示类型‘Gamemanger’上不存在属性‘onMouseUp’,这是因为在GameManger类中定义了setInputActive方法,并在该方法中尝试监听MOUSE_UP事件,但GameManger类本身并没有定义onMouseUp方法,导致报错。
要解决这个问题,你可以在GameManger类中定义一个onMouseUp方法,用于处理鼠标弹起事件。具体步骤如下:
- 在
GameManger类中添加onMouseUp方法,处理鼠标弹起事件。
typescriptCopy codeonMouseUp(event: EventMouse){
// 在这里处理鼠标弹起事件
}
- 修改
setInputActive方法,使用onMouseUp方法作为参数传递给input.on和input.off方法。
typescriptCopy codesetInputActive(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)时,会取消对鼠标弹起事件的监听。
请注意,以上是根据你描述的问题提供的解决思路。如果还有其他问题或需要进一步解释,请随时提出。
如果该回答解决了您的问题,请采纳!如果没有,请私信联系或评论您的疑惑
本回答被题主选为最佳回答 , 对您是否有帮助呢? 本回答被专家选为最佳回答 , 对您是否有帮助呢? 本回答被题主和专家选为最佳回答 , 对您是否有帮助呢?
解决
无用
评论
打赏
微信扫一扫 点击复制链接
分享 举报