cocos-creator / cocos-tutorial-first-game Goto Github PK
View Code? Open in Web Editor NEWQuick start game for Fireball
Quick start game for Fireball
因为Player.ts下的getCenterPos返回的是只有x和y的cc.v2
getCenterPos () {
//var centerPos = cc.v2(this.node.x, this.node.y + this.node.height/2);
//必须改成下面带有z坐标的v3,上面注释掉的v2不行
var centerPos = cc.v3(this.node.x, this.node.y + this.node.height/2, 0);
return centerPos;
}
从而导致如下Star.ts里getPlayerDistance 计算距离dist的结果为NaN了
getPlayerDistance () {
// 根据 player 节点位置判断距离
var playerPos = this.game.player.getCenterPos();
// 根据两点位置计算两点之间距离
var dist = this.node.position.sub(playerPos).mag();
return dist;
}
debug看了下是因为cc.Node的getPosition返回的是Vec3而不是Vec2,所以this.node.position.sub(playerPos).mag()结果就是NaN了
版本2.3.3。
新版CCC在运行时提示Action
即将被废弃,推荐使用tween
来做缓动动画,于是用tween
重写了缓动部分 (TypeScript代码):
cc.tween(this.node)
.repeatForever(
cc.tween(this.node)
.by(this.jumpDuration, { position: cc.v2(0, this.jumpHeight) }, { easing: 'cubicOut' })
.by(this.jumpDuration, { position: cc.v2(0, -this.jumpHeight) }, { easing: 'cubicIn' })
.call(() => this.playJumpSound())
)
.start();
但是在运行时发现update()
函数里更新node.x
的代码不能正常工作:
update(dt: number) {
// ...
// 上面是计算 this.xSpeed 的代码
this.node.x += this.xSpeed * dt;
}
打印this.node.x
发现 x
的值没有正常累加,感觉是 tween
的位移计算重置了 this.node.x
的值?打印出来的值大概是这样( x1 = 本帧修改前的this.node.x,v = xSpeed * dt,x2 = x1 + v ):
...
x1: 0, v: 6.667600000000676, x2: 6.667600000000676
x1: 0, v: 6.653600000000005, x2: 6.653600000000005
x1: 0, v: 6.687199999999575, x2: 6.687199999999575
...
请问为什么会这样?用tween
的时候如何正确更新node
的位置?
报出来cc.tween is not a function.
是版本你太低?
Hi Everyone,
The following code is not working
// Initialize the keyboard input listening
cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this); ..
when I run it on Simulator I get an error on that ...
Simulator: E/jswrapper (268): ERROR: Uncaught TypeError: Cannot read property 'a' of undefined, location: assets/scripts/Player.js:0:0
Simulator: jsb: ERROR: File ..\manual\jsb_cocos2dx_manual.cpp: Line: 653, Function: invokeJSKeyboardCallback
Simulator: invokeJSKeyboardCallback call function failed!
I have no clue on how to fix this ...any advice will be greatly appreciated.
thank you!
my cocos crater version is 1.9.3
and message shows eventManager will be removed. but when i try EventTarget i get an error again. can you update tutorial accordingly.
CCDebugger.js:343 The 'cc.eventManager' will be removed in v2.0, please use 'cc.EventTarget or cc.systemEvent' instead.
原文:
`setJumpAction: function () {
// 跳跃上升
var jumpUp = cc.moveBy(this.jumpDuration, cc.p(0, this.jumpHeight)).easing(cc.easeCubicActionOut());
// 下落
var jumpDown = cc.moveBy(this.jumpDuration, cc.p(0, -this.jumpHeight)).easing(cc.easeCubicActionIn());
// 不断重复
return cc.repeatForever(cc.sequence(jumpUp, jumpDown));
},
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Player.js
onLoad: function () {
// 初始化跳跃动作
this.jumpAction = this.setJumpAction();
this.node.runAction(this.jumpAction);
},
`
我的环境:Win10 Pro 64bit ,中文,VS2017 ,Cocos Creator 1.5
错误:经实践只能用.easing(cc.easyIn()/或cc.easyInOut()之类/),直接用tutorial的easeCubicActionOut()会Invalid key
疑问: 文章前面并只有setJumpAction,onLoad里却冒出了this.jumpAction ,不知道目的是什么??
Uncaught TypeError: Cannot read property 'jumpHeight' of null
这是游戏运行报的错误,this.player.getComponent('Player').jumpHeight 这个获取不到这个属性
但是我输出this.player 可以得到这个组件
//
游戏开始时得分从0开始,每收集一个星星分数就会加1。要显示得分,首先要创建一个 Label 节点。在层级管理器中选中Canvas节点,右键点击并选择菜单中的创建新节点->创建渲染节点->Label(文字),一个新的 Label 节点会被创建在Canvas下面,而且顺序在最下面。接下来我们要用如下的步骤配置这个 Label 节点:
将该节点名字改为score
将score节点的位置(position属性)设为(0, 180)。
选中该节点,编辑Label组件的string属性,填入Score: 0的文字。
将Label组件的Font Size属性设为50。
从资源管理器中拖拽assets/mikado_outline_shadow位图字体资源(注意图标是bmfont)到Label组件的Font属性中,将文字的字体替换成我们项目资源中的位图字体。
//
完全按照上述步骤, 在资源管理器里面拖动"mikado_outline_shadow" BF 资源到Canvas下的label里的Font属性时候,提示 TypeError: Cannot read property '_fntConfig' of undefined
之后字体没有在预览里显示出来,运行之后显示也不正常.
//
现在要添加主角收集星星的行为逻辑了,这里的重点在于,星星要随时可以获得主角节点的位置,才能判断他们之间的距离是否小于可收集距离,如何获得主角节点的引用呢?别忘了我们前面做过的两件事:
这句话,原本Star就被Game引用了,现在又把Game赋值给Star.这种设计思路不太好会不会?循环引用容易出现内存泄漏问题.最好就是把player直接赋值给Star,因为player和Star是并行关系.
按照我的思路,拾取到星星后的处理应该定义在Game里面,由Game去生成新的星星,摧毁老的星星,增加积分.这个功能可以在Star类里声明一个回调函数,在Game里面设置回调函数的具体工作.
//Game.js
spawnNewStar: function() {
// 使用给定的模板在场景中生成一个新节点
var newStar = cc.instantiate(this.starPrefab);
var star = newStar.getComponent('Star');
var self = this;
star.player = this.player;
star.pickedCallBackFunc = (star) => {
star.node.destroy();
self.spawnNewStar();
self.gainScore();
};
// 重置计时器,根据消失时间范围随机取一个值
star.duration = this.starDuration = this.minStarDuration + cc.random0To1() * (this.maxStarDuration - this.minStarDuration);
this.timer = 0;
// 将新增的节点添加到 Canvas 节点下面
this.node.addChild(newStar);
// 为星星设置一个随机位置
newStar.setPosition(this.getNewStarPosition());
},
Hi,朋友
setInputControl: function () { var self = this; // 添加键盘事件监听 cc.eventManager.addListener({ event: cc.EventListener.KEYBOARD, // 有按键按下时,判断是否是我们指定的方向控制键,并设置向对应方向加速 onKeyPressed: function(keyCode, event) { switch(keyCode) { case cc.KEY.a: self.accLeft = true; self.accRight = false; break; case cc.KEY.d: self.accLeft = false; self.accRight = true; break; } }, // 松开按键时,停止向该方向的加速 onKeyReleased: function(keyCode, event) { switch(keyCode) { case cc.KEY.a: self.accLeft = false; break; case cc.KEY.d: self.accRight = false; break; } } }, self.node); },
上个方法中为什么要在开头保存self,我试过不保存直接用this却得不到想要的结果,没有理解为什么?
cc.NodePool()与cc.NodePool('star')有啥区别,还有get(this)跟直接get有什么不一样的么,网上搜也没有搜到相关资料
我按照教程上所写一步步做到最后总结。运行后发现在游戏失败重置后Player第一次落地时整个游戏会卡住。
请问,我猜想是callback调用的问题,但是我不知道如何调试,有何方法调试呢?(同时在写教程代码的同时因为不少的拼写错误导致游戏运行出错也不知道如何调试)
如题,配置和publish_project_es6的配置一模一样的,为什么就运行不了,提示错误: Can not declare Star.onLoad, it is already defined in the prototype of Star
和 Please specifiy a default value for "onLoad" property at its declaration: 😂
i tried to recreate from zero the polished project, but some how when i call this.player.startMoveAt function, it did not call the Player:update function. so Player cannot move when i tap on left/right key. anyone has the same issue?
Hi Guys,
I have followed quick start doc and tried to prepare demo app. After jumbing char when i added star via Prefab but star is not showing in my demo app.
Can you guys please let me know what is my issue in below code?
https://dl.dropboxusercontent.com/u/75720388/start_project.zip
Thanks
Like this -> this.btnNode.setPositionX(3000);
错误地址
这个要从哪里入手写
newStar.getComponent('Star').game=this;
不注释出现错误,注释无法实现功能
报错:(271)ERROR: Uncaught TypeError: Cannot read property 'length' of null, location: src/cocos2d-jsb.js:0:0
Simulator: E/jswrapper (534): [ERROR] (..\cocos\scripting\js-bindings\jswrapper\v8\Object.cpp, 534): Invoking function (1012C818) failed!
No error is reported, but the screen is dark, I can hear the audio and feels like I could control it, but I can not see anything.
Creator version: 2.1.2 WIN
Code version: V2.0 polished
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.