对于动画的每一帧都去判断物体是否接触地面,如果接触立即上下跳跃,并且触摸屏幕后水平前移
在contactDetection函数内用rectIntersectsRect()来判断物体是否与地面接触,接触则上跳,
在update函数中调用contactDetection()函数,由于物体开始与地面有交叉那么跳跃动作未完成时物体与地面还是有部分交叉,此时update函数又调用了contactDetection()函数导致物体多次跳跃。
想问下,如果需要完成上述的一个要求,代码逻辑大致应该是怎样的。
我的丑代码
`cc.Class({
extends: cc.Component,
properties: {
hero: {
default: null,
type: cc.Node
},
jumpDuration: 1,
jumpHeight: 100,
jumpACTION_TAG:0,
},
heroJumpUp: function () {
console.log("heroJumpUp")
var jumpUp = cc.moveBy(1, cc.p(0, this.jumpHeight)).easing(cc.easeCubicActionOut())
this.hero.runAction( jumpUp )
},
heroJumpDown:function( ){
console.log("heroJumpDown")
var jumpDown = cc.moveBy(1, cc.p(0, -this.jumpHeight)).easing(cc.easeCubicActionOut())
this.hero.runAction( jumpDown )
},
//判断角色与地面是否接触
contactDetection: function () {
var surfaceBox = this.node.getBoundingBoxToWorld( )
var heroBox = this.hero.getBoundingBoxToWorld( )
if( cc.rectIntersectsRect(surfaceBox,heroBox) ){
console.log("角色当前位置")
console.log(this.hero.y)
this.heroJumpUp()
}
},
onLoad: function () {
this.preHeroY = this.hero.y
},
update:function () {
this.contactDetection( )
}
});
`